4: Correction to Chat Program
Fall 2004, David Matuszek
There seems to be a flaw in the original version of this assignment. Namely, you apparently cannot have two programs listening to the same port simultaneously. (I did have a reason for thinking you could do this, but my reasoning was incorrect.)
Since each instantiation of the program must (1) have a blocking
accept() messages, and (2) have a nonblocking
to send messages, it follows that two ports are required.
Another possibility is to use a single socket, with timeouts, but that's too ugly to be considered.
For consistency (that is, to make it easier for us to test your program), please
have the first instantiation of your program listen on port
as on the original assignment (this would be the blocking thread), and
send on port
8123. The second instantiation of your
program should do the reverse: Listen on port
8123 and send
As usual, apologies for getting it wrong the first time. Thanks to Charlie for pointing this out.
I mentioned that some things might need to be
me clarify that a bit.
If two people were using this program on two separate computers, it is possible that both Threads in your program would try to insert something into the main text area at the same time. You should guard against this possibility by synchronizing the main text area.
In practice, this would happen only rarely--but it could happen, with the resultant loss of a message. In the case of the server and the client running on the same computer (with one human using both sides), it is a virtual impossibility for this problem to occur. Hence you cannot readily test whether your synchronization correctly provides protection. (You could test this by having each side send a rapid-fire barrage of messages--maybe--but I'm not asking for that.)
A different error would be if your synchronization caused your Threads to deadlock, each waiting for the other, in which case your program would grind to a halt. This would be quite noticeable, and should be easy enough to fix.
Bottom line: Do the synchronization, try to get it right, but don't worry about testing it.