meta data for this page



03.10.2013 : New optional feature and more details to description.
11.10.2013 : In packet 6 description column → row (as it should be).
30.09.2014 : More clear optional feature specification.

Assignment 1 (DEADLINE 11.11.14)

The first assignment is to implement the “Connect Four” game over UDP using TCP as chat channel. See Wikipedia article about the game or try it yourself. The game is also available for Linux, in Debian/Ubuntu the package is named as gnect. Only difference here is that your game must support 2-4 players.

In this assignment you have to implement a client and a server. You can use the example codes as reference material or as a basis for your game. All communication from the clients is going to the server (UDP and TCP), no peer-to-peer communication between the clients is required.

The server can serve only one game at a time. After the game has started other connecting clients are rejected with an error message.

Each client first sends a UDP message (Join) to the server. When the server has accepted the client (and delivered the client id) a TCP connection can be established to the server.

<OPTIONAL 1> Use the reported unique client id to connect a TCP connection to a player at the server. You'll need 2 additional TCP packets for this - devise them by yourself. Also, add the client id to every chat message the server forwards, e.g. Haddock has id 1 → [1]Haddock. </OPTIONAL 1>

After joining to the game each client has to report to the server that the player is ready. Server will start the game when all connected players are ready OR 3 seconds after when the 4th player joins. When all players are ready (or other conditions are met) the server reports to all clients that the game is starting and includes the player count into this message. Each client proceeds to game state and awaits for turn from the server.

The game area is 7×6 for 2 player game and 9×7 for 3 and 4 player game. In this game every player has to put a mark during his/her turn. The turn order for the players is selected by the server and should be random but consistent within that game. The server delegates the turns, i.e. tells the clients when it can place a mark into the game area by selecting a column. If the player attempts to put a mark into full column server must notify this and the client must select another column.

<OPTIONAL 2> Each turn message contains also the number of the turn so clients and server can keep track of the game rounds. Add this number field into packets 4 & 5. The client uses the same turn number when placing the mark, if the server receives a turn with invalid number, the message is discarded. The server resends the turn message after every invalid turn to the client whose turn it is. After 3 invalid turn numbers from the same client the server removes that client from the game and gives the turn to the next in line. </OPTIONAL 2>

During a game, you can detect crashed/dropped players by setting a 30s timeout for actions - if the player does nothing in 30s the player is kicked out from the server and other players continue the game. Unless there is only one player left, who in this situation naturally wins the game by forfeit.

<OPTIONAL 3> Implement a ping-like system for server and client over UDP. Server sends a special PING packet to client every second and if there is no reply within one second the client is considered as crashed and server removes that player from the game. </OPTIONAL 3>

While in the game, either waiting for it to start or playing a game, each player can send chat messages to the server. Server forwards these messages to other clients which have established a TCP connection.

The game can end in 2 occasions, (1) a player has 4 in a row horizontally, vertically or diagonally or (2) the game area is full (a tie). Server reports this to every client and after this it, and the clients reset themselves and disconnect.

When the second last player leaves, or is removed from the game the remaining client is declared as winner.


  • Send the numbers in network order.
  • Client:
    • Start:
      ./c4client -h <server IPv4/IPv6 address> -u <server UDP listening port> -t <server TCP listening port>
    • Commands:
      • /join - join to specified server
      • /ready - tell server that the player is ready
      • /area - show latest area
      • /place <column> - place own mark into <column> position, where <column> = 1…7 (or 1…9 in 3-4 player game)
      • /chat <message> - chat message
      • /quit - shut down connection to server and quit the client
  • Server:
    • Start:
      ./c4server -u <server UDP listening port> -t <server TCP listening port>
    • Runs until stopped with Ctrl+C.
    • Serves supports 2-4 clients at a time per game. Runs only one game at a time.
    • Iterative, no threads or fork().

Protocol messages


0 Join

16 bit integer

The join message sent by client after establishing the connection.

1 OK

16 bit integer 8 bit integer 8 bit integer
1 player count assigned player id

Reply from the server to Join message. Gives details about player amount including the player and returns the assigned player id (1 to 4).

2 Ready

16 bit integer

Ready message sent by the client to the server. Sending this message requires user interaction and tells the server that this client is ready to start the game.

