TCP/IP: ITcIoTcpProtocol(Recv)

The ITcIoTcpProtocol and ITcIoTcpProtocolRecv interfaces enable TCP/IP communication from the real-time environment.

A project that uses this interface contains a pointer to an ITcIoTcpProtocol object and implements ITcIoTcpProtocolRecv itself. ITcIoTcpProtocolRecv serves as a callback interface for receiving data and events from the TCP/IP module within the application. The interfaces are based on a socket API.
Before a socket can be used, it must be allocated with AllocSocket().

TCP/IP: ITcIoTcpProtocol(Recv) 1: ITcIoTcpProtocolRecv methods:

Name

Description

ReceiveData

Is called by the TCP/UDP RT module as a callback to transfer data.

ReceiveEvent

Is called by the TCP/UDP RT module as a callback if an event has occurred.

TCP/IP: ITcIoTcpProtocol(Recv) 2: ITcIoTcpProtocol methods:

Name

Description

AllocSocket

Allocates a socket.

FreeSocket

Enables a socket.

Connect

Establishes a connection to a remote terminal.

IsConnected

Indicates whether a socket is connected (for inbound and outbound connections).

Close

Closes a socket.

Listen

Opens a TCP port for incoming connections (see remarks).

Accept

For server functionality: Accepts incoming connections (see remarks).

SendData

Sends data (server and client functionality).

CheckReceived

Must be called cyclical; ReceiveEvent and ReceiveData are used as callback in the context of this method (server and client functionality).

GetRemoteIpAddr

Returns the remote IP address of a communication partner.

GetFreeSendDataSize

Returns the number of free bytes in the TCP send buffer.

TCP/IP: ITcIoTcpProtocol(Recv) 3:

Call CheckReceived() continuously.

TCP/IP: ITcIoTcpProtocol(Recv) 4:

Perhaps call AllocSocket() again in the event of an OnlineChange, in order to refresh the callback target.

The client and server implementation process is described here, independent of programming languages.
Only an overview is provided; the samples illustrate the application.

Implementation of an TCP server:

Name

Description

AllocSocket

Opens a socket.

Listen

Opens a port on which connections are expected.

Accept

Is called in the ReceiveEvent() method in order to accept a connection.

ReceiveData

Is called when data are received.

SendData

Can be used to send data.

FreeSocket

On the Listen socket and all connection sockets for stopping.

Code diagram for accepting a connection:

HRESULT CIpStackDemo::ReceiveEvent(ULONG socketId, TCPIP_EVENT tcpEvent)...
case TCPIP_EVENT_CONN_INCOMING:
m_spTcpProt->Accept(socketId);
break;

Implementation of a TCP client:

Name

Description

AllocSocket

Opens a socket.

Connect

Starts connection establishment.

IsConnected

Checks whether the connection was established successfully.

ReceiveData

Is called when data are received.

SendData

Can be used to send data.

FreeSocket

On the Listen socket and all connection sockets for stopping.

TCP/IP: ITcIoTcpProtocol(Recv) 5:

Disconnection by the operating system in Promiscuous mode

If Promiscuous mode is active at the RT Ethernet adapter in the “Adapter” tab, any TCP connection attempts are blocked by the operating system, since it does not recognize a port opened in the TCP/UDP RT object.