Sockets - Server and Client

  1. You can create a simple server application with the socket command:

    Syntax: socket -server procName port
    Opening a server socket has this syntax:
    procName The name of a procedure to call when a connection is established.
    port The port to accept connections on.

    The procedure that gets called when a client attaches to the server will automatically receive three arguments:

    channel The channel that's been assigned to this connection
    ipAddress The IP address that this connection originated from. This can be used for simple access control.
    port The port number assigned to this connection. This can be used to distinguish multiple connections from a single IP address.

    A server can be tested by connecting to it with the telnet or putty application.

    Write a test server that will accept a connection, send a message and immediately close the channel.


  2. A server usually has some state information that it retains: lists of clients, current status, etc.

    Modify the previous server by adding a global associative array named State with an index named count. When a connection is made the server should report how many connections have been made before closing the channel.


  3. Most servers will read data from a client, process it in some manner and reply.

    A server can't wait for one client to enter data, ignoring other clients, and it's wasteful of CPU resources to round-robin poll all the clients to see if any have input to process.

    The fileevent command makes a server event driven - it will only read data when data is available for reading, instead of polling or waiting for input.

    Syntax: fileevent channel direction script
    Register a script to be evaluated when an event occurs on a channel.
    channel The channel to watch for file events on.
    direction The direction of data flow to watch. May be readable or writeable
    script A script to invoke when the condition becomes true. (A channel has data or can be written to.)

    Modify the previous server by:

    1. Do not close the channel.
    2. Add a fileevent command to the acceptClient procedure to invoke a procedure named readData when data is available for reading.
    3. The readData procedure should echo the data that was received.


  4. Another common server paradigm is a broadcasting server that transmits a message every N seconds. This could be a used as a system monitor or as a simple "am I alive?" heartbeat monitor to confirm that a remote system is still functioning.

    Modify the code from exercise 2 to save a list of client channels and transmit a message every second.


  5. We can write a client even more easily than writing a server.

    To open a client-side connection to a server:

    Syntax: socket IP_Address port
    IP_Address The address of the system to be connected to. May be a numeric address, or a fully qualified domain name.
    port The port to connect to on the target machine.

    Write a client that will connect to the server in exercise 4, get and print 2 lines of data from the server, then close the channel.

    Put the open/read/puts lines in a loop so that they will run 5 times. You will probably need to add an after command inside the loop.


Copyright Clif Flynt 2009