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: 278
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: 7577
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: 410
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.
Post Reply