DatagramPacket ( IP address extraction )
i am using DatagramPacket class to send datagram from system A to System B... the datagram is received by sysB (program is working fine)...now my question is how do i retreive the Source address from this packet and store it in a Variable..( i can print it directly using System.out.println.....but unable to store it in a variable)....
can any1 please help me put here..
[384 byte] By [
live_wirea] at [2007-11-26 18:06:50]

# 1
http://java.sun.com/j2se/1.5.0/docs/api/java/net/DatagramPacket.html#getAddress()or http://java.sun.com/j2se/1.5.0/docs/api/java/net/DatagramPacket.html#getSocketAddress()
ejpa at 2007-7-9 5:37:59 >

# 2
thanx...but still how do i save it or assign it to a variable...?
my client prog:-
import java.net.*;
public class Prec2
{
public final static int port = 9999;
public static void main(String str[]) throws Exception
{
byte[] buff = new byte[50000];
DatagramSocket ds = new DatagramSocket(port);
DatagramPacket dp = new DatagramPacket(buff, buff.length);
while(true)
{
ds.receive(dp);
String temp = new String(dp.getData(), 0, dp.getLength());
System.out.println("Packet data = " + temp);
System.out.println("Source= " + dp.getAddress());
System.out.println("length= " + dp.getLength());
System.out.println("Port= " + dp.getPort());
System.out.println();
System.out.println("getSocketAddress = " + dp.getSocketAddress());
/*
String tt = new String(dp.getSocketAddress()) does not work
byte[] bb = dp.getSocketAddress() does not work
*/
}
}
}
# 3
The method doesn't return a byte[], does it? Check the Javadoc again.
ejpa at 2007-7-9 5:37:59 >

# 4
well...the getAddress() method returns an InetAddress object while the getSocketAddress() method returns a SocketAddress object....
however i found this info:-
>> public InetAddress getInetAddress() method returns address of remote host iff a DatagramSocket is connected.
i havnt worked on it, ( using connect() method etc..since i am using udp protocol)...is there any other way ? or will i hav to use connect() method to b able to catch the remote host address. ?
# 5
You're looking in the wrong place. That's a method of DatagramSocket. Not what I directed you to.
ejpa at 2007-7-9 5:37:59 >

# 6
alrite mate...i'll search a bit more...
# 7
Search? I gave you the link!
ejpa at 2007-7-9 5:37:59 >

# 8
yep...u gave me the link.
but there is very little info available....as i said earlier, i need to store the source address in a variable ( which i am still unable to do so )... i m not quite sure one way could be by calling the connect() method followed by calling the getInetAddress() method and then finally calling the disconnect() mothod.
but u said i was looking in the wrong place( getInetAddress() method corresponds to DatagramSocket Class)...now what am i supposed to do?
# 9
Well when I click the link I gave you, I end up in the documentation for the DatagramPacket class, and there is nothing there about having to call connect().
You have somehow found another pair of methods with the same names in another class. I know all about them, but they're not what I directed you to, and they have nothing to do with your question. Neither do its connect()/disconnect() methods.
ejpa at 2007-7-9 5:37:59 >

# 10
the getAddress() method returns the raw IP address of the InetAddress object.
when i chk about this method in the InetAddress class it indicates it returns data in byte array format.
public byte[] getAddress()
Returns the raw IP address of this InetAddress object. The result is in network byte order: the highest order byte of the address is in getAddress()[0].
# 11
Yep
ejpa at 2007-7-9 5:37:59 >

# 12
ok...here's my problem:
the getAddress() method returns data in byte array...now how do i assign it to a variable,
ex-
byte[] bb = packet.getAddress(); // this is giving the following error
incompatible data types
found : java.net.InetAddress
required : byte
# 13
DatagramPacket.getAddress() doesn't return a byte array. The thing you quoted above is from InetAddress.getAddress(). I'm having a really hard time believing that it's really so hard for you to follow the link I provided to get the correct information you need.
ejpa at 2007-7-9 5:37:59 >

