About the Chat program 26-Jul-16

advertisement
About the Chat program
26-Jul-16
Constraints

You can't have two programs (or two copies of the same
program) listen to the same port on the same machine







This is not a problem if the two programs are on different machines.
Socket and ServerSocket are unrelated classes—neither extends
the other
You can't successfully set up a client Socket unless there is a
server running
A client Socket cannot accept() a message; only a
ServerSocket can do that
A ServerSocket cannot send a message; only a client Socket can
do that
The only way to get a client Socket from a ServerSocket is to
accept() a message
Once a Reader has read in a message, it apparently can’t be used
again
2
A successful approach, I


Try to set up a server on port #1
If successful (i.e. you don’t get an exception),


else



Set output port = port #2
Set up a server on port #2
Set output port = port #1
Create a thread to listen to (accept messages from) the
server port
3
A successful approach, II

The listener for user input creates a writer for the output
port and writes to it



It also copies the text to the display area, and clears the text
input area for future use
It should be possible to use the same writer every time, but
that doesn’t seem to work for me
The run() method accepts the message, creates a reader
for it, and reads and displays the message

As long as you know all input comes from the same port, it
should be possible to use the same reader every time; but even
if you can make this work, it probably isn’t a good idea
4
The End
5
Download