Network server, UDP - getting client IP (windows)

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Network server, UDP - getting client IP (windows)

Post by jassing »

Is there a way (api or otherwise) to get the client IP of the client when sending data to a UDP server?

Code: Select all

IPString(GetClientIP(clientID))
returns ""
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Network server, UDP - getting client IP (windows)

Post by infratec »

Server:

Code: Select all

If CreateNetworkServer(0, 12345, #PB_Network_IPv4|#PB_Network_UDP, "127.0.0.1")
  
  *Buffer = AllocateMemory(1024)
  
  Repeat
      
    ServerEvent = NetworkServerEvent()
  
    If ServerEvent
    
      ClientID = EventClient()
  
      Select ServerEvent
      
        Case #PB_NetworkEvent_Data
          Len = ReceiveNetworkData(ClientID, *Buffer, MemorySize(*Buffer))
          Rcv$ = PeekS(*Buffer, Len, #PB_UTF8|#PB_ByteLength)
          Msg$ = "Client " + ClientID + " has send a packet !" + #LF$
          Msg$ + "IP: " + IPString(GetClientIP(ClientID)) + ":" + Str(GetClientPort(ClientID)) + #LF$
          Msg$ + "Received: " + Rcv$
          MessageRequester("Info", Msg$)
          If Rcv$ = "quit"
            Quit = #True
          EndIf
          
      EndSelect
    EndIf
    
  Until Quit

EndIf
Client:

Code: Select all

Con = OpenNetworkConnection("127.0.0.1", 12345, #PB_Network_IPv4|#PB_Network_UDP)
If Con
  SendNetworkString(Con, "quit", #PB_UTF8)
  CloseNetworkConnection(Con)
EndIf
Works here. (Win10 x64 with PB 6.01 x32):
Client 37488632 has send a packet !
IP: 127.0.0.1:52018
Received: quit
But why have I to write the test code?

If you provide code for tests, then 0.5 hours are lost.
If all potential helperes have to write the code, then 0.5 * X hours are lost.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Network server, UDP - getting client IP (windows)

Post by infratec »

Also works with PB 6.01 x64.

I had to test this, because you forgot to write which exact version of PB you are using.

Btw. I tested only the assembler backend.
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: Network server, UDP - getting client IP (windows)

Post by jassing »

Thanks -- I'll dig into it further. Must be something with my system.

Code: Select all

If CreateNetworkServer(0, 500, #PB_Network_UDP)
  *Buffer = AllocateMemory(1024)
  Repeat
    ServerEvent = NetworkServerEvent()
    If ServerEvent
      ClientID = EventClient()
  ; << <here is where I attempt grab the IP...
On a win10 pb 6.01x64 system, it's always empty for UDP. (0.0.0.0 for tcp/disconnect)
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Network server, UDP - getting client IP (windows)

Post by infratec »

You can only get the IP if a #PB_NetworkEvent_Data event arrived.
You have to check for this event first.
Post Reply