# 14
oh...damn....now what should i do.. ?Message was edited by: live_wire
# 15
This is getting completely ridiculous.
I gave you the link to DatagramPacket.getAddress(). You looked up two or three other getAddress() methods, which have nothing to do with your question, which is why I didn't give you links to them. You still haven't apparently looked up the link I gave you. Which is where you will find the information you need. Which is why I gave it to you.
Try it.
ejpa at 2007-7-21 17:15:18 >

# 16
aahh....finallly i think i got ( well the program compiled without any errors)this is what i did ( after going through the links AGAIN )InetAddress ia = packet.getAddress();byte[] bb = ia.getAddress();i cant chk this program now coz my other pc is down
# 17
Why do you store the received datagram packet source address into a variable?
hiwaa at 2007-7-21 17:15:18 >

# 18
> oh...damn....now what should i do.. ?> > Message was edited by: > live_wireGo back to reply #1 and click on the link.
# 19
> aahh....finallly i think i got ( well the program
> compiled without any errors)
> this is what i did ( after going through the links
> AGAIN )
>
> InetAddress ia = packet.getAddress();
> byte[] bb = ia.getAddress();
>
> i cant chk this program now coz my other pc is down
You said you wanted to save the source address in a variable, well the bold line above, did that. So what is it that you really want to do? Do you want to save it as a String? What do you want to do with it, that saving as an InetAddress is not good enough?
# 20
oh...nevermind....the problem is solved...thanks to ejp..i was just working on implementing a routing algorithm...
# 21
yipes....today i tried the program....i wanted to extract and store the ip address in byte or string however it is returning junk charecters. what i did was:-
InetAddress ia = packet.getAddress();
byte[] bb = ia.getAddress(); // if i print it....shows junk charecters.
String sss = new String(ia.getAddress()); // again prints junk charecters.
although i understand why this is happening (because of periods in IP address).., i want to know if there is any other way?
# 22
What do you mean by 'junk characters'?
ejpa at 2007-7-21 17:15:18 >

# 23
sorry...in case of string it prints something like smileys...spades ...etc..etc.in case of byte it prints something like 4(@p2... ....etc ...etcc
# 24
> yipes....today i tried the program....i wanted to
> extract and store the ip address in byte or string
> however it is returning junk charecters. what i did
> was:-
>
> InetAddress ia = packet.getAddress();
> byte[] bb = ia.getAddress(); // if i print
> it....shows junk charecters.
> String sss = new String(ia.getAddress()); // again
> prints junk charecters.
>
> although i understand why this is happening (because
> of periods in IP address).., i want to know if there
> is any other way?
They are not characters nor string. They are four byte values representing an IP address value, like 127, 0, 0 and 1.
hiwaa at 2007-7-21 17:15:18 >

# 25
The [b@... thing is because you're effectively calling byte[].toString().
Have a look at what's inside each byte of the byte[], it is exacttly what you want. I think. You haven't said what you want this IP address for exactly and how you're going to use it later on.
Or else use ia.toString() directly.
ejpa at 2007-7-21 17:15:18 >

# 26
well i want to implement DSR (dynamic source routing) between 4-5 wireless pc's...thats the reason...i want the ip addresses so i can store them in a route cache.
# 27
Well, how are you trying to store them. If you are using something where you need a String then call ia.getHostAddress(). That gives you a String in the classic IP format, which seems to be what your looking for.
Edit: That fact is also easily found by reading the API doc for about 10 secs.
# 28
... as is every other issue in this thread ...
ejpa at 2007-7-21 17:15:18 >

# 29
> well i want to implement DSR (dynamic source routing)> between 4-5 wireless pc's...thats the reason...> i want the ip addresses so i can store them in a> route cache.May I ask .... how are you going to implement DSR with Java?
hiwaa at 2007-7-21 17:15:18 >

# 30
very slowly?
ejpa at 2007-7-21 17:15:23 >
