Professional Documents
Culture Documents
discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/262198350
CITATION READS
1 8,299
1 author:
Wojciech Gomolka
Festo France
14 PUBLICATIONS 11 CITATIONS
SEE PROFILE
All content following this page was uploaded by Wojciech Gomolka on 12 May 2014.
CoDeSys® v.2.3.9
Library SysLibSockets.lib
Part 1
UPD Client/Server
1
Introduction:
Basic Function Blocks for communication over Ethernet
In automation applications, there are four general use Function Blocks for communication over Ethernet
network.
1. UDP Server
2. UDP Client
For connectionless communication over Ethernet under UDP protocol
3. TCP Server
4. TCP Client
For connection oriented communication over Ethernet under TCP protocol
These Function Blocks are based on the SOCKET concept, so in this notice, we will describe some basic
elements of the socket implementation and its first application to communicate under simple,
connectionless Ethernet protocol: UDP.
We present a simple application UDP Client/Server made under CoDeSys ® and which uses some FBs
provided by CoDeSys library: SysLibSockets.
2
1. Basic concepts of Client and Server programming
The theory and practice of Client Server systems based on the concept of Ethernet socket is very broad.
Nevertheless there are some general rules and basic steps for Client/Server connections which will be
discussed in this chapter.
For more information, you can go see different dedicated sources.
1.1 Sockets
The basic building block for communication is the socket.
A socket is an endpoint of communication via which an application (process) can send and receive data.
Each socket in use has a type and an associated process.
A socket exists as long as the associated process (application) maintains an open link to the socket.
When a process creates a socket (e.g. with socket() C-function or SysSockCreate() for CoDeSys), some
specifying parameters must be used:
a) Socket domain (address family)
b) Socket type
c) Socket supported protocols
These parameters define the socket application and how it interoperates with other socket applications.
The parameter Socket domain (address family) determines the format of the address structure to
use on socket functions and supported protocols for data transportation from one application to
another.
3
For socket applications that use Internet protocols, Address family is described by global constant
AF_INET (SOCKET_AF_INET := 2 for CoDeSys).
Addresses for AF_INET sockets are IP addresses and port number and address structure is
described by basic structure sockaddr
Normally, applications are presumed to communicate only between sockets of the same type.
Note:
When Internet socket is created, the parameter supported protocols is set to 0 (default value)
4
1.1.4 Socket descriptor
When socket is created, the function returns the socket identifier: socket descriptor.
This descriptor is used as input parameter in other Socket oriented functions.
Note:
Depending OS, if socket creation was incorrect, a special constant INVALID_SOCKET (or SOCKET_INVALID) is
assigned to the socket descriptor.
E.g. CoDeSys :
SOCKET_INVALID: DINT := -1;
C/C++
int my_socket;
CoDeSys
VAR
xSocketIsOpen: BOOL; (* bit info : Socket is open *)
diSocketHandle: DINT; (* descriptor of open Socket *)
diErrCode: DINT; (* this FB Error code *)
diAddressFamily: DINT := SOCKET_AF_INET;(* internet: UDP, TCP, ..*)
diType: DINT := SOCKET_DGRAM; (* connectionless socket *)
diProtocol: DINT := SOCKET_IPPROTO_UDP;(* User Datagram Protocol *)
END_VAR
IMPORTANT:
Dont forget tests of the result for socket creation: your socket must be created correctly !!
5
1.1.5 How do sockets work?
The client and server communication model requires a well-known set of conventions before service
may be accepted and rendered. This set of conventions comprises connection modes and set of
protocols which must be implemented at both ends of a connection (sockets).
In both cases, the socket on the server process waits for requests from a client.
To do this, each server has at least to create a socket and to establish (binds) an address that
clients can use to find the server. When the address is established, the server waits for clients to
request a service.
The client-to-server data exchange takes place when:
o a client connected to the server through a socket,
o the server performs the client's request, and
o it sends the reply back to the client.
The server has to take care for closing the connection when client has closed its appropriate
connection.
6
1.1.6 How to build a UDP Client/Server socket application
The following figure illustrates the relationship between the Server and Client application in a
connectionless design.
Each step of the diagram needs the usage of specific socket functions.
Some details on the use of a particular function will be presented in next chapters.
More information could be found in respective technical documentation, e.g. online help for
operating systems.
1. The socket creation function returns a socket descriptor. The INET (Internet Protocol)
address family with the UDP transport (SOCK_DGRAM) will be used for this socket.
2. After the socket descriptor is created, a bind() function establishes a unique entry point
(IP address and port) for the socket that clients can use to communicate with the server..
3. The server uses the RecvFrom() function to receive data (client request).
4. The SendTo() function is used to send data (server response) to the client.
5. The Close() function closes any open socket and destroys corresponding descriptors.
Connectionless client uses the same sequence of function calls, excepted step 3 where it uses the
SendTo() function to send (firstly) the data (request) to the server, and step 4 where RecvFrom()
function is used to receive the data back (response) from the server.
Note:
Binding is optional for Client. It can use a default port to communicate with Server.
But bind() is mandatory for the Server.
7
1.1.7 How to build a TCP Client/Server socket application
The following figure illustrates the relationship between the Server and Client application in a
connection oriented protocol design.
Connection oriented client doesnt use function calls for Listen() and Accept(). At step 3 it uses the
Connect() function to establish a connection to the server.
8
1.2 Sockets and Client/Server applications: Résumé
Then, what socket function to use, when and in what order?
UDP Client:
C/C++ CoDeSys
socket() SysSockCreate()
bind() SysSockBind()
sendto() SysSockSendTo()
recvfrom() SysSockRecvFrom()
close() SysSockClose()
UDP Server:
C/C++ CoDeSys
socket() SysSockCreate()
bind() SysSockBind()
recvfrom() SysSockRecvFrom()
sendto() SysSockSendTo()
close() SysSockClose()
Note:
- For connectionless communication, the order of sento() and recvfrom() is important in Client or
Server application,
- As there is connectionless communication, in the final application with UDP Server you must
include some routines for detecting a disconnection of the Client; e.g. by sending of the special
character by Client just before disconnection.
TCP Client:
C/C++ CoDeSys
socket() SysSockCreate()
connect() SysSockConnect()
send() SysSockSend()
recv() SysSockRecv()
close() SysSockClose()
TCP Server:
C/C++ CoDeSys
socket() SysSockCreate()
bind() SysSockBind()
listen() SysSockListen()
accept() SysSockAccept()
send() SysSockSend()
recv() SysSockRecv()
close() SysSockClose()
Note:
- For connection oriented communication, the order of send() and recv() functions does not
matter. You can call one before the other or vice versa. They are also optional.
9
1.3 PLC application: Blocking/non blocking mode of sockets
The socket is basically created in the blocking mode. Some socket function calls are blocking.
This may cause trouble in PLC application because the program will stop on function call and wait
for the end.
By default, the blocking call of socket function will happen for the following CoDeSys functions:
- SysSockRecv(), SysSockSend(),
- SysSockRecvFrom(), SysSockSendTo(),
- SysSockConnect(), SysSockAccept(),
- SysSockClose();
The second method is the simplest way to have non blocking mode of the socket behaviour.
CoDeSys library, SysLibSocket.lib provides the function of the type DINT:
SysSockIoctl(diSocket, diCommand, piParameter)
Where:
diSocket : DINT; is the descriptor of the socket, returned by SysSockCreate
diCommand : DINT; the command (with corresponding parameters) to apply on the socket
piParameter : DWORD; pointer to the command parameter
If you want to put the socket into non blocking mode, you must use a global constant (defined in
the library) which is recommended as diCommand in the SysSockIoctl call:
SOCKET_FIONBIO : DINT := 2;
And piParameter is a pointer to a variable which has to be set to value unequal zero if the socket
must be in non blocking mode.
Also, the following call could be used to put a socket into non blocking mode:
diNoBlock: DINT := 1;
SysSockIoctl(diSocket, SOCKET_FIONBIO, ADR(diNoBlock));
10
2. CoDeSys Basic Function Blocks for communication on Ethernet
After reception of UDP message sent by a Client, the Server may respond to this client.
By setting the variable xSendAnswer a message can be transmitted to the client with coordinates described
by output variables strLastSenderAddr and udiSenderPort.
After execution, the variable xSendAnswer will be reset by Function Block.
IMPORTANT: Server waits for first message from Client. And if diRcvPacketCount is > 0, it is able to answer.
11
2.2 UDP Client
The Function Block UDPClient gives a possibility to connect the application to a UDP Server.
strRemoteIPaddr is the IP address of the Server.
iRemotePort defines the communication port for the Server.
iLocalPort defines the local communication port for the FB UDPClient.
By setting the variable xSendStart a message (e.g. with request) can be transmitted to the server.
Coordinates of the Server are described by strRemoteIPaddr and iRemotePort variables.
After execution, the Function Block will reset this variable.
Length of messages is defined by diNbBytesToSend and limited to value defined by diMaxDataSize.
Maximal accepted value is 1472 Bytes due to the UDP protocol specification.
If UDPClient receives any message (e.g. from Server), coordinates of the sender are given by Output
variables:
- strLastSenderAddr : for IP address of the sender
- udiSenderPort : for the communication port of the Sender
In the second case, coordinates of the receiver are described by strLastSenderAddr and udiSenderPort.
After transmission, the Function Block will reset xSendAnswer.
Length of the answer messages is defined by diNbBytesToSend.
Type Description
EN BOOL Enable FB, if TRUE this FB will be evaluated completely normally
strRemoteIPaddr STRING IP address of the remote Server
udiRemotePortr DINT Ethernet port of Remote Server
iLocalPort INT Local, assigned Ethernet port
diMaxDataSize DINT Max size of data to transmit, if size >1472 or =0, default values are used
(1472 bytes due to the UDP protocol specification)
ptReceiveBuffer POINTER Points to a buffer (ARRAY OF BYTE) where received data should be stored
TO BYTE
diReceiveBufferSize DINT Specifies the length of the buffer (in bytes)
ptSendBuffer POINTER Points to a buffer (ARRAY OF BYTE) where transmit (send) data should be
TO BYTE stored
diNbBytesToSend DINT Specifies the number of data (bytes) to send
12
Tab 2.2-2 FB UDPClient : Output variables
Type Description
ENO BOOL TRUE if FB enabled and is evaluated completely
xSocketIsOpen BOOL TRUE if socket is correctly open
diSocketHandle DINT The socket file descriptor
diNbRcvBytes DINT The number of received bytes (by SysSockReceiveFrom)
strLastSenderAddr STRING IP address of the last sender which sent a UDP message
udiSenderPort UDINT Ethernet port of the last sender
diErrCode DINT Error code
iPhase INT Nb of internal step of FB execution (for test purposes)
NOTE:
For the complete source code of the CoDeSys project and described POUs: please
contact the author of this notice.
13