↑ Software ↑


tcp_util - TCP/IP Networking Utilities

The TCP_UTIL functions allow you to easily establish and communicate over TCP/IP network connections between client and server processes, possibly residing on different hosts. The TCP_UTIL functions follow 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

tcpListen(), tcpAnswer(), and tcpCall() are used to establish a communications link between a client and a server. The server process calls tcpListen() to create a network endpoint at which the server listens for connection requests from clients. When one is received, the server process calls tcpAnswer() 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 tcpCall() 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.

tcpRead() and tcpWrite() 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  <stdio.h>				-- Standard I/O definitions.
    #include  "tcp_util.h"			-- TCP/IP networking utilities.

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

        tcpListen ("name", 99, &server) ;	-- Create listening endpoint.

        for ( ; ; ) {				-- Answer next client.
            tcpAnswer (server, -1.0, &client) ;
            for ( ; ; ) {			-- Service connected client.
                if (tcpRead (client, -1.0, 64, buffer, NULL))  break ;
                printf ("Message from client: %s\n", buffer) ;
            tcpDestroy (client) ;		-- Lost client.


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

    #include  <stdio.h>				-- Standard I/O definitions.
    #include  "tcp_util.h"			-- TCP/IP networking utilities.

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

        tcpCall ("name", 0, &server) ;		-- Call server.

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

        tcpDestroy (server) ;			-- Hang up.


Public Procedures (for listening endpoints)

tcpAnswer() - answers a client connection request.
tcpListen() - creates a listening endpoint.
tcpRequestPending() - checks if a client is trying to connect.

Public Procedures (for data endpoints)

tcpCall() - establishes a client-side connection with a server.
tcpComplete() - completes a no-wait call.
tcpIsReadable() - checks if data is waiting to be read on a connection.
tcpIsUp() - checks if a connection is up.
tcpIsWriteable() - checks if data can be written to a connection.
tcpRead() - reads data from a connection.
tcpSetBuf() - changes the sizes of a connection's receive and send buffers.
tcpWrite() - writes data to a connection.

Public Procedures (general)

tcpDestroy() - closes an endpoint.
tcpFd() - returns the file descriptor for an endpoint's socket.
tcpName() - returns the name of an endpoint.

Source Files


(See libnet for the complete source, including support routines and build files.)

Alex Measday  /  E-mail