Java Reference
In-Depth Information
String message, response;
do
{
System.out.print("Enter message: ");
message = userEntry.nextLine();
output.println(message); //Step 3.
response = input.nextLine(); //Step 3.
System.out.println("\nSERVER> "+response);
}while (!message.equals("***CLOSE***"));
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
fi nally
{
try
{
System.out.println(
"\n* Closing connection… *");
link.close(); //Step 4.
}
catch(IOException ioEx)
{
System.out.println(
"Unable to disconnect!");
System.exit(1);
}
}
}
}
For the preceding client-server application to work, TCP/IP must be installed
and working. How are you to know whether this is the case for your machine? Well,
if there is a working Internet connection on your machine, then TCP/IP is running.
In order to start the application, fi rst open two command windows and then start the
server running in one window and the client in the other. (Make sure that the server
is running fi rst, in order to avoid having the client program crash!) The example
screenshots in Figs. 2.3 and 2.4 show the dialogues between the server and two
consecutive clients for this application. Note that, in order to stop the TCPEchoServer
program, Ctrl-C has to be entered from the keyboard.
Search WWH ::




Custom Search