TCP example

The following example shows an implementation of an "echo" client/server. The local client should send a test string to the remote server at certain intervals (e.g. every second). The remote server should then immediately resend the same string unchanged to the client.

The client should be implemented as a function block, of which several instances can be created. The server should be able to communicate with several clients.

Several instances of the server may be created. Each server instance is then addressed via a different port number. The server implementation is more difficult if the server has to communicate with more than one client. An implementation of a suitable client in .NET is also presented. The example can be used as a basis for realising more complex implementations.

System requirements

Project sources

Project description

Auxiliary functions in the project example

In the example several functions, constants and function blocks are used, which are briefly described below:

FUNCTION LogError : DINT

TCP example 1:

The function writes a message with the error code into the log book of the operating system (Event Viewer). The global variable bLogDebugMessages must first be set to TRUE.

FUNCTION LogMessage : DINT

TCP example 2:

The function writes a message into the log book of the operating system (Event Viewer) if a new socket was opened or closed. The global variable bLogDebugMessages must first be set to TRUE.

FUNCTION SCODE_CODE : DWORD

TCP example 3:

The function masks the lower 16bits of a Win32 error code returns them.

Global constants/variables

Name

Default value

Description

bLogDebugMessages

TRUE

Activates/deactivates writing of messages into the log book of the operating system;

MAX_CLIENT_CONNECTIONS

5

Maximum number of remote clients that can simultaneously establish a connection with the server;

MAX_PLCPRJ_RXBUFFER_SIZE

1000

Max. length of the internal receive buffer;

PLCPRJ_RECONNECT_TIME

T#3s

SERVER: After this time has elapsed, the local server will attempt to re-open the listener socket;

CLIENT: Once this time has elapsed, the local client will attempt to re-establish the connection with the remote server;

PLCPRJ_SEND_CYCLE_TIME

T#1s

The test string is sent cyclically at these intervals from the local client to the remote server;

PLCPRJ_RECEIVE_POLLING_TIME

T#1s

SERVER and CLIENT: The client and server reads (polls) data from the server or client using this cycle;

PLCPRJ_RECEIVE_TIMEOUT

T#50s (SERVER)

T#10s (CLIENT)

SERVER: After this time has elapsed, the local server aborts the reception if no data bytes could be received during this time;

CLIENT: After this time has elapsed, the local client aborts the reception if no data bytes could be received during this time;

PLCPRJ_ACCEPT_POLLING_TIME

T#1s

At these intervals, the local server will attempt to accept the connection requests of the remote client;

PLCPRJ_ERROR_RECEIVE_BUFFER_OVERFLOW

16#8101

Sample project error code: Too many characters without zero termination were received;

PLCPRJ_ERROR_RECEIVE_TIMEOUT

16#8102

Sample project error code: No new data could be received within the timeout time (PLCPRJ_RECEIVE_TIMEOUT).