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

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1122
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

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

Post 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
nsstudios
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

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

Post 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.
PBJim
Enthusiast
Enthusiast
Posts: 294
Joined: Fri Jan 19, 2024 11:56 pm

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

Post 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.
Quin
Addict
Addict
Posts: 1122
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

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

Post 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?
Post Reply