ProSoft Technology MVI69E-LDM User Manual
Page 47

MVI69E-LDM ♦ "C" Programmable
Contents
Linux Application Development Module
Developer's Manual
ProSoft Technology, Inc.
Page 47 of 130
August 21, 2014
To execute the sample:
1 Navigate to the default home directory
/psft/sample
.
2 Type the command
./Server_Sample&
to run the program as a background task. The
server will wait forever processing requests from clients.
While reviewing the source code, you'll see that the program:
registers
sigquit_handler
for four signals using the signal function.
checks command line and prints usage message if needed.
opens the backplane using
open_backplane()
. See the description in
Backplane_Sample
.
initializes the LEDs on the front panel.
Calls the function
socket()
to create an UN-named socket inside the kernel.
socket()
returns an integer known as a socket descriptor:
o
The function takes domain/family as its first argument. For Internet family of IPV4
addresses, use
AF_INET
.
o
The second argument
SOCK_STREAM
specifies that type of connection to use. In this
case, a sequential, reliable, two way connection is desired.
o
The third argument select the protocol. Generally, this is zero as the system normally
only has one protocol for each type of connection, although it is possible to have
multiple protocols for a connection type. Zero tells the system to use the default
protocol for the specified type of connection. In this case, the default is TCP.
zeros out the
send buff
and
serv addr
variables.
In preparation for the call to
bind()
, the
serv_addr
is then set to the well known port
address
SERVER_PORT_NUMBER
, and any IP address. This allows a connection to be
accepted from any IP address as long as the well known port address is specified.
calls function
bind()
to assign the address specified in the structure
serv_addr
to the
socket created by the call to
socket ()
.
calls function
listen()
with second argument as '10' to specify the maximum number of
client connections that the server will queue for this listening socket.
The call to
listen()
makes the socket a functional listening socket.
Code enters an infinite while loop in which:
o
the call to
accept()
puts the server to sleep, waiting for an incoming client request.
When a request is received and the three way TCP handshake is complete,
accept()
wakes up and returns the socket descriptor representing the client socket.
o
time()
is called to read the current system time.
o
snprintf()
is used to put the time into the send buffer in a human readable format.
o
write()
is then called to send formatted time to the client.
o
close()
is then used to close the connection to the client.
o
sleep()
is invoked to yield the processor for one second.
Client ENet Sample
To run the Client ENet Sample:
1 Establish a command window using Telnet or similar terminal software on the PC
through the Serial P1 port.
2 Login as user:
root
, using password:
password
.
3 The Ethernet Port E1 will be used to communicate with the server. The server and client
devices must both be connected on the same IPv4 subnet.
4 Set the IPv4 address and mask of the first Ethernet port using
ifconfig
command.