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


22.8.2012: Initial version.
22.8.2012: Text-file based database set as a requirement.
22.8.2012: Added sequence diagram.
10.9.2012: Added clarification for error message sending.

Assignment3: Aphorism exchange system

Third assignment is to implement an aphorism exchange system according to protocol described below. The system must be implemented with UDP. One has to implement a client and a server. Server listens in certain port (remember that you have no rights to use system ports between 1 – 1024 on class 6325 machines) and client sends UDP datagrams into that particular port using the IP address or the hostname of the server and server responds based on the message contents.


Client can either add aphorisms to the database of the server or retrieve them from the server's database. The client sends a message to server, waits for a response from the server, informs the user about response and then quits. Client should get the command from a command line parameter. When adding an aphorism the client must request the aphorism from the user (via standard input).

Server can store up to 10 aphorisms and serves one request at a time. It is required to store the IP-address of the sender with the exact date and time along the sent aphorism (feel free to select the format). Save this information to a text file when server is quitting and load it at startup. When server is run for the first time (no database exists yet) it will start with an empty database and the text file is created. OPTIONAL update the content of the text file every time new aphorism is added. If the database is full server should response with appropriate message. If the command is not recognized server should return usage instructions in a message.

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. Client must not crash because of the invalid input given by user.

IPv4 and IPv6 must be supported. Use PF_INET6 type socket and allow data from IPv4 mapped addresses although there exists some risks with this. For more details see Linux IPv6 HOWTO IPv4 Mapped Addresses and/or read chapter 4.3 in IPv6 Network Programming book.


  • Use UDP-protocol for data transfer (remember that UDP is connectionless protocol).
  • Sending and receiving messages over UDP.
  • Do not use connect() -function
  • IPv4 & IPv6 support.
  • Implement client and server.
    • Client:
      • Start:
        ./aphorismclient -h <server address> -p <port number> -c <command>
        • <server address> - The IP address or hostname of the server
        • <port number> - Listening port of the server
        • <command> - ADD or GET
      • When ADD command is used the client waits for user to input the aphorism.
      • Quit after receiving a response from server.
    • Server:
      • Start:
        ./aphorismserver -p <listening port>
        • <listening port> - Port that server listens for incoming datagrams
      • Can store 10 aphorisms.
        • Data to save: aphorism, who sent (IP), when (date & time)
        • Save aphorisms into a text-file when the server is terminated.
        • Read aphorisms from a text-file into database when starting the server.
      • Runs until is stopped with Ctrl-C or quit command from command line.
      • Server must serve one request at a time. After serving one request process the next one.
      • Iterative, no threads or fork().


Protocol packets and data types from client to server

Add an aphorism

3 characters 10 to 509 characters
ADD aphorism

For adding an aphorism to server. If the adding of an aphorism succeeded (database not full) server responds with AOK -message, otherwise with ERR. Minimum length for an aphorism is 10 characters. If the aphorism is less than this server responds with ERR. Example: ADDThat which does not kill us makes us stronger.\0

Get an aphorism

3 characters

For requesting an aphorism from server. Server responds with an aphorism (APH message). Or if there is no aphorisms an ERR message is sent.

Protocol packets and data types from server to client

AOK (Aphorism OK) message

3 characters

Sent by server when adding of an aphorism was a success.

Aphorism reply message

3 characters 10 to 509 characters
APH aphorism

Aphorism sent by server. Example: APHBelieve nothing you hear, and only half of what you see.\0

Error message

3 characters 0 to 509 characters
ERR Error message

Error message, sent when database is empty (response to GET) or full (response to ADD). Sent also when the aphorism is too long or command is not recognized, in this case usage instructions are sent to client (separate commands to multiple lines). Example:ERRDatabase is full.\0

Sequence diagram

Additional notes and hints

  • Server and client can not accept or send messages longer than 512 characters. Remember the terminating NULL in character strings!
  • Hints: sendto(), recvfrom(), socket(), bind(), getaddrinfo(), getnameinfo()
  • OPTIONAL (remember to mention this in readme!):
    • Use dynamic list for aphorisms. Define a adjustable limit for maximum number of aphorisms (hardcoded, using #define or with additional startup parameter for the server)


  • How to detect when connection is lost / application has crashed at the other end with UDP?
  • What is the maximum size for UDP datagram in class 6325?

Network programming @ Noppa | @ wiki