3 Start

16 bit integer 8 bit integer 8 bit integer 8 bit integer
3 column count row count player count

Game starting message sent by the server to all accepted clients. This tells the game area size to clients. Expected value pairs are column=7 and row=6, or column=9 and row=7. After this clients must expect either a Turn message or Area message.

4 Turn

16 bit integer

Turn message sent by the server to a client whose turn it is to place a mark.

5 Column

16 bit integer 8 bit integer
5 selected column

Client reply to Turn message, sent to server only. This tells the server that player wants to put the mark on selected column. Server answers to this by Ok message (player count = 0) when placement was ok or an Error message either telling that column is full (4) or it isn't player's turn yet (3).

6 Area

16 bit integer 7 or 9 characters 1 char 1 char 7 or 9 characters
6 Lowest row \0 4-5 rows \0 Highest row

Updated game area, sent by server to every playing client. Each client must update the view after receiving this (one way: see man 3 system & man clear). Or you can just use ncurses (example)

E.g. in 2 player game:

0x0006 1xx2112 \0 xxx2121 \0 xxx121x \0 xxxx2xx \0 xxxxxxx \0 xxxxxxx \0

where a number 1-4 (as character) represents the player whose mark it is and 'x' is an empty slot. This would represent a game situation:


7 Winner

16 bit integer 8 bit integer
7 winner id

Winner has been detected and server informs players about the winners id. The winner id can be 0, meaning that the game area was filled and the game results in tie.

8 Quit

16 bit integer

Client wants to quit. Server does not need to notify other clients in any way but if TCP chat is implemented the server sends a message, where nickname=“Player <id>”, message=“Has left the game”.

1000 Error/Notify

16 bit integer 8 bit integer n characters
1000 Error code Error message

Error/Notify codes and messages:

  1. Game running. New clients are not allowed.
  2. Game full. New clients are not allowed.
  3. Not your turn. Placement discarded.
  4. Column full. Select another.
  5. Server is shutting down now.


These messages are sent over TCP connection.

Chat message from client to server

16 bit integer 32 bit integer n characters 1 byte n characters
10 Total length   Nickname \0 Chat message

Chat message the client sends to the server. Server forwards this message to all clients. After a client receives such message it is to be printed to the chat area of the client.

Chat message from server to clients

16 bit integer 32 bit integer n characters 1 byte n characters
11 Total length   Nickname \0 Chat message

Forwarded chat message, sent by server only.


The total amount of points from this is 15.

Working game over UDP: 10 points.

  • Two player game only: 7 points max.

Working chat over TCP including leaving notifications sent by server: 5 points.


Std. num GAME (incl. UDP) TCP EXT Comment
0319790 6 - - No readme, server gets stuck if something garbage is sent by another client, UDP only, somewhat works, 2pl only, mixed use of signed and unsigned, return values of sending and receiving is not checked.
0342385 5.5 - - UDP only, old dropped clients not detected, client segfaults at quit → server segfaults too, client state not properly tracked (ready accepted while playing, quit sometimes does not work), protocol not implemented according to specification (e.g. packet 5 in two parts)
0342408 10 4.5 2 Server does not check input parameters, TCP send/print buffers not cleared, nicknames used, timeouts ok, works ok
0342709 8.5 5 ? works only if clients are from same machine, otherwise works ok, some optionals done (?) - none was mentioned in readme.
0372074 10 5 2 All seems to be ok, ncurses ui, nicknames used
0372197 10 5 2 Nicknames used, seems to work ok
0372223 8 4 1 Segfault with hostname, player tracking is errorrous, timeout kick of wrong client?, sometimes broken pipe at connect, the code structure is not easy to maintain – probably the reason behind errors, no proper check for TCP send
0387836 9 3.5 2 weird ui but works, no proper tcp/udp send/recv checks
0388071 7 5 1 Works only if clients are from the same machine as server (otherwise no tcp connection seems to be made), resets game if someone else sends ready during game, server closes sometimes by itself? Nicknames used
0388110 10 4 2 Works ok, nicks used, timeouts ok, tcp does not verify that all is sent, unlimited amount of players with dynamic field size – nice
0434378 10 5 - Works ok, nicks used