View RSS Feed

sixxvirus

Get the info of a socket

Rate this Entry
by , 05-16-2012 at 12:50 AM (2031 Views)
This program reads a list of hostnames from the command-line, attempts to open a socket to each host, and then prints to the remote host, the remote port, and the local address, and the local port.

** Happy Coding**

Java Code:
/* SocketInfo.java
 * Get the info of a socket
 */
import java.net.*;
import java.io.*;
public class SocketInfo{
    public static void main(String[] args){
        for(int index=0; index<args.length; index++){
            try{
                Socket s=new Socket(args[index], 80);
                System.out.println("Connected to "+s.getInetAddress());
                System.out.println("On port "+s.getPort()+" from port "+s.getLocalPort());
                System.out.println("Of "+s.getLocalAddress()+"\n");
            }catch(UnknownHostException e){
                System.err.println("Cannot find "+args[index]);
            }catch(SocketException e){
                System.err.println("Cannot not connect to "+args[index]);
            }catch(IOException e){
                System.err.println(e);
            } // end exception
        } // end loop
    } // end main
} // end class
renukabharate and Vinx like this.

Submit "Get the info of a socket" to Facebook Submit "Get the info of a socket" to Digg Submit "Get the info of a socket" to del.icio.us Submit "Get the info of a socket" to StumbleUpon Submit "Get the info of a socket" to Google

Comments