首页 > > 详细

python辅导Socket Programming辅导C/C++编程、C/C++讲解

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…

 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!