meta data for this page

This is an old revision of the document!

Network programming @ Noppa | @ wiki - Updated: — Jussi Laakkonen 2011/09/21 12:05


21.9.2011: Updated the demonstration code & further specification
21.9.2011: Memory leak point → MSC creation point

Assignment3: Read, learn and implement tic-tac-toe

The third assignment is about Stream Control Transfer Protocol (SCTP). In this exercise the approach is different, you will have to:

  • Read the material
  • UNDERSTAND the material
  • Answer the questions
  • Look at the example code
  • UNDERSTAND the example code
  • Implement required parts (tic-tac-toe) and send the result as all other assignments
    • This time it is recommended to use the example code as basis for assignment
    • Remember to rename the folder!

Grading of this assignment: total points == 11:

  • 1 point for each question (5 total)
    • Answers do not have be long (no essays!) but descriptive.
    • Return the question answers as separate answers.txt/pdf (no other formats!)
  • 6 points for the implementation of the tic-tac-toe
    • 2 points for properly using SCTP with the help of example code
    • 2 points for following the game protocol (and the game works!)
    • 1 point for creating the MSC chart for the game protocol!
    • 1 point for following the instructions (commands work, generic features fulfilled, returned as requested etc.)

Reading material about SCTP


  • What are the major differences when comparing SCTP to UDP? How about TCP?
  • Explain 3 benefits that SCTP offers when compared to existing transport layer protocols.
  • What are the differences between SCTP one-to-one and one-to-many styles? Why there are 2 “different” styles?
  • Explain the difference between a connection (TCP) and an association (SCTP).
  • What benefits does the multihoming feature of SCTP give? Give at least 3 examples.

Example code

Example code will contain a server and a client using SCTP for communication. Server address multihoming is enabled by getting address and interface information of each running interface on the machine. Server and client support IPv4 and IPv6 addresses. Data sending and receiving is implemented in demonstrative manner as well as SCTP notification handling.

SCTP demonstration code

Updated on 21.9.2011 @ 11:25


  • make to compile without tests
  • make tests to compile test server and client
  • make debug make with -g flag
  • make bybook make with a method from a book for address structure packing
  • make bybookdebug make previous with -g flag

Start the test server with ./testsctps <port>

And the client ./testsctpc <IPv4/IPv6 address> <port>

  • When using IPv6 link local addresses add the interface to the end of the address: e.g. fe80::7a2b:cbff:fea5:68e0%eth0

Close server with Ctrl+C, client quits after the demonstrative tests quit.

The server starts listening on all interfaces that are available and running and accepts incoming data from clients. Client sends some amount of data to server twice and server echoes the data back to client.

Notification information is just printed to stdout on both server and client - reaction not implemented as this is just a demonstration.


Your task in this third assignment is to see what the code is doing and understand it. In addition you are required to implement a simple tic-tac-toe game according to protocol described below.

Game description

The game is played between 2 players, server does not need to support more than one game at a time. 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 top left corner.

Server awaits for connections in given port and when a client connects to server with correct ppid and introduction message (HELLO) server replies (HREPLY) and gives 'x' for the first player, response to second player is similar but 'o' is assigned to that player.

When 2nd player has connected server sends a turn indication message (TURN) to the first player and the player responds to it with a POSITION message which contains the position for the marker. If the position is not used and position is valid server replies to both players with GRID message containing the current state of game . After this server gives the turn to the other player.

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

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


  • Send the numbers in network order.
  • Client:
    • Start: <code>./sctpc <server IPv4/IPv6 address> <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
      • /quit - shut down connection to server and quit the client
  • Server:
    • Start: <code>./sctps <server listening port>
    • Runs until stopped with Ctrl+C (implemented into example code)
    • Serves supports 2 clients at a time per game. Runs only one game at a time.
    • Iterative, no threads or fork().

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 to client when client was accepted. Contains the playing character for the player ('x' or 'o')


unsigned 8bit int

Turn indication message sent by server to the client whose turn is to place a mark into the grid.


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 by client to the server.


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 o


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

The game grid message sent by server to clients 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 unsigned 16bit int
0x07 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).

Additional notes and hints

  • Hints socket() getaddrinfo() getifaddrs() setsockopt() connect() sctp_sendmsg() sctp_recvmsg() sendto() recvfrom() listen() select() inet_ntop() inet_pton() sctp_bindx() sctp_connectx() sctp_getpaddrs() sctp_getladdrs() shutdown()
  • Note that with SCTP connect() function is not needed in one-to-many style.
  • Required packages for Ubuntu to develop applications on top of SCTP: libsctp-dev, libsctp1.

Network programming @ Noppa | @ wiki