Socket Interface Example V4.0.0.x Usage Guide

The example demonstrates the TCP/UDP server and client ability of the socket interface (netX 90 - Ethernet Interface - Examples).

For the opposite part, any TCP/UDP server respectively client can be used, for example Socket Test shown below (download link: https://sourceforge.net/projects/sockettest/).


The Socket Sources in the example are in the folder: Components\cifXApplicationDemoSockIf\Sources:


To run the example, communication channel 1 (in case of netX90) must be activated:

Configuration

The configuration of the socket interface is done by hSockCtrl[] table in AppSockIf_DemoApplicationFunctions.c. Several instances may be created, thus an amount of combination of TCP/UDP servers and clients can be implemented, the limitation of the instances is given by the BACKLOG define, the bigger the BACKLOG, the fewer instances are possible.

TCP Server

With following configuration a TCP Server accepts clients on TCP port 300, the IP address of the running real-time Ethernet protocol must be also configured (e.g. 192.168.0.44).

TCP Server
static SOCKIF_SOCK_CTRL_T s_tSockCtrl[1] =
{
  // TCP Server
  {     SOCK_SERVER,
    SOCK_AF_INET,
    SOCK_SOCKET_STREAM,
    SOCK_IPPROTO_TCP,
    {SET_OPT<<SOCK_TCP_NODELAY | SET_OPT<<SOCK_TCP_KEEPCNT | SET_OPT<<SOCK_TCP_KEEPIDLE | SET_OPT<<SOCK_TCP_KEEPINTVL \
      | SET_OPT<<SOCK_SO_KEEPALIVE | SET_OPT<<SOCK_SO_LINGER | SKIP_OPT<<SOCK_SO_REUSEADDR,
      {
        {0, 0}, /* reserved*/
        {1, 0}, /* 1: NoDelay/disable Nagle, reserved */
        {0, 0}, /* reserved, reserved */
        {0, 0}, /* reserved, reserved */
        {0, 0}, /* reserved, reserved */
        {3, 0}, /* KeepAliveCnt 1 ... 65535, reserved */
        {4, 0}, /* KeepAliveIdle 1 ... 65535, reserved */
        {2, 0}, /* KeepAliveIntvl 1 ... 65535, reserved */
        {1, 0}, /* 1: enable KeepAlive, reserved */
        {0, 0},/* 0/1: disable/enable Linger, 0 ... 65535 seconds*/
        {0, 0}, /* reuse addr (prepared), reserved */
        {0, 0}, /* broadcast (prepared ), prepared */
      }
    },
    0,          /* possible flags: SOCK_FL_O_NONBLOCK | SOCK_FL_O_NONBLOCK_CONNECT */
    0,          /* IP address to bind (TCP server mode) */
    300,        /* Input port number */
    0,          /* Destination IP address for client, not yet known in server mode */
    0,          /* Destination port number, not applicable for TCP  */
    {0},        /* Socket state flags */
    {{0}},      /* Socket handles */
  },
};


Once the server is running, a client can connect to it, the server example will echo each sent message back to the client.


Second TCP Server

It is possible to run a second TCP Server, that can be achieved by extending the field by a second configuration set with a different port number.

Second TCP Server
static SOCKIF_SOCK_CTRL_T s_tSockCtrl[2] =
{
  // TCP Server
  { ...  },
  // Second TCP Server
  {     SOCK_SERVER,
    SOCK_AF_INET,
    SOCK_SOCKET_STREAM,
    SOCK_IPPROTO_TCP,
    {SET_OPT<<SOCK_TCP_NODELAY | SKIP_OPT<<SOCK_TCP_KEEPCNT | SKIP_OPT<<SOCK_TCP_KEEPIDLE | SKIP_OPT<<SOCK_TCP_KEEPINTVL \
      | SKIP_OPT<<SOCK_SO_KEEPALIVE | SET_OPT<<SOCK_SO_LINGER | SKIP_OPT<<SOCK_SO_REUSEADDR,
      {
        {0, 0}, /* reserved*/
        {1, 0}, /* 1: NoDelay/disable Nagle, reserved */
        {0, 0}, /* reserved, reserved */
        {0, 0}, /* reserved, reserved */
        {0, 0}, /* reserved, reserved */
        {0, 0}, /* KeepAliveCnt 1 ... 65535, reserved */
        {0, 0}, /* KeepAliveIdle 1 ... 65535, reserved */
        {0, 0}, /* KeepAliveIntvl 1 ... 65535, reserved */
        {0, 0}, /* 1: enable KeepAlive, reserved */
        {1, 10},/* 0/1: disable/enable Linger, 0 ... 65535 seconds*/
        {0, 0}, /* reuse addr (prepared), reserved */
        {0, 0}, /* broadcast (prepared ), prepared */
      }
    },
    SOCK_FL_O_NONBLOCK | SOCK_FL_O_NONBLOCK_CONNECT,          /* possible flags: SOCK_FL_O_NONBLOCK | SOCK_FL_O_NONBLOCK_CONNECT */
    (192<<24 | 168<<16 | 210<<8 | 19), /* IP address to bind (TCP server mode) */
    301,        /* Input port number */
    0,          /* Destination IP address for client, not yet known in server mode */
    0,          /* Destination port number, not applicable for TCP  */
    {0},        /* Socket state flags */
    {{0}},      /* Socket handles */
  },
};


TCP Client

For the client configuration, the destination port number needs to be defined.

TCP Client
static SOCKIF_SOCK_CTRL_T s_tSockCtrl[1] =
{
  // TCP Client
  {     SOCK_CLIENT,
    SOCK_AF_INET,
    SOCK_SOCKET_STREAM,
    SOCK_IPPROTO_TCP,
    {SET_OPT<<SOCK_TCP_NODELAY | SKIP_OPT<<SOCK_TCP_KEEPCNT | SKIP_OPT<<SOCK_TCP_KEEPIDLE | SKIP_OPT<<SOCK_TCP_KEEPINTVL \
      | SKIP_OPT<<SOCK_SO_KEEPALIVE | SKIP_OPT<<SOCK_SO_LINGER | SKIP_OPT<<SOCK_SO_REUSEADDR,
      {
        {0, 0}, /* reserved*/
        {1, 0}, /* 1: NoDelay/disable Nagle, reserved */
        {0, 0}, /* reserved, reserved */
        {0, 0}, /* reserved, reserved */
        {0, 0}, /* reserved, reserved */
        {0, 0}, /* KeepAliveCnt 1 ... 65535, reserved */
        {0, 0}, /* KeepAliveIdle 1 ... 65535, reserved */
        {0, 0}, /* KeepAliveIntvl 1 ... 65535, reserved */
        {0, 0}, /* 1: enable KeepAlive, reserved */
        {0, 0}, /* 0/1: disable/enable Linger, 0 ... 65535 seconds*/
        {0, 0}, /* reuse addr (prepared), reserved */
        {0, 0}, /* broadcast (prepared ), prepared */
      }
    },
    SOCK_FL_O_NONBLOCK_CONNECT, /* possible flags: SOCK_FL_O_NONBLOCK | SOCK_FL_O_NONBLOCK_CONNECT */
    0,          /* IP address to bind (TCP server mode) */
    0,          /* input port number, not applicable for client ??? */
    (192<<24 | 168<<16 | 210<<8 | 100), /* Destination IP address for client, not yet known in server mode */
    300,        /* Destination port number */
    {0},        /* Socket state flags */
    {{0}},      /* Socket handles */ 
  },
};



Use the IP address of the Ethernet adapter for TCP Server, set the destination port number and start listening. The TCP client of the example will echo each received message back to the TCP Server:



UDP Server

For UDP Server input and output port numbers need to be defined.

UDP Server
static SOCKIF_SOCK_CTRL_T s_tSockCtrl[3] =
{
  // TCP Server
  { ...  },
  // TCP Client
  { ...  },
  // UDP Server  
  {      SOCK_SERVER,
    SOCK_AF_INET,
    SOCK_SOCKET_DGRAM,
    SOCK_IPPROTO_UDP,
    {0,{{0}}},    /* options */
    SOCK_FL_O_NONBLOCK_CONNECT, /* possible flags: SOCK_FL_O_NONBLOCK | SOCK_FL_O_NONBLOCK_CONNECT */
    0,          /* IP address to bind (TCP server mode) */
    310,        /* Input port number */
    0,          /* Destination IP address for client, not yet known in server mode */
    311,        /* Destination port number */
    {0},        /* Socket state flags */
    {{0}},      /* Socket handles */  
  },
};



Use the IP address of the Ethernet adapter for remote UDP Server, set the destination port number and start listening. The UDP server of the example will echo each received message back to the remote UDP Server:


For the transmitting and receiving, following commands need to be served: SOCK_CMD_RECVFROM_REQ and SOCK_CMD_SENDTO_REQ


Wireshark

To see the transmitted packets just use "tcp" filter, to blank out non TCP frames, and follow TCP stream by using context menu of one of the frames: