Page 1 of 1

GetClientIP may return the gateway IP address when the client is on the same local network but not the same machine.

Posted: Sun Apr 20, 2025 3:53 pm
by Quin
To reproduce:
Run this server on one machine:

Code: Select all

EnableExplicit

OpenConsole("Server test")
If CreateNetworkServer(0, 10987)
  Repeat
    Define Event.i = NetworkServerEvent()
    If Event
      Define ClientID.i = EventClient()
      PrintN(IPString(GetClientIP(ClientID)))
    EndIf
  ForEver
  End
EndIf
And run this client on another:

Code: Select all

EnableExplicit

Define ConnectionID.i = OpenNetworkConnection("ServerIP", 10987)
If ConnectionID
MessageRequester("Client test", "Client connected to server...")
CloseNetworkConnection(ConnectionID)
EndIf
End
You'll have to replace the IP of the client to match whatever your router is okay with, I don't think 127.0.0.1 will work because they're on different machines, but the same LAN.
Whenever I run this, it returns the IP address of my gateway, not the actual IP address of the client. Can anyone else confirm?
Note, because I was lazy when I coded the server you'll sometimes see 0.0.0.0, just ignore those :D

Re: GetClientIP may return the gateway IP address when the client is on the same local network but not the same machine.

Posted: Mon Apr 21, 2025 4:09 pm
by nsstudios
Edit: tried with pb 6.21 beta 4, Win 10.
If by gateway IP you mean something like 192.168.0.1, I can't reproduce that. I ran server on 192.168.0.24, and ran client from .14, when I run the client, server says 192.168.0.14. I tried multiple times, and I set the client IP address for createServer to 192.168.0.24.

Re: GetClientIP may return the gateway IP address when the client is on the same local network but not the same machine.

Posted: Tue Apr 22, 2025 7:46 am
by PBJim
This is not caused by PureBasic — it's because of the way your network is configured. Your router is performing NAT on the connection from the client to the server, hence you see the gateway address as the connecting client.

The code that you've shown works in a properly configured network and returns the connecting client's address. Tested here on LAN and on public-facing IP. Tried Windows PB 6.00 64-bit and PB 6.20 64-bit.

I don't think this is a PureBasic bug.

Re: GetClientIP may return the gateway IP address when the client is on the same local network but not the same machine.

Posted: Sun May 04, 2025 12:13 pm
by Quin
PBJim wrote: Tue Apr 22, 2025 7:46 am This is not caused by PureBasic — it's because of the way your network is configured. Your router is performing NAT on the connection from the client to the server, hence you see the gateway address as the connecting client.

The code that you've shown works in a properly configured network and returns the connecting client's address. Tested here on LAN and on public-facing IP. Tried Windows PB 6.00 64-bit and PB 6.20 64-bit.

I don't think this is a PureBasic bug.
You're correct, my bad. Both networks I tried this on performed NAT. Just tried it without though, and it works as expected. :mrgreen:
It is slightly unusual for a home network to perform NAT on internal traffic, but it's certainly possible. Wonder why its the default though?