CSC145/290 Programming Assignment 3 Simple Chat Client/Server For this assignment you'll be writing a single program that can both be client and server. You are to do this assignment with ONE and exactly ONE other person. If you're the odd person out in the class, I'll put you in a group of three. The idea is to be able to alternate between sending and receiving text with a remote host, thereby "chatting" with the other side. Here's the protocol specification: 1. Both client and server will use port 30517 (or some agreed port, see below) 2. The program will initiate by specifying a remote ip to connect to, as in "./mychat 147.4.150.248" 3. The initiating program will FIRST TRY TO BE CLIENT by connecting to port 30517 on the remote host, 4. If this connection fails, it means the other side is not up, so you must therefore switch to SERVER and listen on the same port. 5. The other side will be doing the same thing, so eventually one side will be client and the other server. (on rare occasions both sides will try to connect at the same time and both fail - but we won't worry about that: the internet, as you know, never works perfectly). Now, the initiating side (side that became server) will initiate the two-way chat by sending out the first string. 6. Your program will terminate when you receive ".signoff." from the other side. Since you'll be communicating ascii data, you should create FILE* streams and use fprintf, fscanf over the socket (don't forget to fflush the output streams). You should also write a function that lets you read a line from the keyboard (Linux has readline built-in, we don't have it on Solaris here - consult the text for help). Since a lot of people will be using husun3, port 30517 will be very busy, in which case you should simply try another port (30518, 30519, etc). Use the netstat program to find out what ports are being used. 7. After you get this much working, modify your program to take another command-line argument: the username of the person on the otherside you want to talk to (all sorts of weirdos will be using 147.4.180.241). Learn to use the "getlogin" function (man getlogin). The first two strings exchanged between the two sides should be usernames that each side want to talk TO (server should send first). Terminate connection if the other side is not trying to talk the current user. This provision also means that if you are trying to talk to somebody but someone else answered first, your program should restart. Be warned: We will be going to the lab and your program will be tested against those written by others, so it can't just work with itself on both sides. Make sure you read this document carefully and observe all protocol details (especially regarding point 7 - think carefully). Food for thought: As you do this assignment, which shouldn't be hard, you should be thinking about what's necessary to improve the program, including: 1. instead of alternating between sending and receiving, both sides should be able to write/read simultaneously. 2. port 30517 will be busy once this little program becomes really popular, and using netstat to find a common free port on both hosts is obviously cumbersome. What's needed is a "daemon" server on a designated port, to which all sides will connect to. The purpose of the daemon server will be to "demultiplex" the various connections. The daemon server can also pick free ports that can be shared by client/servers. 3. Using "getlogin" to check username works if the users don't know how to write their own client/server applications. But there is obviously a serious security/ethical issue. If someone does know how to write their own networking programs, they can easily bypass this test by telling the other side what they "want to hear". This is not just a programming issue but also an administrative, legal and ethical one. As a network programmer who have access to server systems, you carry a degree of moral responsibility far greater than other programmers and users.