Professional Documents
Culture Documents
Network Programming
Network Programming
Contents
1. Game Scenario ....................................................................................................................... 3
1.1 Background ....................................................................................................................... 3
1.2 Implementation ................................................................................................................. 3
2. Functions of the game ............................................................................................................ 5
2.1. Main function ...................................................................................................................... 5
2.2. int roll_dice(int *dice) function .......................................................................................... 6
2.3. void print_dice function ...................................................................................................... 7
3. Functions of the chat server ................................................................................................... 7
4. Client ...................................................................................................................................... 8
1. Game Scenario
1.1 Background
The game “Funny Dice rolling” is simple 3 round game mathematical game, which give 5
chances to each player. There are 3 dices and before the first attempt of each player computer
will roll the dices and display the sum of 3 upside numbers of 3 dices. Players have to predict
whether the sum of next upside numbers of 3 dices will lower, greater or the same as the sum
of earlier attempt. If the answer is correct points will be added to player. Each player will give
5 chances to predict the random dice value is lower, greater or the same as the earlier value.
The player who scores the most points after 3 attempts will win the game.
1.2 Implementation
The game “Funny Dice rolling” is a multiplayer game and therefore it is implemented as a
concurrent gaming server. The communication layer protocol used for this scenario is TCP.
Since TCP is a reliable protocol it checks for errors and use acknowledgements, in this game,
reliability is more important than delay and latency because this is a 5 round game and the
players’ answer of current round is based on previous round results therefore we use TCP as
transport layer protocol rather than choosing connectionless protocols like UDP.
There is one game server which include chat room and game. The port no 55555 is used for
game and port no 66666 is used for chat room.
Figure 1.1.1: To begin the game 3 clients should connect to game by providing port no of the game server
When the listening port of server accepts client’s connection it asks name from client and the
server sends message to client that they connected and provide their socket information.
To start the game 3 clients should connected to the game server. After the 3 clients connected
game will start.
After each round player will notify their answer is correct or not.
After 5 rounds each player will notify how many times, they give correct and wrong answer.
Figure 1. 2.5: Notification after 5 rounds
The player who get the highest successful guesses will be inform as winner.
While the 3 clients play the game, new clients that try to connect the server are introduced to
the chat server.
Clients inside the game can not chat with each other but clients connect after first 3 clients will
redirected to chat room and they can chat with each other until game ends,
Once the 1st three clients have finished their match, they are automatically introduced to the
chat server.
In the main function we give five chances to player using for loop and get the responses from
users and check them whether it is greater, equal or lower with previously generated sum which
is stored in old sum variable
In this function we get a random value for the dice values and store them in sum
In this function we print the random values generated in the previous function as total of three
dices
Figure 4.1
Get the ip address. port no and use coonect function to connect to server.
Figure 4.2
To get information about server socket here we use getsockname function and to get
information about its own socket we use getpeername function
Figure 4.3
Figure 4.4
Figure 4. 5