Comparision of IP
Dear Friends,
I am new to the world of RMI. I am currently working on the client/server program using the Java RMI.
I am actually trying to compare two IP Address, but i have not found a solution. I am trying to use
public void isVeryCloseIP(byte[] addr0,
byte[] addr1)
throws RemoteException
{
if (addr0 == addr1) {
System.out.println("Both the IP address match, therefore a Multicast address will be assigned");
System.out.println("The following Multicast address will beassigned: 239.0.0.1");
}
else
System.out.println("A Unicast transmission will be done");
}
The problem is the client sends the ip address in a string and this function works in byte. They just dont get along and when i try to convert them i dont get a proper solution. Is there any other way to do the conversion ?
Hope to hear from you'll soon.
Thanking you in advance.
Regards
Gan.P
[984 byte] By [
Gan.Pa] at [2007-11-26 14:23:51]

# 1
No, the problem is not String vs. Byte, the problem is your if statement. When you use == you are trying to determine if two variables are references to the same object (i.e. do both variables contain a handle to the same object, in this case a byte array). What you want to do is to determine whether the contents of the referenced objects are the same. You would be better off leaving the addresses as Strings (not byte arrays) and using the equals method of String.
if (addr0.equals(addr1)) {
// your code
} else {
// your code
}
Edit: typo
# 2
Dear Masijade,
Many Thanks for your reply.
The reason on why i need to be converted to bytes, is i want to verify the subnet which they belong too. when we compare string, it compares whether the address is exactly the same.
If we change it to bytes, it will check the first two parts of the ip address to verify whether they belong to the same subnet. Thats why i need it to be in bytes. The client sends the address in a string format, and the server receives it in a string format. So this is where i need the change to be done.
Thank you again.
Regards
Gan.P
# 3
You would still be better off using Strings. You can use
String[] parts0 = addr0.split("\\.");
String[] parts1 = addr1.split("\\.");
Then you can compare each of the four resulting Strings in each array to its corresponding String in the second array using the equals shown above.
Again. Your real problem is that you are comparing the object references using == when what you really want to do is to compare the referenced objects contents.
# 4
> Then you can compare each of the four resulting> Strings in each array to its corresponding String in> the second array using the equals shown above.>That won't work with subnets.
# 5
> The reason on why i need to be converted to bytes, is
> i want to verify the subnet which they belong too.
> when we compare string, it compares whether the
> address is exactly the same.
Hard to tell what you want.
Do you want to check subnets? Then if it was me I would convert the IP to a long and the subnet to a long then mask the values to find a match.
You could do that with bytes as well just by looping through them and masking each one.
Regardless how you do it a comparison won't work to figure out if it is a subnet. You have to mask it.
# 6
Dear Masijade,
Many Many Thanks for your reply.
I will need a small help from you. Your earlier codes on .equal works fine for me, many thanks to you. In your following reply you said,
String[] parts0 = addr0.split("\\.");
String[] parts1 = addr1.split("\\.");
i am not too sure on how to use it. I hope you would be able to help. My string comes in from the client as Addr0 and another clients sends in as Addr1. Since i am relatively new to the world of programming, i am a bit ble here. Hope you would be able to guide me.
Thanking you in advance.
Regards
Gan.P
# 7
Well, first we need to ask (I'm sorry I wasn't thinking about subnets), as jschell did already, are you going to need to worry about subnets?
Or are you only worried about Class B/C nets?
If you are worried about subnets, then the IP Addresses will need to be converted to IP Numbers and masked to the subnets.
# 8
Dear Masijade,
I need to compare the two subnets of the ip, so that if they are similar i will assign them a multicast address. thats the reason for me to compare the subnets and for me to change them to bytes.
i am yet to convert them into bytes, always end up with errors.
hope to get some solution soon.
thank you
regards
Gan.P