Question

I am creating a chat application using java sockets programming. I want to launch it in my local network which means my application doesn't require internet to run. I tested the application on my computer itself by using the

InetAddress.getLocalHost();

method to create an Inetaddress object of my ip address. Now the problem comes when i want to create this object with some other ip address in the local network..

After some experimenting i came to know that there is another function Inetaddress.getbyAddress(byte[]);

which takes byte array as ip address argument. Now if i want to create an InetAddress object of an ip address say 192.168.234.190 i am not able to... since the . byte array holds only values up to 127!!

what to do?

thanks in advance...

Was it helpful?

Solution

You could use InetAddress.getByName("192.168.234.190"); or if you really want to use getByAdress:

InetAddress.getByAddress(new byte[]{(byte)192,(byte)168,(byte)234,1});

The IP can be stored like this:

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

Now you have the IP in the array of bytes named IP and you could call multiple times InetAddress.getByAddress(IP);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top