Finding IP address from NetworkClientID()?

Just starting out? Need help? Post your questions and find answers here.
pthien
Enthusiast
Enthusiast
Posts: 150
Joined: Sun Jun 29, 2003 9:39 pm

Finding IP address from NetworkClientID()?

Post 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
D'Oldefoxx
User
User
Posts: 30
Joined: Wed Aug 06, 2003 5:52 pm
Location: Florida, USA

Yes there is

Post 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. :)
Has-been Wanna-be (You may not like what I say, but it will make you think)
pthien
Enthusiast
Enthusiast
Posts: 150
Joined: Sun Jun 29, 2003 9:39 pm

Post 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
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post 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..)
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
aszid
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 01, 2003 8:38 pm
Location: California, USA
Contact:

Post 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.
--Aszid--

Making crazy people sane, starting tomorrow.
pthien
Enthusiast
Enthusiast
Posts: 150
Joined: Sun Jun 29, 2003 9:39 pm

Post by pthien »

Thank you Aszid, that is exactly what I was looking for, and it works perfectly!

-Phil
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Post Reply