RSS

Comparing InetAddress and InetSocketAddress in Java

22 Dec

In java, InetAddress and InetSocketAddress seem to be same. There are describe the IP’s address and host’s name. Of course, There’re support the method of routine to checking address is : Loopback’s address, local’s address, multicast’s address; And the basic return method : get IP, get host’s name…

The important thing, InetSocketAddress includes InetAddress. Meaning , we have do any thing of InetAddress into InetSocketAddress, just only .getAddress() .

Comparing Table:

Property

InetAddress

InetSocketAddress

Described object for

IP Address

Socket address( IP’s address + port number)

Describe

IP and host’s name of object

IP and host’s name of object  and include port number

Resolve

IP to Name, Name to IP

IP to Name, Name to IP could include port

Get object from

InetAddress.getLocalhost();

InetAddress.getByName(String);

InetAddress.getByAddress(String);

InetSocketAddress.createUnresolved(String, port);

 

Some code example:

public class  hotdream {

public static void main(String args[]){

byte[] b = new byte[] {(byte)192,(byte)168,(byte)1,(byte)1};

try {

InetAddress add = InetAddress.getByAddress(b);

System.out.println(add.toString());

add = InetAddress.getByName("localhost");

System.out.println(add.toString());

InetSocketAddress addsock = InetSocketAddress.createUnresolved(

"localhost", 90);

System.out.println(addsock.toString());

}catch(Exception e) {

}

}

}

written by hotdream1990

About these ads
 
1 Comment

Posted by on December 22, 2010 in Java

 

Tags:

One response to “Comparing InetAddress and InetSocketAddress in Java

  1. commitflame

    January 7, 2011 at 3:26 am

    Address had looked up in DNS local

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.