Java Reference
In-Depth Information
public class MultiEchoClient
{
private static InetAddress host;
private static fi nal int PORT = 1234;
public static void main(String[] args)
{
try
{
host = InetAddress.getLocalHost();
}
catch(UnknownHostException uhEx)
{
System.out.println("\nHost ID not found!\n");
System.exit(1);
}
sendMessages();
}
private static void sendMessages()
{
Socket socket = null;
try
{
socket = new Socket(host,PORT);
Scanner networkInput =
new Scanner(socket.getInputStream());
PrintWriter networkOutput =
new PrintWriter(
socket.getOutputStream(),true);
//Set up stream for keyboard entry…
Scanner userEntry = new Scanner(System.in);
String message, response;
do
{
System.out.print(
"Enter message ('QUIT' to exit): ");
message = userEntry.nextLine();
//Send message to server on the
//socket's output stream…
//Accept response from server on the
//socket's intput stream…
networkOutput.println(message);
Search WWH ::




Custom Search