Presentation is loading. Please wait.

Presentation is loading. Please wait.

19-Aug-15 About the Chat program. 2 Constraints You can't have two programs (or two copies of the same program) listen to the same port on the same machine.

Similar presentations


Presentation on theme: "19-Aug-15 About the Chat program. 2 Constraints You can't have two programs (or two copies of the same program) listen to the same port on the same machine."— Presentation transcript:

1 19-Aug-15 About the Chat program

2 2 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

3 3 A successful approach, I Try to set up a server on port #1 If successful (i.e. you don’t get an exception), Set output port = port #2 else Set up a server on port #2 Set output port = port #1 Create a thread to listen to (accept messages from) the server port

4 4 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

5 5 The End


Download ppt "19-Aug-15 About the Chat program. 2 Constraints You can't have two programs (or two copies of the same program) listen to the same port on the same machine."

Similar presentations


Ads by Google