CS470 Programming Assignment 1 Zilong Ye
A Chat Application Goal: learn socket programming and implement a chat application for exchanging messages between remote users; Peer 1 Peer 3 Peer 2 Socket enables this “Hello” “Hi back”
Start the program Name your program as “chat” Specify the port number that the process is running on The process works like a unix shell E.g.: C://course/cs470/./chat 4545 >>
help Show the command manual E.g.: C://course/cs470/./chat 4545 >> help >> myip display IP address connect connect to another peer send send messages to peers …………. exit exit the program >>
myip Show the IP of your laptop E.g.: >> myip >> The IP address is >>
myport Show the port number that the process runs on E.g.: >> myport >> The program runs on port number 4545 >>
connect E.g.: >> connect >> The connection to peer is successfully established; >> Peer 2 displays the connection news and update its list On peer 2, e.g.: >> The connection to peer is successfully established; >>
list List all the connected peers E.g.: >> connect >> The connection to peer 2 is successfully established; >> list id: IP address Port No. 1: : : : >> On peer 2, e.g.: >> list id: IP address Port No. 1: >>
send E.g.: >> list id: IP address Port No. 1: : : : >> send 2 Hello
send Peer 2 receives the message and should display the message >> Message received from : “Hello” >> list id: IP address Port No. 1: >> send 1 Hi, back >> Peer 1 receives the reply and should display the message >> Message received from : “Hi, back” >>
terminate Terminate the connection E.g.: >> list id: IP address Port No. 1: : : : >> terminate 2 >> list id: IP address Port No. 1: : :
terminate Peer 2 displays the termination news and update its connection list >> list id: IP address Port No. 1: >> Peer terminates the connection >> list id: IP address Port No. >>
exit Terminate all the connections and exit the program; The other peers should display the exit news and update their peer list; E.g.: C://course/cs470/./chat 4545 >> help ……. >> list ……. >> exit C://course/cs470/
Useful links Socket Programming: Beej Socket Guide: Select function: ect.3.asp ect.3.asp
How to recognize the command input
Code structure
Fill out addr_info structure
Regular steps for socket programming
How to use connect
myip is your “Lo” IP address, and it is NOT what we want; Create a UDP socket to contact any public DNS server (e.g., or by Google) to get a response from the DNS server and obtain your real IP address
Select based I/O
Select() related functions
Select() example