Task for week 47

This time the task is a bit different. Here you have to implement a small game of peer-to-peer tic-tac-toe using the SCTP example code and knowledge from previous exercise code in order to get more familiar with SCTP.

Game description

The game is played between 2 players using peer-to-peer connection without a server. The game happens in 3×3 grid where indexes are from 0 to 2. Player has to give 2 coordinates (e.g. '0,1') for his/her marker. Position 0,0 is the bottom left corner.

The client started as server (client1) awaits for connections in given port and when a peer (client2) connects to server with correct PPID and introduction message (HELLO) the server (client1) replies (HREPLY) and gives 'x' for the peer player (client2).

When the peer has connected the server (client1) sends a turn indication message (TURN) to the peer (client2) and the player responds to it by placing a mark at available position (client sends a POSITION message which contains the position for the marker). If the position is not used and position is valid a GRID message containing the current state of game is sent. After this the client acting as server can place a mark to GRID. After the mark has been set the GRID is sent to peer, followed by a TURN message indicating turn change.

After the grid is full or either of the players gets 3 of his/her markers in a row (horizontal, vertical, or diagonal) the client acting as the server reports ending of the game to peer (WINNER) and disconnects. After this both clients quit.

Use three streams between connected clients. One stream is for game controls, second one is for error messages and third one is for short user written messages between peers (limit these to 20 characters).

Use notifications from connection changes to detect if the route between peers is disrupted. You can test this with computers and using the packet destroyer in 6218 (by unplugging the cord to the small blue Linksys router).

Try to minimize the bandwidth usage (remember how PPIDs can be utilized with SCTP).

Add own files for client if necessary. Add client specific source files into SOURCES_CLI variable in the provided Makefile. Common source files can be added into SOURCES_GEN variable in the Makefile.


  • Send the numbers in network order.
  • Client:
    • Start as server:
      ./sctpc -s -p <server listening port>
    • Start as client:
      ./sctpc -c -h <server IPv4/IPv6 address> -p <server listening port>
    • Commands:
      • /join - join to specified server
      • /grid - show latest grid
      • /place x,y - place own mark into x,y position, where x = x coordinate, y = y coordinate
      • /chat <message> - send a short message to peer
      • /quit - shut down connection to server and quit the client

Protocol messages


unsigned 8bit int

Hello message sent by client to server as first message.


unsigned 8bit int 1 char
0x02 players mark as character

Reply message to HELLO, sent by server /client1) to peer (client2) when accepted. Contains the playing character for the player ('x' or 'o')


unsigned 8bit int

Turn indication message sent to peer.


unsigned 8bit int unsigned 8bit int unsigned 8bit int
0x04 x coordinate y coordinate

Player reply to TURN message, this message tells that in which position player wishes to put his/her mark on the grid. Sent to peer.


unsigned 8bit int char char char char char char char char char
0x05 mark at 0,0 mark at 0,1 mark at 0,2 mark at 1,0 mark at 1,1 mark at 1,2 mark at 2,0 mark at 2,1 mark at 2,2


unsigned 8bit int char char char char char char char char char
0x05 x o - - x - - x o


  • 'x' is mark of first player
  • 'o' is mark of second player
  • '-' is empty place in grid



The game grid message sent to peer after each new mark placement. Contains markers placed by players.


unsigned 8bit int 1 char
0x06 players mark as character

Message that indicates the winner of the game, includes the mark of the winning player.


unsigned 8bit int 1-20 chars
0x07 short message

A short message sent by either of the peers.


unsigned 8bit int unsigned 16bit int
0x08 error type
  • Error types
    • 1 = game full
    • 2 = invalid position

Error message, sent by server when game was full or given position was invalid (invalid coordinates or position taken).


Here is one solution from previous years, thanks to Lauri Haara (identifications removed from sources), that follows almost the same protocol.



Run server:

./sctps <port>

Run client:

./sctpc <host> <port>

Task for next week (week 48): RTT, latencies and jitter