Introduction
UDPTCP,“”,,,
,c++
Requirement
CSE422 Computer Networks Fall 2016
Laboratory 1: Introduction to Socket Programming
Due: 23:59 Monday, Oct 3
1 Goal
Gain experience with socket programming by implementing a simple “hacker” game using
both UDP and TCP sockets. In this game, one participant tries to guess a four-digit password
known to the other participant, receiving feedback on each guess as described below.
2 Overview
In this lab, you will implement a networking version of the hacking mini game, which will
comprise of two C++ programs, one client and one server. This lab will help you to gain
experience with socket programming using Berkeley socket interface. In order to focus more
on the details of socket programming part, the game and most of the command parsing are
provided as a skeleton code.
This lab is worth for 60 points. This lab is due no later than 23:59 Monday, Oct 3. No late
submission will be accepted.
3 Specification
In this lab, you are required to implement a client and a server program for the ”hacker”
game. The server program and client program will interact by exchanging messages to
simulate the game play. A simple message/packet format is provided in the file: message.h.
The server and client program will handshake through TCP and the gaming interaction will
use UDP.
3.1 “Hacker” Game
The game will display a list of possible passwords (formatted in a way to mimic what is seen
in the Fallout game series). The user will then enter a guess for which one they believe the
password to be. The game will report back the “likeness” of the guess (how many correct
letters are in the correct position).
1
For example, suppose the password is ”FULL” and the displayed text, containing all of the
possible passwords, is:
ROBCO INDUSTRIES (TM) TERMALINK PROTOCOL
ENTER PASSWORD NOW
The default number of attempts is set to 4.
A demo of the ”hacking” game play can be found at: [Demo Play]
-In the demo, click the power button in the right lower corner then click on the phase that
you would like to guess as the password.
3.2 The Server Program
Example invocation: ./server
The server program will create a TCP socket and will then wait for an incoming TCP
connection. This TCP socket’s port will be assigned by the operating system and will be
printed to the console. We assume that the client knows this TCP port number, because the
client is started after the server. When the server receives an incoming TCP connection, it
creates a UDP socket, whose port is also assigned by the OS. Via the TCP socket, the server
program returns a packet to the client containing the UDP port number. This can be done
using the UDP Cntr MSG type found in message.h.
Once the UDP connection is established, the server program initializes a variable of type
HackerGame. The initialization of this variable generates the server password, a list of
possible passwords, a string to act as the display board for the game, and the number
of attempts left. These are accessible through functions in the HackerGame class. We
recommend that you look over HackerGame.h. Guesses from the client can be evaluated by
2
using the TestEntry() function. The server keeps receiving and testing guesses from the
client until either the number of attempts has been exhausted or the password is guessed
correctly. For each guess, the server sends a packet containing the “Likeness” of the guess
to the client using the UDP socket. Once the game is over, either by guessing the password
correctly or running out of attempts, the server must notify the client using the UDP socket,
after which the server closes both the TCP and UDP sockets.
3.3 The Client Program
Example invocation: ./client -h lenny.cse.msu.edu -p 48192
The client program is required to accept the following arguments.
• -h is the server address (domain name).
• -p is the TCP port number on which the server listens for an incoming connection.
The client resolves the server address by using gethostbyname() and connects to the server
over TCP. The client then receives a packet from the server containing the UDP port number.
Using the UDP port number, the client sends a confirmation message to the server, letting
it know that it can send the first game message, using the UDP socket. The client receives a
packet containing the game display text and prints it to console. The client then takes input
from the user for the a guess at the password and sends the guess to the server program via
the UDP socket. For each guess the client should receive a response containing the likeness
of the guess and how many attempts are left. These are to be printed to console.
The client should monitor the content of each game message for ending conditions. Specif-
ically, if AttemptsLeft = 0 or IsGameWon = true, then the client should print to console
either “Terminal Locked Out!” or”Access Granted!”, respectively. The client then closes
both the TCP and UDP sockets.
3.4 Packet Format: message.h
There are two different types of packets for this lab; both are defined in message.h.
1. UDP Cntr MSG: which contains only one field: int port
2. Game MSG: which contains:
(a) char Display[1000]
(b) int AttemptsLeft
(c) bool IsGameWon
(d) int Likeness
3
(e) char Entry[10]
String type variables cannot be used in these packets due to how they utilize memory. For
information to be correctly sent in packets, the packet most know the starting address of the
variable and how large it is. Hence why we use arrays of chars.
You may want to still use strings in your client and server programs, however. Following are
two examples of copying content between the two variable types.
Copying char array to a string:
char Word1[10] = “test”;
string Word2(Word1); //Word2 now contains “test”
Copying a string to a char array:
string Word2 = “test”;
char Word1[10];
strcpy(Word1, Word2.c str()); // Word1 now contains “test”
4 Deliverables
You will submit your lab using the web based handin utility. Don’t use the command-line
based handin facility. Please submit all files in your project directory. If you start your lab
with the skeleton code, submit all files, even if the file is not modified.
This lab is due no later than 23:59 Monday, Oct 3. No late submission will be accepted.
The compilation must be done using a makefile. The code should compile on multiple
machines (can use lenny, carl, bart, etc). Please do not do your debugging on the servers
(arctic or black). You will not be awarded any points if your submission does not compile
using a makefile. A sample makefile will be provided. It will only have to be changed if you
add more files to your solution. Please test your programs before handing them in.
A README file is required including your name, PID, and machines that you tested on.
You will run your server and client programs and paste the output in your README file.
A sample README file is also included in the skeleton code. You are also encouraged to
include explanations about parts of your code that may not work in the README file.
5 Example
Follows is an example of output from the client and server. Your output may differ.
1. Invoke the server
4
Server:
>./server
Creating TCP Socket…
TCP socket has port number: 53401
2. Invoke the client. The client connects to the server using the TCP connection infor-
mation that you provide via the command line.
Client:
>./client -h lenny.cse.msu.edu -p 53401
Client is running…
remote host: lenny.cse.msu.edu, remote TCP port: 53401
Connected to Server!
3. Server accepts the TCP connection from the client and sends the UDP port information
using the TCP connection.
Server:
A new client is connected to the server!
Creating UDP Socket…
UDP socket has port number: 32471
Sending UDP port number to client using TCP connection…
4. Client uses the message from the TCP connection to establish an UDP port.
Client:
Reading TCP socket for UDP set up info…
UDP port on Server: 32471
Server IP: 35.9.23.42
Establishing UDP Connection…
5. Once the UDP connection is established, the server starts the game and accepts guesses
from the client until either the password is guessed or the number of attempts is
exhausted. Optionally the server can display the password for debugging purposes.
Server:
Password: FOUR
Starting game…
Entry: term
Likeness: 0
Entry: ball
Likeness: 0
Entry: year
Likeness: 1
Entry: four
Likeness: 4
Access Granted!
Closing TCP and UDP sockets…
5
Client:
Receiving first game message…
ROBCO INDUSTRIES (TM) TERMALINK PROTOCOL
Entry: term
Likeness: 0
Attempts left: 3
Entry: ball
Likeness: 0
Attempts left: 2
Entry: year
Likeness: 1
Attempts left: 1
Entry: four
Access Granted!
Closing TCP and UDP sockets…
6 Grading
You will not be awarded any points if your submission does not compile.
Please feel free to mail TA Glen Simon simongle AT msu.edu for questions or clarifications.