Page 1 of 1
Finding IP address from NetworkClientID()?
Posted: Mon Aug 11, 2003 3:29 pm
by pthien
I want to know the IP address associated with a machine connected to a PureBasic server socket. That way I can log hits to the Atomic Web Server with the IP address of the machine making the request. Is there a way to do this?
Thanks,
Phil
Yes there is
Posted: Mon Aug 11, 2003 7:24 pm
by D'Oldefoxx
The simplest way is to shell to DOS and use the PING command, which should be somewhere along your path. Noet everyone realizes that the PING command will accept either an IP address or a URL name and return information so that you know how they translate. If will also let you know if you can currently reach the target, and just how far away it is in terms of the time to send a ping and receive a reply. Most versions of PING repeat this four times to help you gauge the stability of the connection as well.
Instead of shelling out of your program this way, you can write your own PING process within PureBASIC. In fact, that is one of the many example programs provided when you visit the various PureBASIC forums. It might pay you to follow the links available at
www.purebasic.com and search for code snippits, which are not intended to be complete programs in their own right, but which illistrate a capability or approach to solving problems such as this.

Posted: Mon Aug 11, 2003 7:30 pm
by pthien
Well, I'm hoping it doesn't get as complicated as that.
PureBasic must already know the IP address of the machine connected with the associated NetworkClientID(). I just want to find out from the library what I'm betting it already knows.
Thanks,
Phil
Posted: Mon Aug 11, 2003 7:46 pm
by Karbon
Ping does accept IPs or hostnames and can be used for name -> IP resolution but I don't think tha's what he was asking.. He wants to know how you get the IP address of someone connecting to the server.. I'd say you'd use getsockopt_() on the connected socket but I'll have to take some time to get a working example out.. I haven't really messed with the API functions that much..
Also it looks like winsock has a gethostbyname/gethostbyaddr functions so you'd probably use those to resolve hostnames and IP addresses (instead of shelling out and using ping then parsing the results..)
Posted: Tue Aug 12, 2003 11:59 pm
by aszid
try this:
Structure IPType
Reserved.w
Port.w
StructureUnion
IPLong.l
IP.b[4]
EndStructureUnion
Zeros.l[2]
EndStructure
Length.l = SizeOf(IPType) ;get ip
result.l = GetPeerName_(userid, @IP.IPType, @Length)
If result = 0
ip$ = StrU(IP\IP[0],#Byte)+"."+StrU(IP\IP[1], #Byte)+"."
ip$ + StrU(IP\IP[2],#Byte)+"."+StrU(IP\IP[3], #Byte) ;+":"+StrU(IP\Port,#Word) would be hostname
Else
result = WSAGetLastError_()
ip$ = "0.0.0.0"
EndIf
i got this from somewhere else in the forums, but i couldn't find it easily...
i have made a few modifications to it as well, originally it would find the ip as well as the hostname.
anyhow, hope this is what you were lookin for.
Posted: Wed Aug 13, 2003 1:12 am
by pthien
Thank you Aszid, that is exactly what I was looking for, and it works perfectly!
-Phil
Posted: Wed Aug 13, 2003 1:28 am
by Flype
hello, is this code useful for you ?
This one make use of the API win32 and the IPHlpApi lib. Should work everywhere... can i have feedback about compatibility ?
Code: Select all
#MAX_IP = 5
Structure IPINFO
dwAddr.l
dwIndex.l
dwBCastAddr.l
dwReasmSize.l
unused1.l
unused2.l
EndStructure
Structure MIB_IPADDRTABLE
dwEntries.l
mIPInfo.IPINFO[ #MAX_IP ]
EndStructure
; #TRUE or #FALSE --> Sort the ip table or not !
Ret.l
GetIpAddrTable_( #NULL, @Ret, #TRUE )
GetIpAddrTable_( @test.MIB_IPADDRTABLE , @Ret, #TRUE )
For i=0 To test\dwEntries - 1
Debug IPString( test\mIPInfo[i]\dwAddr )
Debug Hex( test\mIPInfo[i]\dwBCastAddr )
Debug test\mIPInfo[i]\dwReasmSize
Debug "---------------------------------"
Next