Sockets enable communication between computers in a network. Every time client machine need to talk to a server (for e.g.: client’s browser requesting a webpage from internet), a socket is opened, and it is used for transferring information between client and server. In general, sockets refer to Internet Protocol(IP) for relaying data packets across network boundaries. TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are widely used protocols for one-to-one connections between two nodes in a network. TCP protocol guarantees the recipient will receive the packets in the order they are sent, but is relatively slow, hence suitable for applications that require higher message reliability. UDP is faster and does not guarantee that the messages or packets sent would reach at all. UDP is used when speed is desirable and error correction is not necessary. For example, UDP is frequently used for live broadcasts and online games. For this blog, we will focus on TCP sockets only and especially TCP socket communication with WSO2 ESB.
TCP Socket protocol makes it possible to establish a continuous full-duplex connection with the stream of bytes between a client and a server. Sockets are associated with a socket address, which consists of an IP address and a port number.
TCP socket session needs to be created between client and server to get the communication going in form of bytes stream. When TCP Socket session is established, socket streams are created for Inbound and Outbound traffic between a client and a server.
TCP Transport in WSO2 ESB
The TCP transport in WSO2 ESB allows you to send and receive SOAP messages over the TCP to the TCP Proxy through the same TCP connection.
To enable TCP transport in WSO2 ESB, add Transport sender and Receiver blocks in axis2.xml (path: ESB_HOME/repository/conf/axis2/axis2.xml).
Transport receiver block:
<transportReceiver name="tcp" class="org.apache.axis2.transport.tcp.TCPTransportListener">
Transport Sender block:
<transportSender name="tcp" class="org.apache.axis2.transport.tcp.TCPTransportSender"/>
See here for details on how to enable TCP transport.
Refer this sample 266 to see a Proxy receiving SOAP messages over TCP and forwarding them over HTTP.
Important aspects of TCP Communication in Real-world
Here are some aspects of TCP socket connections to be considered for design of process flows for client/Server communication in Real-world:
As we are dealing with bytes stream in TCP socket connection, both inbound and outbound messages need to be included inside special characters to indicate message boundaries. (for eg: we can use special characters STX for start of message and ETX for end or message).
int STX = 2, ETX = 3;
String stx = new Character((char) STX).toString();
String etx = new Character((char) ETX).toString();
String OutMessage = stx + “Hello Server!” + etx;
Note: There is a separate WebSocket protocol that enables a stream of messages instead of a stream of bytes, in which you don’t have to manage message boundaries. In fact, WebSocket is basically a message-oriented application protocol, which makes use of TCP as a transport layer.
Clients can send the request and block the outgoing stream until it has received a response from the server in a specified time interval. This is also called synchronous mode of communication with the server.
Client can also choose not to block the outgoing stream after sending a request in which case it is called asynchronous mode of communication. In the Asynchronous mode of communication, clients can send multiple requests to Server without blocking or waiting for the response to previous requests.
Single port or Dual Port Communication
TCP Sockets communication can be set up in one of two different ways:
Single socket– Requests flow in both directions between client and Server through one socket. The Server listens for and accepts connections from the client.
If using a single socket, then Client must be aware that after sending a request, the next incoming message is not guaranteed to be the response to that request, but instead may be an unsolicited notification or a request from the Server. The client must examine all of the members of the incoming Request to determine how to correctly handle the message.
Dual socket – This setup provides a clearer distinction between client and server roles. Each side acts as both a client and a server and establishes a connection with the other. Requests are always sent from client to server over the socket connection that was established by the side sending the message. Both sides must listen for and accept connections.
In a short-lived connection, the client creates a TCP connection with Server, sends or receives messages and closes the connection. In a long-lived connection, the client creates TCP connection with Server and it is left open as long as transactions are flowing, and the connection is closed after a period of inactivity on the socket connection. Based on requirements, the client may need to send the heartbeat messages to Server to keep the TCP session active at regular time intervals.
You may choose to develop your own Java client or server apps to mimic client or server roles, however it would be useful to have some traffic debugging tools like: Tcpmon, Tcpdump & Hercules to save you ton of time.
The TCP transport in WSO2 ESB allows you to send and receive SOAP messages over the Transmission Control Protocol (TCP) to the TCP Proxy running on WSO2 ESB through the same TCP connection. But not all aspects of TCP communication are configurable in TCP transports. Based on your requirements if TCP Transport configuration in WSO2 ESB are not sufficient, you may consider developing custom mediator and/or custom inbound Endpoints for TCP communication.