Java Reference
In-Depth Information
2.4 Program Echo is similar to program TCPEchoClient from Sect. 2.2.1 , but has a
GUI front-end similar to that of program GetRemoteTime from Sect. 2.3 . It
provides an implementation of the echo protocol (on port 7). This implementa-
tion sends one-line messages to a server and uses the following components:
￿
a text fi eld for input of messages (in addition to the text fi eld for input of host
name);
￿
a text area for the (cumulative) echoed responses from the server;
￿
a button to close the connection to the host.
Some of the code for this program has been provided for you in fi le Echo.java , a
printed copy of which appears at the end of this chapter. Examine this code and
make the necessary additions in the places indicated by the commented lines. When
you have completed the program, run it and supply the name of any convenient
server when prompted for a server name. If you don't have access to a convenient
server use localhost , having changed the port number of TCPEchoServer
(Sect. 2.2.1 ) to 7 and then started that program running.
//For use with exercise 2.2.
import java.io.*;
import java.net.*;
import java.util.*;
public class EmailServer
{
private static ServerSocket serverSocket;
private static fi nal int PORT = 1234;
private static fi nal String client1 = "Dave";
private static fi nal String client2 = "Karen";
private static fi nal int MAX_MESSAGES = 10;
private static String[] mailbox1 =
new String[MAX_MESSAGES];
private static String[] mailbox2 =
new String[MAX_MESSAGES];
private static int messagesInBox1 = 0;
private static int messagesInBox2 = 0;
public static void main(String[] args)
{
System.out.println("Opening connection…\n");
try
{
serverSocket = new ServerSocket(PORT);
}
catch(IOException ioEx)
Search WWH ::




Custom Search