Get Socket Information : TCP « Network Protocol « Java






Get Socket Information


import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

public class getSocketInfo {

  public static void main(String[] args) {

    for (int i = 0; i < args.length; i++) {
      try {
        Socket theSocket = new Socket(args[i], 80);
        System.out.println("Connected to " + theSocket.getInetAddress()
            + " on port " + theSocket.getPort() + " from port "
            + theSocket.getLocalPort() + " of " + theSocket.getLocalAddress());
      } // end try
      catch (UnknownHostException e) {
        System.err.println("I can't find " + args[i]);
      } catch (SocketException e) {
        System.err.println("Could not connect to " + args[i]);
      } catch (IOException e) {
        System.err.println(e);
      }
    }
  }
}
           
       








Related examples in the same category

1.A tcp client, a tcp server, and a Serializable payload object which is sent from the server to the client
2.Use the Daytime TCP and Daytime UDP classes
3.Finger Socket
4.Multicast Sniffer
5.Multicast Sender
6.Connects to the default chargen service port
7.Connects to the default echo service portConnects to the default echo service port