CS332: TCP Chat Server

advertisement
CS332:TCPChatServer
Forthisassignment,youneedtowritethechatserverforwhichyouwroteaclient
lastweek.
Thebasicoutlineoftheservercodeisthis:
• Yourcodereadsoptionsfromthecommandline(seebelow).
• Yourcodeopensasocket,makesitREUSEADDR (lookitup!),bindsittoa
port(default12345),andbeginslisteningonit.
• Addthismainsockettoalistofsocketsfromwhichtoread.I’llrefertothat
variableassocks.
• Inaforeverloop:
o Doaselect() onsocks
o Ifthesocketthatisreadytoreadfromisthemainone,acceptthenew
connection,andsendthestring“Thankyouforconnecting”onthe
socket.Addthenewclientsockettoyoursockslist.
o Else(thesocketisanalreadyconnectedclientsocket):
§ Callrecv(1024)onthesocket(toreadupto1024bytes),
readinginthemessagefromthesocket.
§ Iftheresultisbad,removethatsocketfromsocks.
§ Else:
• Sendthemessageyoureadfromthesocketonallthe
otherclientsockets.
Youmayimplementthisinanylanguageyouwant,buttheoutlineI’vegivenyou
aboveisbasedonmypythonimplementation.
Youmustimplementthesecommand-linearguments:
Name(s)
Option
Meaning
Defaultvalue(if
optionnotgiven)
-v,--verbose
Turnonverbose
False
printingtohelp
debugging
-p,--port
Portnumber
TheTCPportthe
12345
serverislistening
on.
Whenverboseisturnedon,yourclientshouldprintoutenoughinformationtohelp
youfigureoutwhatyourprogramisdoing.E.g.,myclientprintsoutamessage
wheneveritgetsanewconnectionfromaclient,wheneverthatclientconnection
goesaway,wheneveritresendsamessagetoallclients,etc.
ImplementationNotes
Yourcodeshouldhandleerrorsgracefully.I.e.,ifasystemcallreturnsanerror,you
shouldprintoutausefulmessageandexitgracefully.Theusershouldnotseeacore
dumporastacktraceprintedout.Youmusthandlethecasewheretheservergoes
downwhenyourclientisstillconnected.
Youwillfindthefirstlinesofthetalk_server.pyfilein
/home/cs/332/sp2016/chat/folder.
Youexityourtalk_serverbydoingCtrl-Cintheterminalwindowinwhichitis
running.
Thereareatonofimplementationsofthisonline.Foryourownlearning
experience,donotcopyandpastecodefromthem.Ifyouhavetolookuphowto
useselect(),e.g.,you’llprobablyseeanimplementationoftheclientorserver…Try
toimplementasmuchasyoucanwithoutcopyingcodefromonline.Youmustcite
everyplacewhereyoufindcodethatyoucopyintoyourcode.
ThoseDoingtheClassforHonors
Theservermustrecognizeandrespondtothefollowingspecialmessagethatclients
cansend:
• /who:returnastringofinfoaboutwhoallisconnected.Thestringlookslike
this:
<n> clients connected.
<name> connected from <IP address>, <port>.
… <previous line repeated for each client> …
Todothis,you’llhavetokeeptrackofinfoaboutthenamestheclientschose
forthemselves,andwheretheclientsareconnectedfrom.Idothisina
dictionarythatmapsthesockettoa(name,IPaddress,port)tuple.TheIP
addressandportyougetfromtheresultoftheaccept()call.Initially,the
namewillbeunknown,soIjustusethestring“<unknown>”.Thenameyou
willhavetogetfromamessagesentbytheclient.Recallthateachclient
prependsthefollowingtothebeginningofeachmessage:
<name> says: <text of message>.
Whenyoureceiveamessagefromaclient,parsethatmessage,extractingthe
<name>andupdateyourdictionarywiththename.
GradingRubric:20ptstotal
• 15pointsforcorrectness.
o Operatescorrectly:8
o Handlescommand-lineoptionscorrectly:4
o Handleserrorconditionscorrectly:3
• 5pointsforclean,well-indented,well-documentedcode.
Don’tforgettoputyourname,date,etc.,inacommentatthetopofyourfile(s).
Submitbycopyingto/home/cs/332/current/<yourid>/chat/.
Download