T_HSOCKET
Variables of this type represent a connection handle or a handle of an open socket. Via this handle, data can be sent to or received from a socket. The handle can be used to close an open socket.
TYPE T_HSOCKET
STRUCT
handle : UDINT;
localAddr : ST_SockAddr; (* Local address *)
remoteAddr : ST_SockAddr; (* Remote endpoint address *)
END_STRUCT
END_TYPE
handle: Internal TwinCAT TCP/IP Connection Server socket handle;
localAddr: Local socket address;
remoteAddr: Remote socket address;
The following sockets can be opened and closed via the TwinCAT TCP/IP Connection Server: listener socket, remote client socket, or local client socket. Depending on which of these sockets was opened by the TwinCAT TCP/IP Connection Server, suitable address information is entered into the localAddr and remoteAddr variables.
Connection handle on the server side
- The function block FB_SocketListen opens a listener socket and returns the connection handle of the listener socket;
- The connection handle of the listener sockets is transferred to the function block FB_SocketAccept. FB_SocketAccept will then return the connection handles of the remote clients.
- The function block FB_SocketAccept returns a new connection handle for each connected remote client.
- The connection handle is then transferred to the function blocks FB_SocketSend and/or FB_SocketReceive, in order to be able to exchange data with the remote clients;
- A connection handle of a remote client that is not desirable or no longer required is transferred to the function block FB_SocketClose, which closes the remote client socket.
- A listener socket connection handle that is no longer required is also transferred to the function block FB_SocketClose, which closes the listener socket.
Connection handle on the client side
- The function block FB_SocketConnect returns the connection handle of a local client socket.
- The connection handle is then transferred to the function blocks FB_SocketSend and FB_SocketReceive, in order to be able to exchange data with a remote server;
- The same connection handle is then transferred to the function block FB_SocketClose, in order to close a connection that is no longer required.
The function block FB_SocketCloseAll can be used to close all connection handles (sockets) that were opened by a PLC runtime system. This means that, if FB_SocketCloseAll is called in one of the tasks of the first runtime systems (port 801), all sockets that were opened in the first runtime system are closed.
Requirements
Development environment | Target system type | PLC libraries to be linked |
---|---|---|
TwinCAT version 2.8.0 or higher | PC or CX (x86) | TcpIp.Lib ( Standard.Lib; TcBase.Lib; TcSystem.Lib are included automatically ) |
TwinCAT v2.10.0 Build >= 1301 | CX (ARM) |