Home

GEONius.com
2-Oct-2015
E-mail

lfn_util - Line Feed-Terminated Networking Utilities

The LFN_UTIL functions provide a simple means of sending and receiving line-feed-terminated text over a network connection. The LFN_UTIL package is layered on top of the lower-level TCP_UTIL functions. Network connections can be established between clients and servers and LFN streams are built on these connections.

A simple server process that reads and displays the ASCII text messages it receives could be as brief as the following program:

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

    int  main (int argc, char *argv[])
    {
        char  *message ;
        TcpEndpoint  client, server ;
        LfnStream  stream ;

        tcpListen (argv[1], 99, &server) ;	-- Create listening endpoint.

        for ( ; ; ) {				-- Answer next client.
            tcpAnswer (server, -1.0, &client) ;
            lfnCreate (client, NULL, &stream) ;
            for ( ; ; ) {			-- Service connected client.
                if (lfnGetLine (stream, -1.0, &message))  break ;
                printf ("Message: %s\n", message) ;
            }
            lfnDestroy (stream) ;		-- Lost client.
        }

    }

The server's name is specified as the first argument on the command line (i.e., argv[1]). If a client connection is broken, the server loops back to wait for another client.

A simple client process that reads its user's input and forwards it to the server process would look as follows:

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

    int  main (int argc, char *argv[])
    {
        char  buffer[128] ;
        TcpEndpoint  connection ;
        LfnStream  stream ;
						-- Call server.
        tcpCall (argv[1], -1.0, -1.0, &connection) ;
        lfnCreate (connection, NULL, &stream) ;
        for ( ; ; ) {				-- Forward input to server.
            if (gets (buffer) == NULL)  break ;
            lfnPutLine (stream, -1.0, "%s\n", buffer) ;
        }
        lfnDestroy (stream) ;			-- Lost user!

    }

In event-driven applications (e.g., those based on the X Toolkit or the IOX dispatcher), the socket connection underlying a line-feed-terminated stream, returned by lfnFd(), can be monitored for input by your event dispatcher. Because input is buffered, the input callback must repeatedly call lfnGetLine() or lfnRead() while lfnIsReadable() is true.


Public Procedures

lfnCreate() - creates a LF-terminated network stream.
lfnDestroy() - deletes a LF-terminated network stream.
lfnFd() - returns an LFN stream's socket number.
lfnGetLine() - reads a line of input from an LFN stream.
lfnIsReadable() - checks if input is waiting to be read from a stream.
lfnIsUp() - checks if an LFN stream is up.
lfnIsWriteable() - checks if data can be written to a stream.
lfnPutLine() - writes a line of output to an LFN stream.
lfnRead() - reads unformatted data from an LFN stream.
lfnWrite() - writes unformatted data to an LFN stream.

Source Files

lfn_util.c
lfn_util.h

Alex Measday  /  E-mail