Setting up a TCP link


Introduction

           The Transmission Control Protocol (TCP) is one of the main protocols on which TCP/IP networks are based. It is a connection based protocol, so the transmission is over a connection set up between two points. It is a very reliable method of sending data because the receiver acknowledges data sent when it reaches the destination. TCP organizes data in streams of bytes and is always full duplex.

More about TCP?

QUANTA provides users direct control of lower level tcp socket calls.


QUANTA classes used

    QUANTAnet_tcpServer_c

                A server can be set up using this class. The port number on which the server is to be opened is given to the init() method. After this we can use the checkForNewConnections() method to look for incoming client connections.

    QUANTAnet_tcpClient_c

                A client can be created using this class. It can connect to the server set up using QUANTAnet_tcpServer_c class using connectToServer(). The clients can carry out read() and write() operations in the blocking or non blocking mode. If the non blocking mode is being used the isReady(), isReadyToRead(), isReadyToWrite() methods may be useful. You can also change the timeout values for the blocking mode by using setTimeOut() .
                Performance measuring methods are also available and the showStats() method should be typically executed after a read/write is done over a network.

                The server.cxx program in the demo sets up a TCP server and the client.cxx program runs a client which connects to the server. The client sends a message 'TICK(number of time intervals)' in every time interval and sends an 'UPDATE' message every ten time intervals. When the server receives the 'UPDATE' message it broadcasts the data in it's buffers to all the clients. This demos allows 10 clients to connect to the server.


Usage

 1. To run the server
          servertcp

 2. To run a client
           clienttcp <server-address>

Output

    When a client connects to the server the message displayed looks like :
        Client 0 connected
        Read in data from client 0
        Data for client 0 stored: (TICK(1))
    After 10 time intervals the server receives the 'UPDATE' message and so it displays :
        Read in data from client 0
        UPDATE received
    The server then sends out the contents of its buffers to all the clients and the clients may then display messages like :
        Data received: Client #1  TICK(6)

    Performance measuring statistics are also displayed.


for more information: Post your questions online on the Quanta forum page.