Index of Sockets

1. Introduction

Zeus framework was developed for distributed computing such as cell computing. Sockets is a very important concept for interprocess communication on the low level. They are mostly used as point to point communication. Sockets implement simple IO communication over a network and allow to use different protocols.

Following classes implement the concept of socket communication

  • TIPAddress: Abstract class wrapping an IP address.
    • TIPv4Address: Wrapps a IP address version 4 (old style)
    • TIPv6Address: Wrapps a IP address version 6
  • TAbstractSocket: Implements the general socket handling using API functions.
    • TSocket: A client socket for TPC/IP communication.
    • TServerSocket: A server socket for TCP/IP communication
    • TDatagramSocket: A socket used for message oriented communication using UDP
  • TDatagramPacket: Packet of UDP messages
  • Bluetooth Extensions: Implements the handling of bluetooth connections and device finding
    • TBTAddress: Address for bluetooth connections
    • TBluetoothDevice: Represents a bluetooth device
    • TBluetoothSocket: Socket to communicate with the device
  • TSerialConnection: Connection over serial interfaces (COMx for win32)
  • TNetworkInterface: Helps to locate and find network interfaces.

 


2. TCP Networking

The TCP/IP networking is the base for many other services, such as RMI for C++ or messaging. This chapter explains the basics of the TCP socket programming with Zeus-Framework. The TCP networking typically uses a server which handles incomming connections and a client which proactivly initializes the communication.

First the server must be bound to a network interface and a port (a). Then a client can contact the server using the port (b). If the client is accepted (c) a client session is opened. The session socket communicates directly with the client, where as the server is listen to new incomming clients (d).

2.1 TCP Server

First a TCP based server must be created. This can be done with the TServerSocket class. Typically the server is implemented in the main thread (process represents the server) or in a seperate thread if the process implements multiple servers.

Following code shows a simple TCP server. The TServerSocket has following options to set:

  • setReUsable(): This method sets the SO_REUSEADDR flag. This allows your server to bind to a address (and port) which is still in TIME_WAIT state. See also UNIX Socket FAQ.
  • setBlockable(): If this is set the thread blocks if no client is available.
  //The main thread listens to clients
  TAutoPtr<TServerSocket> ptrServerSocket = new TServerSocket(L"localhost", 9000);
  ptrServerSocket->setBlockable(true);
  ptrServerSocket->setReUsable(true);

As second step the server must be bound to the network interface and the port. This can be done with bind().

  if (ptrServerSocket->bind() == RET_NOERROR)
  {
    //Handle all incomming client connections
    while(!bTerminateServer)
    {
      //create sessions here
    }
  }

Within a while loop all incomming client connections can be handled now. For non-blocking server sockets a timeout can be given to accept clients. Blocking server sockets suspend the thread until a client arrives and is accepted. The accept() method returns a TSocket object which is ready for point to point communication with the client. Typically the socket is wrapped in a client session object handling all the communication (and protocol issues).

  TAutoPtr<TSocket> ptrClient;
  if (ptrServerSocket->accept(ptrClient) == RET_NOERROR)
  {
    //Create a client session for communication here
  }
At the very end make sure all client sessions are properly disconnected using disconnect() method. Also the server must be disconnected from the network interface.
  ptrServerSocket->disconnect();

2.2 TCP Client

The client is much simpler than the server. To connect a client to a server a simple TSocket object must be created with the correct IP address and port. The connect() method connects the client with the server. After than reading and writing can be done using the corresponding methods.

  TAutoPtr<TSocket> ptrSocket = new TSocket(L"localhost", 9000);

  //Connects the client
  if (ptrSocket->connect() == RET_NOERROR)
  {
    //Communicates with the server here
  }
Similar to the server the client socket (as well as the client session socket returned by the accept()-method) can be setup with various options:
  • setBlockable(): If this is set the thread blocks when reading and no data is available.
  • setReceiveBufferSize(): Sets the buffer of the incomming data.
  • setSendBufferSize(): Sets the buffer of the outgoing data.
  • setTCPBufferEnable(): Enables or disables the TCP data buffering.