Page 1 of 1
Getting the IP address of a client machine?
Posted: Mon Aug 11, 2003 8:10 pm
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
Posted: Tue Aug 12, 2003 3:35 am
by sec
127.0.0.1 (localhost)
Posted: Tue Aug 12, 2003 3:39 am
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
Posted: Wed Aug 13, 2003 7:16 am
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
Posted: Fri May 19, 2006 9:45 pm
by Psychophanta
Might somebody update that wonderful code to PB4.0?
Posted: Fri May 19, 2006 10:19 pm
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!!!!!
Posted: Fri May 19, 2006 10:29 pm
by maw
Why not use these instead??
Code: Select all
GetClientIP(Client)
GetClientPort(Client)
Posted: Fri May 19, 2006 11:07 pm
by Psychophanta
Yes, that helps, but i think maw proposition should be taken in account in order to update it to PB4.0

Posted: Sat May 20, 2006 7:34 am
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
