beautypg.com

ProSoft Technology MVI56E-LDM User Manual

Page 53

background image

ControlLogix Platform ♦ "C" Programmable

Understanding the MVI56-LDM API

Linux Application Development Module

Developer's Manual

ProSoft Technology, Inc.

Page 47 of 264

March 12, 2014

The call to the function

bind()

assigns the address specified in the structure

serv_addr

to the socket created by the call to

socket()

.

The call to the function

listen()

with second arguments as '10' specifies the

maximum number of client connections that the server will queue for this
listening socket.

The call to

listen()

makes this 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 description

representing the client socket.

o

time()

is called to read the current system time

o

Snprintf

is used to pu 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 1 second

Client ENetSample
To run the Client Enet sample:
1 Open a command window using telnet or a similar terminal software on the

PC through a serial (P1) or Ethernet port.

2 Login as user: root, password: password.
The Ethernet port E1 is used to communicate with the server device. The server
and client devices must both be connected on the same IPv4 subnet.
Set the IPv4 address and mask of the first Ethernet port using the

ifconfig

command.
To execute the sample:
From the default home directory

/psft

, type the command

./Client_Sample

ip.address.of.server

to run the program. The IP address of the server node

must be provide so the server will know which node is executing the server
program. The client will send a connection request to the server, print the
response from the server to the console, and then exit.
While looking at the the sample source, you'll see that the following occurs:

register

sigquit_handler

for four signals

check command line and print usage message if required

open the backplane using

open_backplane()

initialize the LEDs on the front panel

create a socket with a call to the

socket()

function

initialize the server address (

serv_addr

) structure:

indicate that an IPv4 address is going to be used with

AF_INET

set the destination port as the well known port

SERVER_PORT_NUMBER

Convert the string version of the server IP address to binary with

inet_pton()

After changing the front panel display to run,

connect()

is called to create the

TCP connection to the server