0

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...

nitish712
  • 19,504
  • 5
  • 26
  • 34
  • No, a byte holds objects having bit patterns 0x00 through 0xff. Java interprets those as signed, but you can still store 256 distinct bit patterns in each byte. – Jim Garrison Mar 24 '12 at 08:34

1 Answers1

2

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);

Burkhard
  • 14,596
  • 22
  • 87
  • 108
  • but when i try to print the values in that array i am getting -64 -88 -22 1 !!! but not those values.... how is this possible... – nitish712 May 31 '12 at 05:08
  • The byte (in java) data type is an 8-bit signed two's complement (http://en.wikipedia.org/wiki/Two%27s_complement) integer. 128 is represented as -127, because the first bit is 1 which means it is a negative number. The unsigned representation is still 128. Same for 192 which is a signed -64 and an unsigned 192. – Burkhard May 31 '12 at 05:53
  • yea yea i know that... but how do you store remaining values upto 255 – nitish712 Jun 07 '12 at 07:38
  • These "byte" are just a few (8) bits (0 or 1). It is the JVM (or the Java specs or just Java) that interprets these bits as a number between -128 and 127 (a byte is signed). So if you store 192 as a byte or -64 it will both be the same bit pattern, but in one case the first bit is used as a sign bit (see my above link to wikipedia). – Burkhard Jun 07 '12 at 09:20
  • so can't i store a value more than 127...??? coz i am unable to understand how to create a byte array which holds the ip address which has bit values more than 127?? & this byte array is required for creating the INetAddress object...plz tell me Burkhard.. :( – nitish712 Jun 10 '12 at 14:00
  • "0xff" is the hexadecimal representation of 255. If you store 0xff into an int and then print it, you will get 255 and not 0xff. Likewise, 128 (0x80) stored in a byte will still be 0x80, but printed on the screen it will be -128. Java could have used an int instead of a byte, but they decided not to in order not to waste space (8 bit versus 32/64). – Burkhard Jun 10 '12 at 15:58