Network programming @ Noppa | @ wiki - Updated: — Jussi Laakkonen 2012/09/04 16:53


Updates:

04.09.2012: Initial version.
04.09.2012: Added protocol.

Assignment5: Read, learn and implement "Connect four" game

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 (“Connect four”, also known as “Four in a row”) 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 Connect four
    • 3 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 following the instructions (commands work, generic features fulfilled, returned as requested etc.)

Reading material about SCTP

Questions

  1. What are the differences between SCTP one-to-one and one-to-many styles? Why there are 2 “different” styles?
  2. Explain the difference between a connection (TCP) and an association (SCTP).
  3. Why SCTP can be regarded as more secure as TCP?
  4. What benefits does the multihoming feature of SCTP give? Give at least 3 examples.
  5. Explain Head-of-line blocking. Why it is a problem with TCP? How SCTP handles this or how this affects SCTP? Give an example application/application layer protocol that could benefit from SCTP where head-of-line blocking can occur.

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 (with ifaddrs and combine parameters). 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

Compile:

  • make to compile without tests
  • make tests to compile test server and client
  • make debug make with -g flag

Start the test server with

# Note the difference with these following approaches
# ifaddrs  - use getifadds() to get all addresses of each networking interface
# addrinfo - use getaddrinfo() to get bindable addresses
# combine  - use both getifaddrs() and getaddrinfo() to gather all addresses.
./testsctps <port> <ifaddrs|addrinfo|combine>

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.

Tasks

Your programming task now is to implement the “Connect Four” game over SCTP. 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. You have to implement client and server. Server can serve one game at a time, after the game has started other connecting clients are rejected with an error message.

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. If a new player connects while, e.g., one player has not reported as ready the state of others is not needed to be reset. When all are ready the server reports that the game is starting to all players and includes the player count into this message.

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 turns for the players are selected by the server and should be random. This should be done at game start and the order is fixed onwards from that point. 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.

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.

Note that there is no quit messages. Server and client must then use the SCTP notifications to detect disconnections. If a client disconnects the server can continue the game with remaining clients and to skip the turn of the disconnected client. But when the second last player leaves the game must end and the remaining client is declared as winner.

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.

Requirements

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

Protocol messages

Use following values with SCTP:

  • Protocol Payload Identifier: 2155905160

Server and client must not accept messages with some other Protocol Payload Identifier.

0 Join

16 bit integer
0

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
2

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
3 column count row 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
4

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 column \0 4-5 columns \0 Highest column

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).

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:

xxxxxxx
xxxxxxx
xxxx2xx
xxx121x
xxx2121
1xx2112

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.

1000 Error

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

Error 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.

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 Debian/Ubuntu to develop applications on top of SCTP: libsctp-dev, libsctp1.

Network programming @ Noppa | @ wiki