Package Chapter2;
Import java.io.IOException;
Import java.net.*;
public class Connecttester {
public static void Main (string[] args) {
String host = "localhost";
int port = 8000;
if (Args.length > 1) {
Host=args[0];
Port = Integer.parseint (args[1]);
}
New Connecttester (). Connect (host, port);
}
public void Connect (String host, int port) {
//
SocketAddress Remoteadd = new Inetsocketaddress (host, Port);
Socket socket = NULL;
String result = "";
try {
Long begin = System.currenttimemillis ();
Socket = new socket ();
Socket.connect (Remoteadd, 10000);//Maximum connection time is 10 seconds
Long end = System.currenttimemillis ();
result = (End-begin) + "MS";
} catch (Bindexception e) {
result = "Local address and Port can ' t be binded";
} catch (Unknownhostexception e) {
result = "Unknown Host";
} catch (Connectexception e) {
result = "Connectexception";
} catch (Sockettimeoutexception e) {
result = "TimeOut";
} catch (IOException e) {
result = "Failure";
} finally {
try {
if (socket! = NULL)
Socket.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
System.out.println ("Access:" +socket.getremotesocketaddress () + "Connection time" + result);
}
}
Socket Programming 2-connecttester