TcpEndpoint - TCP/IP Endpoint

TcpEndpoint objects allow you to easily establish and communicate over TCP/IP network connections between client and server processes, possibly residing on different hosts. The TcpEndpoint class follows a telephone-like model of networking: clients "call" servers and servers "answer" clients. Once a network connection is established between a client and a server, the two can "talk" to each other by reading from and writing to the connection:

                  Client <----------------------> Server

The Listen(), Answer(), and Call() methods are used to establish a communications link between a client and a server. The server process calls Listen() to create a network endpoint at which the server listens for connection requests from clients. When one is received, the server process calls Answer() to accept the client connection. A server may receive and accept connection requests from multiple clients; the operating system automatically creates a new connection (data endpoint) for each client. The server can then multiplex the servicing of the clients or, if the operating system supports it, fork separate subprocesses to service each client.

Client processes call Call() to submit connection requests to the server process (which may be running on another host). Note that a process can be both a client and a server with respect to other processes.

Read() and Write() are used to send and receive data over a network connection. Because there is no concept of record boundaries in a TCP/IP network stream, communicating processes must follow their own protocol in order to determine how long a message is. In the example presented below, the protocol is very simple: every message is exactly 64 bytes long, even if the text of the message doesn't fill the transmitted "record". More sophisticated protocols (e.g., the XDR record marking standard) are available.

The following is a very simple server process that listens for and answers a client "call". It then reads and displays 64-byte messages sent by the client. If the client "hangs up", the server loops back and waits for another client.

    #include  <cstdio>			// Standard I/O definitions.
    #include  "TcpEndpoint.h"			// TCP/IP networking utilities.

    int  main (int argc, char *argv[])
        char  buffer[128] ;
        TcpEndpoint client, server ("name") ;

        server.Listen () ;			// Listen for clients.

        for ( ; ; ) {				// Answer next client.
            server.Answer (client) ;
            for ( ; ; ) {			// Service connected client.
                if (client.Read (64, buffer))  break ;
                printf ("Message from client: %s\n", buffer) ;
            client.Close () ;			// Lost client.


The following client process "calls" the server and, once the connection is established, sends it 16 messages:

    #include  <cstdio>			// Standard I/O definitions.
    #include  "TcpEndpoint.h"			// TCP/IP networking utilities.

    int  main (int argc, char *argv[])
        char  buffer[128] ;
        int  i ;
        TcpEndpoint  server ("name") ;

        server.Call () ;			// Call server.

        for (i = 0 ;  i < 16 ;  i++) {		// Send messages.
            sprintf (buffer, "Hello for the %dth time!", i) ;
            server.Write (64, buffer) ;

        server.Close () ;			// Hang up.


Public Methods

(In addition to those inherited from Endpoint)

TcpEndpoint() - creates a TCP/IP endpoint.
~TcpEndpoint() - destroys a TCP/IP endpoint.
Listen() - listens for connection requests from clients.
Answer() - answers a client connection request.
Call() - establishes a client-side connection with a server.
Complete() - completes a no-wait call.
Read() - reads data from a connection.
Write() - writes data to a connection.

Source Files


Alex Measday  /  E-mail