Java Reference
In-Depth Information
{
try
{
System.out.println(“Waiting for client on port “
+ serverSocket.getLocalPort() + “...”);
Socket client = serverSocket.accept();
System.out.println(“Just connected to “
+ client.getRemoteSocketAddress());
client.close();
}catch(SocketTimeoutException s)
{
System.out.println(“Socket timed out!”);
break;
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
public static void main(String [] args)
{
int port = Integer.parseInt(args[0]);
try
{
Thread t = new SimpleServer(port);
t.start();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
Figure 17.1 shows the output of the SimpleServer program when 5001 is the
command-line argument. The server program is waiting for a client, which I will
show you how to create next. Because no client comes along and connects, this
accept() method blocks for 10 seconds (why?) and the SocketTimeoutException
occurs, causing the thread to run to completion and the program to terminate.
Figure 17.1
Output of the SimpleServer program.
Search WWH ::




Custom Search