Monday, 1 October 2012

Network programming in Java






Network programming in Java


Package:
import java.net.*;
Can use these classes to (a) communicate with any server, (b) construct your own server.

Java network programming reference

Java Reference
Java Standard Edition
Tutorials
Networking tutorial
JDK 6 docs
Networking
API
java.net package
exampledepot.com
java.net examples

Example - Find IP address

Find numeric (IP) host address given text address.
InetAddress.getByName(hostname)

From Graba:



import java.net.*;
import java.io.*;

public class ip 
{
  public static void main ( String[] args ) throws IOException 
  {
    String hostname = args[0];

    try 
    {
      InetAddress ipaddress = InetAddress.getByName(hostname);
      System.out.println("IP address: " + ipaddress.getHostAddress());
    }
    catch ( UnknownHostException e )
    {
      System.out.println("Could not find IP address for: " + hostname);
    }
  }
}
Run it:


$ javac ip.java

$ java ip www.computing.dcu.ie 
IP address: 136.206.11.240

Q. Write program to find text given numeric.

See DNS lookup.


getLocalHost

To find your own numeric IP address in Java:
getLocalHost 
Works on DCU Win.
Works on DCU Solaris.
Doesn't seem to work on DCU Linux. Can anyone tell me why?

Other ways to find your IP address:

On DCU Linux (lab machines):
$ ip addr list eth0
On DCU Linux (ssh student.computing.dcu.ie):
$ hostname -f
shows we are in dcu.ie

$ cat /etc/resolv.conf
shows my DNS server is in 136.206
On Windows:
$ ipconfig
Click a remote site. e.g. My PHP pages to give:
REMOTE_ADDR
HTTP_X_FORWARDED_FOR

My local host is not to be confused with: 127.0.0.1


TCP Sockets

Connection-oriented.
Must explicitly socket.close()
Socket class

Example - Query open ports

Port scanner - look at some machines in DCU to find ports that are "open" - providing a service.
Does this by trying to open a socket to that port.



import java.net.*;
import java.io.*;

public class ports 
{
  public static void main ( String[] args ) throws IOException 
  {
    String hostname = args[0];

    Socket s = null;

    try 
    {
      // this is to see if host exists:
      InetAddress ipaddress = InetAddress.getByName(hostname);

//      int p =  21;            // ftp
//      int p =  22;            // ssh / sftp
//      int p =  23;            // telnet
//      int p =  25;            // smtp
        int p =  80;            // http
//      int p = 110;            // pop3
//      int p = 143;            // imap

                try
                {
                  s = new Socket(hostname, p);
                  System.out.println("A server is running on port " + p + ".");
                  s.close();
                }
                catch (IOException e)
                {
                  System.out.println("No server on port " + p + ".");
                }
    }
    catch ( UnknownHostException e )
    {
      System.out.println("Could not find host: " + hostname);
    }

        if (s != null)
        {
                try
                {
                        s.close();
                }
                catch ( IOException ioEx )
                {
                }
        }
  }
}
Can now look for http servers:


$ java ports www.dcu.ie
A server is running on port 80.

$ java ports dgrayweb.computing.dcu.ie
A server is running on port 80.

$ java ports mailhost.computing.dcu.ie
A server is running on port 80.

POP3 servers:


$ java ports mailhost.computing.dcu.ie
A server is running on port 110.

Search for IMAP servers.

Search for ssh servers from outside DCU for:

student.computing.dcu.ie
Caution when scanning ports: Some sites don't like this.
Scanning lots of ports looks like hostile intent.
If firewall blocks a port, program will wait until timeout - could take a while.

No comments:

Post a Comment

There was an error in this gadget