Finding IP or MAC address of sending unit in UDP

Comalco

Member
Licensed User
Longtime User
Is there a method to get the IP address of the sending (remote) PC in a UDP transaction? Or its MAC address? I have UDP packets coming from multiple sources and I am trying to identify which packet is from which source.....

thanks.
 

Comalco

Member
Licensed User
Longtime User
I should have added......

I would get it was a function of the UDPSocket? I can get MyIP easily enough of course, its the sender I want to ID.
 
Upvote 0

Comalco

Member
Licensed User
Longtime User
So I should have read Packet.host first, BUT

I have the following code;

UDP_PacketArrived (Packet as UDPPacket)

msg = bytestostring(Packet.data, packet.offset, packet.length, "UTF-8")
log(Packet.host & " " & msg)

end sub

So when I include the "Packet.host" in my log statement this log function slows down a heck of a lot. If I just log "msg" it flies......

I would have thought that packet.host was already known as soon as the PacketArrived event was raised???? Why does including it in the log statement slow it down soooooooo much??
 
Upvote 0

Comalco

Member
Licensed User
Longtime User
So how does this work??

I found the below code in a response to a similar packet.host time delay query.

Sub GetHostAddress(packet As UDPPacket) As String
Dim r As Reflector
r.Target = packet
r.Target = r.GetField("packet")
r.Target = r.RunMethod("getAddress")
Return r.RunMethod("getHostAddress")
End Sub

I have tried embedding it in my code to retrieve the host address;

Dim packet as UDPPacket

Sub UDP_PacketArrived (Packet as UDPPacket)
Dim r As Reflector
r.Target = packet
r.Target = r.GetField("packet")
r.Target = r.RunMethod("getAddress")
Return r.RunMethod("getHostAddress")
log(gethostaddress)
end sub

But obviously I still havent understood how to use this code.

What do I need to DIM for this snippet, I have read the syntax explanations in the Reflection Library, but it still doesnt make sense to me (yep, a newbie, still learning B4A!)
 
Upvote 0

Comalco

Member
Licensed User
Longtime User
Thanks Erel.

It compiles OK, but when run on the tablet it gives a "No such method exception" error on either of these lines if I don't rem them out....

r.Target = r.RunMethod("getAddress")
Return r.RunMethod("getHostAddress")

do I have a DIM issue? or ??
 
Upvote 0

Comalco

Member
Licensed User
Longtime User
Error Msg

Sorry, I have this running on a different PC than this one on the internet.
Have typed this in manually from the other PC's screen..........

java.lang.nosuchmethod exeption: getaddress[]
 
Upvote 0

Comalco

Member
Licensed User
Longtime User
Full Error Log (I think) attached in ZIP

Here is the full (I think) relevant section of the log.............attached as a PDF
 
Upvote 0

Comalco

Member
Licensed User
Longtime User
App Code............

Here it is.......it isnt anything specific.
Klaus' Oscilloscope App with lots of stuff removed - as I have been playing/learning with a few different things along the way........
It's messy I know, but just a scratch pad for learning
 
Upvote 0

Comalco

Member
Licensed User
Longtime User
I have corrected the case sensitive words, but....

Erel,

Many thanks, I have a lot to learn yet.

I have made the corrections, the code compiles and bridges into my tablet and runs OK without crashing with errors, BUT

My EditText2.text and "log" statements at the end of the UDP_PacketArrived routine dont seem to execute.
My EditText1.text is, and Ic an see the UDP packets being received as they come in OK. Its almost as if my code isnt getting past the Reflection "r." lines?

Latest version attached so you can my corrections/changes implemented.
 

Attachments

  • Learning App.zip
    63.1 KB · Views: 381
Upvote 0

Comalco

Member
Licensed User
Longtime User
All sorted.

I didnt understand how the RETURN r.runMethod statement worked, and where it needed to sit. Thanks for your patience along the way Erel.
 
Upvote 0

WhataJeff

Member
Licensed User
Longtime User
In the original Post Comalco was looking for IP or MAC.

Looks like he found how to get the IP how about the MAC?
 
Upvote 0
Top