TcpConnect
Description
This function block creates a new socket and performs the connect operation using default TCP settings, and a specified server address and port. You will have use the TcpClose function block to release the socket returned by TcpConnect.
-
- It is possible that the function returns a valid socket ID even if the connection to the server is not yet actually performed. After calling this function, you must use the TcpIsConnected function block to know if the connection is ready.
Figure 7-221: The TcpConnect function block
Related Functions
TcpAccept, TcpBinReceive, TcpBinSend, TcpClose, TcpIsConnected, TcpIsValid, TcpListen, TcpReceive, TcpSend
Arguments
Input
Execute | Description | On the rising edge A rising edge is the transition of a digital signal from low to high. It is also called positive edge request to connect a socket to a server. |
Data Type | BOOL | |
Range | 0, 1 | |
Unit | N/A | |
Default | — | |
Address | Description | The IP Address of the remote server. |
Data Type | STRING | |
Range | N/A | |
Unit | N/A | |
Default | — | |
Port | Description | The network port to use. |
Data Type | DINT | |
Range | N/A | |
Unit | user units | |
Default | — |
Output
Done | Description | If TRUE, then the command completed successfully |
Data Type | BOOL | |
Error | Description | If TRUE, an error has occurred |
Data Type | BOOL | |
ErrorID | Description | Indicates the error if Error output is set to TRUE. See the table in Search IndexFile and TCP/IP Function Block ErrorID Output |
Data Type | DINT | |
ID | Description | ID of the new client socket |
Data Type | UDINT |
Example
Structured Text
(* TcpConnect example *)
CASE StepCounter OF
0:
Inst_TcpConnect(TRUE, '192.168.1.1', 1234);
StepCounter := StepCounter + 1;
1:
Inst_TcpConnect(TRUE, '192.168.1.1', 1234);
IF Inst_TcpConnect.Done THEN
MySocketID := Inst_TcpConnect.ID;
Inst_TcpConnect(FALSE, '', 0);
StepCounter := StepCounter + 1;
END_IF;
END_CASE;