Introduction
:
/* SAMPLE README */
Name: Joe Freedman
PID: 11111111
Machines Tested On: arctic, apple, blueberry
/ Comments */
Code all appears to work as described in the handout. Had no issues in completing all assigned tasks.
/* Sample Server Output */
/* Sample Client Output */
Requirement
CSE 422 Spring 2018
1
1. Project 2: Socket Programming
This project is worth 150 points (15% of the course grade) and must be completed and turned in to the handin
system before 11:59 PM on Thursday, April 26, 2018.
2. Assignment Overview
The goal of this assignment is to gain hands-on experience programming with sockets and transport protocols
(TCP and UDP). You will implement a simple game of hangman
()) using socket programming in Python or C/C++ to send and
receive messages between a client program and a server program. Hangman involves guessing letters of a
hidden word without incorrectly guessing a specified number of times (5 in this case).
3. Project Description
The project2_skeleton.zip file is provided to you. It contains:
• Project2.pdf (This document)
• server.py
• client.py
• README.txt
• game.py (Don’t modify this)
• words.txt (Don’t modify this)
Your job is to complete the skeleton code in server.py and client.py to successfully play a simple game of
hangman. (The hangman implementation is mostly done for you, so you need to mainly focus on sending
messages back and forth using TCP and UDP sockets).
The idea is the client will contact the server by making a connection using TCP. The client will then announce
itself to the server. The server responds with the UDP port the client should use for further communication.
Finally, send game messages back and forth using UDP. The client repeatedly guesses letters by sending them
to the server. The backend (server) handles the game logic by checking the guess and replacing the blanks
with correctly guessed letters. It also decrements the number of attempts left in the case of an incorrect or
already guessed letter.
Each message has 2 parts: the message type and the message text. Each message will be of the form
“ ” (notice the space character in between). The message type will dictate to
each program what to do next (this is outlined in Section 6 below). An example exchange of game messages is
shown in Figure 1 for your reference.
You should modify the README.txt with your information, the machines you tested your programs on, a list of
resources you used, additional comments, and some sample test output from your programs (both client and
server).
CSE 422 Spring 2018
2
Figure 1: Game Messages Visualization
4. Client Specifications
The client should accept two command line arguments:
1. Name of the server host
2. TCP Port to be used to contact the server process
Example: python client.py arctic.cse.msu.edu 15678
When the client is first started it should prompt the user for their name. After the user has entered their
name, the client should create the TCP connection to the server and send the hello message with the user’s
name. Once the client receives the portUDP message from the server, the client should open a UDP socket and
then prompt the user and allow them to enter various game playing commands (these are different than the
network messages). The client should support the game commands being entered in any case (upper, lower,
or mixed case). All additional communication with the server should be performed using UDP messages to the
port provided by the portUDP message.
The player (user) interacts with the client program by entering game playing commands through their
keyboard. The client should support the following game playing commands:
• start – Starts a new game.
• end – Ends the current game. If a game is not already in progress this command has no effect. Sends an
end 㹭ੳ††††⁹†⁴⁹㱲⽵㹥൳ੳ‼㨾㱳⽴㸠ൡਠ† …⁵…⁹㱳†⽴㹯ഠ†⁵†㱩⽥㸠൬ †㱭⁷⽯㹲ਠ†䍭†⁴†⁹⸊⁴㰠⁴⽩㹲ਠ††㰠†⽴㹨ਠ†⁃′⁰㰼†⼯㸾ਊ†⸠䕡㩩㱮⽯㹭൭㰠⽥㹮൴ਠ㱳⽥㸠ൡੰ⁰⁷㱧⁴⽯㸠൴੨‾㱴⁰⽥㹲൦੯⁴Ⱪ⁰†㱩⽨㹯൷੮⸍ ⁴㱴⼠㹷൨੩⁴†ⱥ㰼†⼯㸾ਊ⁶⁓⁴ 㰠⽵㹬ਠ⁰†Ⱞ††⁴㱵†⽥㹩൴੨⁴⁷㱧⽮㹴ഠ੩⸠†⁴⁷㱢⁵⽳㹥ਠ⁴††㰼†⼯㸾ਊ⁴⁵†⁴†㱯⽩㹮൳ਠ⁴⁴⡨† †Ⱐ㱳⽴㹥ਠ⁷⥹⸠㱳⁰⽯㹲൴ਠ䍯䕥†㑧㉡㉭⁴㉩ねㅥ㠬㰠⁴⼠㹡ഠ㕬㱩⁴⼠㹣ൡ੮†㨠㱡⽭㹥ഠ♴㬠⁴⁹☠㭮†† ⁔†††⁵䑡⁵⁴†㱤⼠㹵൳㱨⼠㹧൵♳㬼 ♗㭴䥲⁶††䑯††㱥⽮㹧഼†㱬⁴⽨㹥൮ਠ㱲‾⼍㸊൴੨♩㭲⁴♲㭳†䥩⁴⁷⁴⁵⁷⁴† Ɫ㰍⁹⽴㹨൯੮䜮⡰⥰‾Ɱⱨ㰬†⽕㹄ਠ⁵䥬†⁵⡭⁴‾⁵♥㭣⁴㱬⁵⽲㹴൨ⱭⱧ†⁐⤍Ⰺ†㰠†⽴㹡൫†⁴†⸮†Ⱳ†⁶ †Ⱪ††㰠⽴㸠൩੦††㱯†⽮㹯൴਼♢㬍 ♩㭭䕳⸠′†
†㰠⼊㸶മਠ†㱩⼼㹢൲ਠ☯㭨♬㬠⁴䕡†⁴Ⱐ⁴†Ɽ†䍳㱧‼⽢㹲ഠਯⰠ䍲ⁱ㱥⽰㹡൲⁷††† 㩳㱵†⽴㹨ൡੴ☠㭭䐠⁹♰㭡䕷†††♡㬠⁰䑳㰾⁔⽹㹰਼ †⸼㱢 ⼾㸍ഊ♥㭥‾☍㭲†⁴†††⁴†⸠㱡†⽴㹥൬੬☠㭳†♴㬠 䐾⁅‼‾†⁓㱲⼠㸯ാ☊㭥†♃㬼䑲Ⱐ⁴††⸾㰍⽯㹵൬☠㭯♹㭩䑧⁹†⁐⁓†⹕㱳†⽢㹹ഠੴ♨㭲⁶⁴♯㬠⁴䑨⁰㰠⼠㹡൬੬㜠⁵䅢䑭㰮⽥㸠൳††㩮㱡⽡㹬൬☠㭣⸠㰍⽲㹴മਠ♅㭬⹕㰠‸⼷㸵഼♲㬊䕡䅤䑹䴠䕃㱴⼠㹳ੲ♶㭥⸠⠠ⵥ⥴㱨⁵⽳㹥൲ਠ♳㭳†⸠⠮⤊㱭⽡㹧ਠ††㉷※…※⁉†䴯 ⁴††㱭⁴⽳㸼ൢੲ ‴†‸ †䤾䐍ℊ㱍⽡㹧਼㡢†䄯⁗ 㱩⼼㹢൲ਠ☯㭥䅣㩲㰠⽶㹥൲ਠ†㉓ⵥㅮ⁴⁶†⡰䩥㉭㍥⥬㱡⽮㹳൴ੲ䅴 ⁴⡨⁸䵴㉯㉷⥧㰠⼠㹭ੳ‼⠺⤠㱩⁴⽲㸠ൔ੨䍩䔠㑳㈠㉨⁰㉬ちㅹ㠠㱴†⽧㹡൭㘮㰼†⼯㹯൳੩♮㭐†††⡥⥬‼⠊⥃⁴†⁴″‹㰍⽩㹥൮ੴ†㰯 ⽒㹥൭੯♴㭳⁴⁴⡯⥴♲㬴☯㭥††⁄†‱㱤⼠㹳ੲ⼾⁔⁐※䤠䕕㱯†⽯㹮ഠ♥㬺††‾⁔††㱬⁵⽥㹳൳ਠ♯㭥⁴ⱥ†⁴䍥䕨 …䡱⁵⡯Ⱐⱬ†㱲⽴㸠൬⥧㰠⽴㹩൰੬†㱬⁴⽥㹲൳ਠ⁴㰠⁴⽩㹭ਠ⡳†⁷⁴⁴㱲⽳㹳ഠ⥣㱬†⼭㸠൷੩♮㭢⽲⁴⸠⡩†ⱬ††Ⱐ㱲‾⼍㸊൷੯䥲䕲⥬㱹⁙⽯㹵ഠ੬㥯䜠†㱴⼠㹡൴ੴ䝥㱁⽭㹰൴ੳ⼠⁷ㅩぬ䔠䅣䑡䵳䕥⸠㱢 ⼾㸍ഊ䍮†⡲㝥ば⥥㱴⼠㹧൵⽳ㄮ㔼䌠‾㱴⽥㹦൴⼠‵ㄼ㕢䐯⁵‼㰠⽡㹳൨⼭†ㅁぴ⁴†䐠⁵㱲‾⼍㸊ൗ੯⽲ㄺ〠⁁†⁴㱧⽳㸠൯਼⽢ㄠ〯‾㱦†⼴㸼ൢੲ⼠ ㄾ」 㱭⼠㹬੦⡵㝥び⤯㰾⁙⽯㹵ഠ⽩ㄡ㔠⁗䍲†㰼†⼯㸾ਊ⽔ㅩ㕳†䑳⁵㱯†⽬㹥൴ੴ⽥ㄠち⁴††㱨⽩㹤⁷⽯㍤〬†⁵ …※㱥⽰㹬ൡ䍥䔠㑹㈠㉴㉣ぴㄠ㡬㱥⽲㸮ഠੇ㝵㱥⽮㹧ഠ੭ㅵぬ⁴䍴⁴⁴†㱲‼⽢㹲ഠਯ䔾ㅳ㱳⼠㹴൨㰨⽣㹨ഠ㹩㍵ⴠ㱩⽲㸠൬੯⁴☭㭦㰼†⼯㸾ਊ䍣†䍯♣㬩㰮⽵㸠൷੩䌠㩯㐠㙴㑨㥥ㄠ㱣⽥㹣൴ਠ†☼㬊㱷⼠㹣൯ੲ䅲⁹⁵Ⅽ㱰⼠㹁൴ੴ♳㬠㩲䑭㰠†⽴㹨ਠ䍣䐠‾♮㭴㰠†⽲㹥൰䑴㩗㍲㥤㈺㔠㘦㱭⽨㸻ഭਠ䑬‼ ‼䍗♥㭰㱴⽥㹦൴䠠 㨠㰠⽲㸠യਾ…♨㭥㱭⼠㹬੦†㨠‴⁰㨠′☰㬠‾䄍‾㩩㕮㱴⁵⽴㹰൵ੴ䜼‾㨾†㱵⼠㹯഼䥲†⁴㰠⽲㸠യਾ䄍㩲†㐯㰾⁗⽯㹲㩭⁰䙴㱦†⼳㸼ൢੲ㩢 㨺†☭㭴䅭⁴㩢㐠㰯 ⼾㸠൧ੵ䝥‼㨠 㰍⽵㸠൷੩䍮㰍†⽥㹸൩ੴ䄼⁃㩮㐠㱔†⽡㹮ਠ†㩳…䙨㱰⽲㸠യਾ㨠 㨠⁏♴㭲†䄯㩪㑟㱳⁶⽥㹲മੰ䝹㨯‾㰊⽶㹥൲ਠ䍩※㰯 ⽃㹲䅴†㩥⁴㐦㱨⽩㹰਼഻ 㨠䙯㰠⼠㹰൯ੲ㨼㩴♧㭡䅬㨼㑲㰠⼊㹁ഠ੮䝥㩴†㱳⽮㹮䍴‡㰯 ⽕㹳ੲ䄦†㨺†㑄㱥⽳㸼ൢੲ㩩䙧㰠⽫㹥൴ਦ‼㩕⁄㩨⵰䅴›㨶″㐼㱢 ⼾㸍ഊ䝥㩄⁐㱰⁴⼠㹮൵੭䍢㱐⽮㹮䅴㩢㐠㰯 ⽈㹩䍥†㱓⽴㹩൮੧㩩⁰㰠⼊㹓੮㩴†Ⅱ※㱥⁴⽳㸠൬䡦㨍 㱳⼺㸠਼♴㭳㱳†⽣㹨ൡੲ 㩳†›㩲†☯㭮ⴠ䅴㩢㔠㰯 ⽓㹥൮䝩㩡㰺⁴⽡㹴ഠ䍯⁴㱳⽦㹴ഺਠ䄵 㩳†㕩㱳⼼㹢൲ਠ 㩥䙴㱲⽲㹥ൡ㩢 㩭⁰♴㭴†䄴 㩳⁴㕡㱴›⼠㹆ൡ੬䝳†㨍 㱥⽮㹧ഠ੭䥥⁴⁴㱴⽥㹦൴䄠㩳㐠㱩†⽯㸼ൢੲ㩣⁴䙬㱥⽤㸠ൣ੨‾㩰⁴㨼♲㭗䄠†㩥‼㑢㱲‾⼍㸊䝮†㩳㱧†⽳㹴ൡੴ䤠⁴㱢 ⼾㸍ഊੇ䅵†㨠 ㌾㰍⽣㹯൲ੲ㩬䙥㱧⽳㹥ਠ†㩥㨠″☼㬍⁗䅩㩬㍥㰼†⼯㸾ਊ䝓㩭㱳⼺㸠൳ੴ䍡䔠⁗㑯㉲㉤›㈻はㄭ㡥㰠⁴⽥㹭൰ੴ㡳㰠⽴㸺ഠਲ਼㰠›⼠㹳഼䍲⁵㱤⽡㹲഼䅲⁴㩳†㍬㱥›⼠㸳഼‾㩳⁴䙡㰠⽳㹥഼㩥›㩴†ⵗ›䄠⁴㩴㌠㱬⁴⼺㸠ള਼䝢㩵㱳⼺㸠൬਼䍢⁹㱡⽡㹤൹ਠ䅧㩢㌠㰯 ⽁㹴൴㨠′䘼㰾⁗⽩㹮ഠੳ⁆㨠 㩧†䅧†㨺†㍳㰭⼠㹁൴ੴ䝥†㩦⁴㰠⽲㸠യਾ䌍 㱯⽣㹴൬䄠†㨼㍲㰠⼊㹁൴ੴ䍥⁴ 㱩⽴㹡൴ੵ⁆㩲†㱃†⼴㸲ലਠ′㨯‾‰†ℊ⁓⁴㱢 ⼾㸍ഊ䍥†㰺⁴⽡㹴ഠ⁁⁴♬㬼㱢 ⼾㸍ഊੇ䍥䍳 is: f
Incorrectly or already guessed char
Attempts left: 1
Win status: False
Sending message: stat Word: spo-e Attempts left: 1
Guess is: w
Incorrectly or already guessed char
Attempts left: 0
Win status: False
Sending message: end You lose! Word was spoke
Client exiting
Waiting for a client…
^CClosing TCP and UDP sockets…
Client Output
>python3 jf_client.py localhost 54281
Client is running…
Remote host: localhost, remote TCP port: 54281
Please enter your name: Dennis
Server IP: 127.0.0.1
Connected to server!
Reading TCP socket for UDP Port info…
UDP Port on Server: 56163
> start
This is hangman. You will guess one letter at a time. If the letter is in the hidden word, the “-
“ will be replaced by the correct letter. Guessing multiple letters at a time will be considered
as guessing the entire word (which will result in either a win or loss automatically - win if
correct, loss if incorrect). You win if you either guess all of the correct letters or guess the
word correctly. You lose if you run out of attempts. Attempts will be decremented in the case of
an incorrect or repeated letter guess.
Word: —– Attempts left: 5
> guess e
Word: —-e Attempts left: 5
> guess i
Word: —-e Attempts left: 4
> guess o
Word: –o-e Attempts left: 4
> guess t
Word: –o-e Attempts left: 3
> guess s
Word: s-o-e Attempts left: 3
> guess l
Word: s-o-e Attempts left: 2
> guess p
Word: spo-e Attempts left: 2
> guess f
Word: spo-e Attempts left: 1
> guess w
You lose! Word was spoke
> bye
Invalid command. Try again.
> exit
Closing TCP and UDP sockets…