Get the IP of the UDP sender?

Previous topic - Next topic

Moebius

Hello everyone,

My question is simple - what is the easiest way to get the IP address of the computer that sent your program a UDP message?
I could get the sending program to write it's IP address at the front of the message followed by, say a '|' and use SplitStr etc. to get the byte values from a string IP address and then use binary shifts to convert it into an integer IP address, but I was wondering if there is a simpler way.

Thanks.
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Schranz0r

look at :
ip$ = SOCK_GETIP$(ip%)
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

MrTAToad

That will only do a conversion from string to integer or from a known IP address

If you are using TCP/IP, then you can use NETGETIP$ for a given player index

However, if you are using UDP then there isn't an easy way - I send it in a message stream (along with other information) and store it in an array.

Moebius

#3
Fears confirmed...  There's just one problem with sending the IP address in the message - computers can have multiple IP addresses for different networks right?  I see a SplitStr happening...  Is there a simple way to use Inline C++ or something to deal with 'sockets'?  (yes, I know I just used 'simple' and 'C++' in the same sentence :whistle:)  I'll probably be assuming 'no' and try sending the result of NETGETIP$ along with the message.  Thanks for the quick response.  If anyone at all knows a simpler way, please share it by all means   :)
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Moebius

I think GLBasic uses Winsock, and the 'recvfrom' function (see http://msdn.microsoft.com/en-us/library/ms740120%28VS.85%29.aspx) seems to put the sender's IP address into a 'sockaddr' structure...  I think it would be best to go with putting the PC's IP addresses at the front of the UDP message.  Should I stick with "123.5.2.9|143.8.34.2|" etc. or is there a way to literally put in the IP address as a 4-byte integer at the start of the message string for SOCK_UDPSEND?
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

MrTAToad

You can convert IP string to an integer with SOCK_GETIP$ and then put them into an array for checking to see which one is actually used.

Moebius

I've ended up using SOCK_GETIP to convert the strings from SOCK_GETIP$ into integers, with these int ips at the front of the initial server finding udp messages.  It should be able to get the sender's IP if GLBasic uses winsock, but this method will work fine.
One last question, do all of GL's networking functions work in the same way on iOS?
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

MrTAToad


Moebius

Okay things seem to be working now - thanks for the help!
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Kitty Hello

Yes, all network commands are cross platform.