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
Getting the IP address of a client machine?
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
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
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
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.
BASIC programmers never die, they just return without gosub.
- Psychophanta
- Always Here

- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
perhaps this helps:
EDIT: REMEMBER TO NEVER EVER NEVER EVER insert structure declaration INSIDE a loop!!!!!
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- Psychophanta
- Always Here

- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:


