Task on network

Οἱ ἄγγελοι (The messengers) #

The year is 480 B.C.
The Persian king Xerxes (Old Persian: 𐎧𐏁𐎹𐎠𐎾𐏁𐎠, Khshayārsha) has just invaded the Greek city-states.
This unfortunate event has caused many messengers traveling throughout Greece to begin acting as scouts.
They visit a variety of cities. Some settlements are controlled by the Greeks, while others have been taken by the Persians.

To gain an informational advantage, the citizens of Sparta (Doric Greek: Σπάρτα) decide to establish a library connected to the latest telecommunications network for messengers.

Each messenger receives a TCP client application that can connect to the library server.
The library contains a list of city-states and information about whether they are controlled by the Greeks or the Persians.
Each messenger maintains a similar list locally, updating it when new information is received from the central library.
There are 20 city-states. Each one is identified by a number in the range [1, 20].
Your task is to write a client program compatible with the library server.

The server program is provided to you in the form of an executable file. You can start it by:

./server <PORT>

An executable file of an example client implementation is also provided.

Stages #

  1. Implement connecting to the TCP server.

    • The client accepts two arguments: server address and port number.
    • Example client execution:
      ./client 127.0.0.1 8888
      
    • After starting:
      • Connect to the specified server using the TCP protocol.
      • After establishing a connection:
        • Send a 4-character message read from stdin to the server.
        • Close the connection and terminate the program.
  2. Implement stdin command handling and message sending.

    • After connecting to the server, wait for commands on stdin.
    • Support the following commands:
      • e (exit) – close the connection and terminate the program.
      • m XXX (message) – send a 4-character message (3 characters + newline) to the server, where XXX are any 3 characters.
      • t XX (travel) – randomly choose one of the two letters {g, p}, and send:
        YXX\n
        
        to the server, where Y is the randomly selected letter, and XX is a two-digit number in the range [1, 20].
        For single-digit numbers (1–9), use a leading 0.
        If the number is outside of the range, print an error and wait for another command.
      • o (owners) – print the ownership of each city (described below).
    • Keep an array that stores which side (Greek or Persian) owns which city.
    • Initialize all cities with unknown ownership.
    • After sending a message like gXX\n via the t command, mark city XX as belonging to the Greeks.
    • Likewise, for messages starting with pXX\n, mark the corresponding city as Persian.
  3. In addition to reading from stdin, also handle input from the server socket.

    • You can use epoll (or ppoll or pselect) to add both STDIN_FILENO and the server socket descriptor to the monitored set.
    • The server may send messages to the client in the exact same format as those generated by the t XX command.
    • When such a message is received from the server, treat it exactly the same way as one generated locally by the t command (i.e., update city ownership).
  4. After receiving a SIGINT signal:

    • Print the current owner of each city.
    • Close any active connections.
    • Free all resources.
    • Exit the program.

Client and server executables #