Java Reference
In-Depth Information
{
ioEx.printStackTrace();
}
}
public void run()
{
String received;
do
{
//Accept message from client on
//the socket's input stream…
received = input.nextLine();
//Echo message back to client on
//the socket's output stream…
output.println("ECHO: " + received);
//Repeat above until 'QUIT' sent by client…
}while (!received.equals("QUIT"));
try
{
if (client!=null)
{
System.out.println(
"Closing down connection…");
client.close();
}
}
catch(IOException ioEx)
{
System.out.println("Unable to disconnect!");
}
}
}
The code required for the client program is exactly that which was employed in
the
TCPEchoClient
program from the last chapter. However, since (i) there was only
a modest amount of code in the
run
method for that program, (ii) we should avoid
confusion with the
run
method of the
Thread
class and (iii) it'll make a change (!)
without being harmful, all the executable code has been placed inside
main
in the
MultiEchoClient
program below.
import java.io.*;
import java.net.*;
import java.util.*;
Search WWH ::
Custom Search