TcpClient.Connect (METH)

METHOD Connect : ERROR_ID

Establishes a Tcp connection to a server

VAR
    fbTcpClient         : TcpClient;        (* Instance of function block*)

    (* Instance of TlsCertification with customized names to register new certificate inside security screen *)
    fbTlsCertification  : TlsCertification  := (    CertName    := 'TcpClientCertification',
                                                    UseCase     := 'Tcp Client',
                                                    Oid         := AC500_Tcp.CmpX509Cert_Interfaces.RTS_OID_TLS_WEBCLIENT_AUTHENTICATION);

    stTlsParam          : TLS_PARAMETERS := (   SocketType      := AC500_Tcp.SysSocket2_Type.STD,               (* Socket type - either Standard STD or TLS *)
                                                TlsMethod       := AC500_Tcp.CmpTlsMethod.TLSv2_CLIENT,         (* Tls method - no SERVER allowed in this method - only required for TLS communication *)
                                                TlsVerifyMode   := AC500_Tcp.TLS_VERIFY.TLS_VERIFY_PEER,        (* Tls verify mode - recommended to use peer. TLS_VERIFY_NONE allows connection to unsecure server without certification - only required for TLS communication *)
                                                Sni             := '');                                         (* Server name identification - some server requires additional socket option which are set by the Sni - only required for TLS communication *)

    eErrorId            : AC500_Tcp.ERROR_ID;               (* Error Id of the Connect method *)
    dwAddErrNo          : AC500_Tcp.RTS_IEC_RESULT;         (* Additional error number - list available in GVL of CmpErrors.Erros library *)
    eConnectionState    : AC500_Tcp.TCP_CONNECTION_STATE;   (* Connection state of the Tcp connection *)
    hSocket             : AC500_Tcp.RTS_IEC_HANDLE;         (* Socket handle of the Tcp connection *)
    sRemoteIpAddr       : STRING(15);                       (* IP address of the remote Tcp connection *)
END_VAR


------------
// Cyclic call of fbTcpClient.Connect
eErrorId    :=  fbTcpClient.Connect(    Eth             := ETH.ETH1,
                                        IpAddr          := '192.168.0.10',
                                        Port            := 9000,
                                        TlsParameters   := stTlsParam,
                                        Timeout         := T#5M,

                                        AddErrNo        => dwAddErrNo);

// As long as BUSY, the client is connecting to server
// If eErrorId indicates NO_ERROR, the client successfully connected to the server
IF eErrorId = AC500_Tcp.ERROR_ID.NO_ERROR THEN

    ;   // Continue with send or receive

// If eErrorId does neither has NO_ERROR nor BUSY, an error is pending
ELSIF eErrorId <> AC500_Tcp.ERROR_ID.BUSY THEN

    ;   // For ERR_INTERNAL, please check AddErrNo - AC500_Tcp.RTS_IEC_RESULT - the error codes are listed in the GVL of CmpErros.Errors

END_IF


// Use the properties to get information about
// Connection state
eConnectionState := fbTcpClient.ConnectionState;

// Remote IP address
sRemoteIpAddr := fbTcpClient.IpAddress;

// Handle of socket connection
hSocket := fbTcpClient.Socket;
InOut:

Scope

Name

Type

Initial

Comment

Return

Connect

ERROR_ID

Input

Eth

BYTE

0

Declaration of used ethernet interface

IpAddr

STRING(16)

‘192.168.0.10’

Target IP address of the server that should be connected to

Port

WORD

9000

Target port server

TlsParameters

TLS_PARAMETERS

Tls parameter - as default set to Standard socket

itfTlsCertification

ITlsCertification

0

Interface for Tls Certification - OPTIONAL and only required for client certification

Timeout

TIME

TIME#1m0s0ms

Connection timeout

Output

AddErrNo

RTS_IEC_RESULT

Additional error number gives information about error from SysSock2Close –> please see CmpErrors.Errors