TCP_ServerOpen block

Short summary

Name

TCP_ServerOpen

→POU type

→function

Category

more system blocks, network blocks, block with internal error diagnostic

→Namespace

logicals.system.network.tcp

Graphical interface

images/download/thumbnails/429720051/TCP_ServerOpen-version-2-modificationdate-1562753067712-api-v2.png

Available since

version 3.2.2 (for library Standard )

Functionality

The block reserves an address and a port for the →TCP server .

Inputs, outputs, return value


Identifier

→Data type

Description

Inputs:

interfaceAddress

DWORD

IP address to reserve

interfacePort

UINT

IP port to reserve

Outputs:

serverHandle

TCP_ServerHandle

handle of the reserved address and port

Return value:

(= rc )

TCP_ErrorCode

returns the status of the TCP operation:

  • succeeded: The operation is successful.

  • invalidHandle: The handle is invalid.

  • cannotListenOnSocket: Too many system resources are reserved.
    Reduce the usage of TCP_ServerOpen.

  • cannotCreateSocket: The socket cannot be created.

  • cannotBindSocket: The socket for the interface cannot be bound.

Input EN and output ENO are available when →calling the block. See "Execution control: EN, ENO" for information on input EN and output ENO.

Internal error diagnostic for block

The block checks the following error cases:

  • The handle is invalid.

  • Too many system resources are reserved.

  • The socket cannot be created.

  • The socket for the interface cannot be bound.

In such an error case, the output ENO of the block is set to value FALSE (or an equivalent). Moreover, the block returns the appropriate code (see the above table).

Example for usage within ST-editor

Program with calls of TCP blocks
PROGRAM TCP_Server
USING logicals.system.network.tcp;
VAR
accptRC : TCP_ErrorCode := invalidHandle;
serverCommData : tcpServerCommData;
sendTestMsg : String[30] := 'Test Message';
init : bool := true;
END_VAR
 
IF init THEN
serverCommData.address := INET_ATON('127.0.0.1');
serverCommData.port := 9999;
 
TCP_ServerOpen(interfaceAddress:=serverCommData.address, interfacePort:=serverCommData.port, serverHandle=>serverCommData.serverHandle);
 
init := false;
END_IF;
 
IF accptRC <> succeeded THEN
accptRC := TCP_ServerAcceptConnection(serverHandle:=serverCommData.serverHandle, clientHandle=>serverCommData.clientHandle);
ELSE
TCP_Send(clientHandle:=serverCommData.clientHandle, data:=sendTestMsg);
END_IF;
END_PROGRAM
 
TYPE
tcpServerCommData : STRUCT
address : DWORD;
port : UINT;
serverHandle : logicals.system.network.tcp.TCP_ServerHandle;
clientHandle : logicals.system.network.tcp.TCP_ClientHandle;
END_STRUCT;
 
tcpClientCommData : STRUCT
serverAddress : DWORD;
serverPORT : UINT;
clientHandle : logicals.system.network.tcp.TCP_ClientHandle;
END_STRUCT;
END_TYPE

When creating your application within the ST-editor, enter a call of a block by typing the text as requested by the syntax or use Content Assist.