Getting the IP address of a client machine?

Everything else that doesn't fall into one of the other PB categories.
pthien
Enthusiast
Enthusiast
Posts: 150
Joined: Sun Jun 29, 2003 9:39 pm

Getting the IP address of a client machine?

Post by pthien »

I posted this same question in the beginners forum, forgive me for asking it here but perhaps this isn't a beginner question.

I'm playing with the Atomic Web Server code that came w/ PureBasic. 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 find out what the IP address is if I have the results from NetworkClientID()?

Thanks,
Phil
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Post by sec »

127.0.0.1 (localhost)
pthien
Enthusiast
Enthusiast
Posts: 150
Joined: Sun Jun 29, 2003 9:39 pm

Post by pthien »

Yes, the server can be accessed by using "localhost" as the address in your browser, provided you are running both on the same PC.

But what if I were running the web server on one PC, and a browser on another. And further suppose I wanted to log the IP address of each machine that has accessed my web server via a browser.

How do I find those IP addresses?

Thanks,
Phil
AngelSoul
User
User
Posts: 55
Joined: Tue Jul 29, 2003 9:16 am
Location: Canada

Post by AngelSoul »

Here's an example how to get the remote IP on a connection.
GetpeerName_ will get the remote IP.
GetsockName_ will get the local IP it was connected on.
;-----------------------------------------

If InitNetwork()=0:MessageRequester("Error","Can't initialize the network",0):EndIf
If CreateNetworkServer(8181)=0:MessageRequester("Error","can't bind to port 8181",0):End:EndIf

Repeat
nn=NetworkServerEvent()
If nn=1 ;someone connected to your computer
cnid=NetworkClientID() ;get connection ID
Structure IPType
Reserved.w
Port.w
StructureUnion
IPLong.l
IP.b[4]
EndStructureUnion
Zeros.l[2]
EndStructure
length.l = SizeOf(IPType)
result.l = GetpeerName_(cnid, @IP.IPType, @length)
If result=0
remoteip$ = StrU(IP\IP[0],#Byte)+"."+StrU(IP\IP[1], #Byte)+"."
remoteip$ + StrU(IP\IP[2],#Byte)+"."+StrU(IP\IP[3], #Byte) ;+":"+StrU(IP\Port,#Word) ;remote port
Else
result = WSAGetLastError_()
EndIf

result.l = GetsockName_(cnid, @IP.IPType, @length)
If result=0
localip$ = StrU(IP\IP[0],#Byte)+"."+StrU(IP\IP[1], #Byte)+"."
localip$ + StrU(IP\IP[2],#Byte)+"."+StrU(IP\IP[3], #Byte) ;+":"+StrU(IP\Port,#Word) ;local port
Else
result = WSAGetLastError_()
EndIf
Debug remoteip$+" connected to your computer ("+localip$+")"

EndIf
ForEver
AMD 1.8 - 512mb - ATI All-In-Wonder Radeon 9000 pro - W2k Pro
BASIC programmers never die, they just return without gosub.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Might somebody update that wonderful code to PB4.0?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

perhaps this helps:

Code: Select all

If Not InitNetwork()
 MessageRequester("Error", "Can't initialize the network")
EndIf
If Not CreateNetworkServer(0, 8181)
 MessageRequester("Error", "Can't bind to port 8181")
 End
EndIf

Structure IPType
 Reserved.w
 Port.w
 StructureUnion
  IPLong.l
  IP.b[4]
 EndStructureUnion
 Zeros.l[2]
EndStructure

Repeat
NetworkEvent=NetworkServerEvent()
If NetworkEvent=1
 ClientID=EventClient()
 StructureSize=SizeOf(IPType)
 Result=GetPeerName_(ClientID, @IP.IPType, StructureSize)
 If Result=0
  RemoteIP$=StrU(IP\IP[0], #Byte)+"."+StrU(IP\IP[1], #Byte)+"."
  RemoteIP$+StrU(IP\IP[2], #Byte)+"."+StrU(IP\IP[3], #Byte)
 Else
  Result=WSAGetLastError_()
 EndIf
 Result=GetSockName_(ClientID, @IP.IPType, StructureSize)
 If Result=0
  LocalIP$=StrU(IP\IP[0], #Byte)+"."+StrU(IP\IP[1], #Byte)+"."
  LocalIP$+StrU(IP\IP[2], #Byte)+"."+StrU(IP\IP[3], #Byte)
 Else
  Result=WSAGetLastError_()
 EndIf
 Debug RemoteIP$+" connected to your computer ("+LocalIP$+")"
EndIf
ForEver
EDIT: REMEMBER TO NEVER EVER NEVER EVER insert structure declaration INSIDE a loop!!!!!
maw

Post by maw »

Why not use these instead??

Code: Select all

GetClientIP(Client)
GetClientPort(Client)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Yes, that helps, but i think maw proposition should be taken in account in order to update it to PB4.0 :?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

Psychophanta wrote:Yes, that helps, but i think maw proposition should be taken in account in order to update it to PB4.0 :?
I didn't push much to convert the source, so you could go ahead and do something :wink:
Post Reply