Linux UDP server

Linux specific forum
kake26
Enthusiast
Enthusiast
Posts: 157
Joined: Sun Jan 25, 2004 7:21 pm
Contact:

Linux UDP server

Post by kake26 »

Hi all,

So I'm trying to make something that involves UDP for certain things. I have been at this all day and I'm not sure what might be the issue here. I've attempted to distill this down to just listening on a port and spitting out whatever text is sent. I keep getting invalid memory access.

Code: Select all

; handwritten UDP mini

#ADDR = "127.0.0.1" 
#PORT = 5556
#BUFFER_SIZE = 1024

*buffer = AllocateMemory(#BUFFER_SIZE)
socket = CreateNetworkServer(#PB_Any, #PORT, #PB_Network_UDP)

  Repeat
     event = NetworkServerEvent()
    If event = #PB_NetworkEvent_Data
       bytes = ReceiveNetworkData(socket, *buffer, #BUFFER_SIZE)
      
        message.s = PeekS(*buffer, bytes)
        
        Debug message
        Debug Str(bytes)
      
    EndIf
    Delay(10)
  ForEver
Now I attempt to test it by using netcat to send a simple message, I use the following command.

Code: Select all

echo -n "JOIN lab1 ALL 127.0.0.1" | nc -u  127.0.0.1 5556
I'm not sure what is going on here or if I perhaps discovered a bug. I've tried setting addr to 0.0.0.0 as well and no luck. I'm aware the max UDP buffer is 2048 as well so the one I created is smaller. I've looked on here as well, no real luck either.

I'm running Kubuntu 25.04, but everything else as far as purebasic goes seems to work just fine. If anyone has any clues I'd appreciate it. Though part of me suspects its a case of I overlooked something again.
PeDe
Enthusiast
Enthusiast
Posts: 333
Joined: Sun Nov 26, 2017 3:13 pm

Re: Linux UDP server

Post by PeDe »

You must use the connection from EventClient() for ReceiveNetworkData().

Peter
infratec
Always Here
Always Here
Posts: 7711
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Linux UDP server

Post by infratec »

And your PeekS() is wrong.
I think it should be:

Code: Select all

; handwritten UDP mini

#ADDR = "127.0.0.1" 
#PORT = 5556
#BUFFER_SIZE = 1024

*buffer = AllocateMemory(#BUFFER_SIZE)
If *buffer
  server = CreateNetworkServer(#PB_Any, #PORT, #PB_Network_UDP, #ADDR)
  If server
    
    Repeat
      event = NetworkServerEvent(server)
      Select event
        Case #PB_NetworkEvent_Data
          Client = EventClient()
          bytes = ReceiveNetworkData(Client, *buffer, #BUFFER_SIZE)
          Debug Str(bytes)
          If bytes > 0
            message.s = PeekS(*buffer, bytes, #PB_UTF8|#PB_ByteLength)
            Debug message
          EndIf
          
        Case #PB_NetworkEvent_None
          Delay(10)
      EndSelect
    ForEver
    
    CloseNetworkServer(server)
  EndIf
  FreeMemory(*buffer)
EndIf
miso
Enthusiast
Enthusiast
Posts: 592
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Linux UDP server

Post by miso »

UDP is the only networking I did. I don't know, what your goal is, but You might receive anything from the world, that is not related to your work.
I would add simple message system for recognizing valid packets/clients/ips. After that, a simple periodic keepalive system, that would punch trough NAT. (to not let the recognized clients router to free the clients port because of inactivity) After that a close or disconnect system. (releasing your recognized clients)

After all of these can be added any other messaging. Plan your packets, add a header that will tell what's inside it, how to read the data. Do not process data that is not recognized, or has corrupted or not known header part/IDs.

UDP is not easy to use, but very powerful.
kake26
Enthusiast
Enthusiast
Posts: 157
Joined: Sun Jan 25, 2004 7:21 pm
Contact:

Re: Linux UDP server

Post by kake26 »

Okay, thanks. Building a system where I want to have programs be able to find each other locally. UDP broadcasting seemed ok. I can't believe I missed that. Not even little AI help caught that. I swear there are times when things go crazy. Yes, I feel little silly on this one. I've added the fix to my work and it works now. Lesson learned via repeated segfaults. Thank you everyone.
miso
Enthusiast
Enthusiast
Posts: 592
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Linux UDP server

Post by miso »

For me, this connection thing is still strange in PB. I still would like to have things like senddata(IP, Port) without creating a PB "connection" with UDP.
For example on LAN in a game 1 player is a client and a host also, the other client should be able to send an "are you there?" packet to scan for example 192.168.0-2,x range for the host, hoping in a reply.
infratec
Always Here
Always Here
Posts: 7711
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Linux UDP server

Post by infratec »

:?: :?: :?:

To whom you will send your data?
You need to open a connection to it.
Even is it not a real connection, the OS needs to know to whom it should send the packet.
Same in every language.

You need to write a procedure SendData().

Or you need to get more familar with networking, than you can also send a broadcast, in hope that someone receives it.
Last edited by infratec on Mon Dec 15, 2025 8:53 am, edited 1 time in total.
miso
Enthusiast
Enthusiast
Posts: 592
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Linux UDP server

Post by miso »

infratec wrote: Mon Dec 15, 2025 8:37 am To whom you will send your data?
To an IP and Port.
infratec
Always Here
Always Here
Posts: 7711
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Linux UDP server

Post by infratec »

You need to open a connection to it.
Even is it not a real connection, the OS needs to know to whom it should send the packet.
Same in every language.
infratec
Always Here
Always Here
Posts: 7711
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Linux UDP server

Post by infratec »

Code: Select all

Procedure.i SendUDPString(IP$, Port.i, Text$)
  
  Protected.i Con, Result
  
  
  Con = OpenNetworkConnection(IP$, Port, #PB_Network_UDP)
  If Con
    Result = SendNetworkString(Con, Text$)
    CloseNetworkConnection(Con)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure
Where is the problem?
miso
Enthusiast
Enthusiast
Posts: 592
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Linux UDP server

Post by miso »

Thank you Infratec. Yes, I would not say I'm pro or something. Can I send 3 different packet to 3 different IP/port? Meanwhile listening to answers. (Before any other packet arrives from those 3 to an opened socket, but other sources might be relevant?) I'm not used to this, but I accept that my knowledge is limited.
miso
Enthusiast
Enthusiast
Posts: 592
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Linux UDP server

Post by miso »

So I mean to open a socket without connect(). I think thats what CreateNetworkServer() does with UDP. That way recvfrom() can receive from anywhere, and sendto() can send to anywhere. Without connect(), to any ip:port.

Theres no problem though.
User avatar
skinkairewalker
Addict
Addict
Posts: 810
Joined: Fri Dec 04, 2015 9:26 pm

Re: Linux UDP server

Post by skinkairewalker »

The problem with this code is that all incoming UDP packets are processed sequentially in a single thread, which can cause delays under high load.
miso
Enthusiast
Enthusiast
Posts: 592
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Linux UDP server

Post by miso »

I regretted a bit my first post here. The way UDP server-client is handled in PB is commonly used, and I'm fine with those. Sorry if that post caused conflicts. I'm out. This talk should not exist under the linux topic. Again, sorry.
Post Reply