Network programming @ Noppa | @ wiki - Updated: — Jussi Laakkonen 2012/09/04 11:39


Updates:

  • 20.8.2012: Initial draft.
  • 21.8.2012: More details to delay measurements. Added more hints.
  • 21.8.2012: Added clarification about how to start the counting on server.
  • 21.8.2012: Added a sequence diagram.
  • 28.8.2012: More clear packet length description
  • 30.8.2012: Corrected message types in packets. Added few more hints.
  • 04.9.2012: Requirement clarification: Only IPv4 is required but with the help of TCP example code IPv6 support can be implemented.

Assignment1:

First assignment is to implement a radio type message delivery system for interested listeners. The system uses TCP-protocol and supports at least one user (minimum requirement). One has to implement a client and a server. Since TCP is connection oriented protocol a connection between client and a server must be established. Server listens for connections in a port and when client connects to this port a new connection is established. The server delivers messages to client according to given specifications.

Description

Client needs to establish a connection to server (use connect()-function) and it is required to maintain the connection properly (detect if connection to server is lost and close connection properly when application is about to quit). First the client registers with the given name (limit the length to 30 characters) to the server and listens for messages. Client must request this name from the user. When client is quitting it must send a notification to the server. After this the server has to be ready for serving the next connecting client without the need for restart.

Server must be able to handle one client at a time and detect if client closes connection or connection is lost because of errors in the network for instance. Server sends clock string (date and time) to each connected client every 10 seconds. A news item header (like RSS header) is sent every 60 seconds. When news item header is sent the clock string does not need to be sent. The news items can be hardcoded (minimum requirement) or can be read from file (OPTIONAL), at least 5 items are required. Recycle, do not repeat!

Server starts counting from the point when a client has connected (with multiclient implementation start counting from the first one).

When the client receives the message it prints the content to the user. Client also shows how much time has been spent since the last advert in seconds and calculates the average time difference between received messages. The average delay is sent to server in quit message and printed to stdout when client application is terminated.

OPTIONAL: measure and show the time in seconds and in milliseconds and calculate the variance between messages. Store the variance and send it instead of average delay between messages.

Remember to handle messages correctly and use network order for numbers. Note also that when TCP is used the messages might not be read as a whole as in UDP, e.g. 40 bytes are sent by client and the server receives sent message as separate bytes.

Requirements

Server must not crash if it receives something else than described in the protocol. Same applies for client. Client must inform the user if server is not found or connection is refused. Client must not crash because of the invalid input parameters given by user.

Split the code into multiple files based on responsibilities. Think about re-usability of your code.

Application requirements:

  • Only IPv4 implementation required in the first one. IPv6 can be established by using the TCP example code.
    • When using IPv6 link local addresses add the interface to the end of the address: e.g. fe80::7a2b:cbff:fea5:68e0%eth0
  • Use TCP-protocol for data transfer.
  • Sending and receiving messages over TCP
  • Numbers in network order, clock string as a character string.
  • Implement client and server in one binary
    • Client:
      • Start:
        ./tcpradioclientserver -h <server address/hostname> -p <port number> -n <name>
        • <server address/hostname> - address or hostname of the server
        • <port number> - port number of the server
        • <name> - name to use for registering to server
        • User should be able to enter either the IP address or the hostname of the server.
      • Commands:
        • /quit - Disconnect
    • Server:
      • Start:
        ./tcpradioclientserver -l <listening port number>
      • Can handle at least one user, OPTIONAL: Support any number/fixed amount of users (e.g. 10).
      • Server must be iterative (no threads or fork() allowed)
      • Commandline commands (OPTIONAL):
        • /list - List connected clients
        • /stop - Stop accepting new clients (when set, all connecting clients should receive error message)
        • /start - Start accepting new clients
        • /quit - Shutdown the server, close client connections properly

Protocol

Use following message structure for actions specified.

Message length Message type Message content
16 bit int 8 bit int n bytes (length - header)

Protocol packets and data types from client to server:

Type 0: register as new user

16 bit int 8 bit int n characters
packet total length 0 username

Register message to server. Server does not need to respond this.


Type 1: Disconnect and send the avg. delay

16 bit int 8 bit int n characters
packet total length 1 avg .delay

Message for informing the server that client is quitting. Client reports the average variation that was calculated, server prints this value and when quitting, gives the total average of all variations. Send the avg. variation as character string.

Protocol packets and data types from server to client:

Type 2 - clock string message, packet contains:

16 bit int 8 bit int n characters
packet total length 2 clock string

Contains clock string as character string.


Type 3 - news item message, packet contains:

16 bit int 8 bit int n characters
packet total length 3 news item

Contains a news item.


Type 4 - error message, packet contains:

16 bit int 8 bit int n characters
packet total length 4 message

Contains error message, sent when server cannot handle more connections or message type is not recognized. Can be used also for notifications when server is not accepting more connections (OPTIONAL).

Sequence diagram

Here is a sequence diagram of the protocol behavior with one client support.

Additional notes and hints

  • Remember the terminating NULL in character strings!
  • Remember that TCP is stream oriented protocol so the length of the message is required to be known by the receiver.
  • Send numbers (length and type) in network byte-order
  • Establish new connections to clients by using accept() -function.
  • Use select() to determine which socket has data (remember that standard input comes via socket!).
  • HINTS: socket() bind() listen() accept() connect() select() fgets() send() recv() getaddrinfo() freeaddrinfo() getnameinfo() getifaddrs() freeifaddrs() time() difftime() gettimeofday() clock_gettime() htons() ntohs()

Questions

  • What are the limitations for messages to be sent to the server?
  • Is there a maximum length for packets to be sent according to protocol? What about maximum length of a TCP packet?

Network programming @ Noppa | @ wiki