Reading data received via UDP

Just starting out? Need help? Post your questions and find answers here.
bsilver
User
User
Posts: 27
Joined: Tue Jun 27, 2023 3:36 pm

Reading data received via UDP

Post by bsilver »

I think I'm missing something really BASIC in trying to read a UDP message after a network event.

The loop that is supposed to be handling this:

Code: Select all

        
        ; Check for a UDP server event
        LockMutex(mutNetUDPReference)
        intEventNet = NetworkServerEvent(structNetUDPListener\intConnectionID)
        intConnectionID = structNetUDPListener\intConnectionID
        UnlockMutex(mutNetUDPReference)
        If intEventNet = #PB_NetworkEvent_Data
          
          Repeat
            NetBuffer = AllocateMemory(#ConstantIncomingNetBufferSize)
            intNetReadLen = ReceiveNetworkData(intConnectionID,
                                               NetBuffer,
                                               #ConstantIncomingNetBufferSize) ;; THIS IS THE LINE THAT APPEARS TO ERROR
            strNetMessageReceived = strNetMessageReceived + PeekS(NetBuffer, 
                                                                  -1, 
                                                                  #PB_UTF8)
            FreeMemory(NetBuffer)
          Until intNetReadLen <> #ConstantIncomingNetBufferSize
          
          ;; DEBUG for now send this to debug
          If CheckTreeEntries(intActiveMenuItem) = #TreeDebugWindow
            AddGadgetItem(String_Debug_Receive_Box, 
                          -1, 
                          RTrim(strNetMessageReceived, #LF$))
            strNetMessageReceived = ""
          EndIf
        EndIf
        
It doesn't crash right away; it seems to happen when I suspect the UDP packet comes in and it does try to read the message from the buffer. I marked in the source where my trace shows the error (it's translating to an "Abort signal", error code 6, and I don't know if by that line number it means that parameter is where it is dying or the ReceiveNetworkData() call is the crash and it just flagged it at the last parameter.

Can someone offer some insight to what's going on here?
User avatar
HeX0R
Addict
Addict
Posts: 1189
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Reading data received via UDP

Post by HeX0R »

NetworkServerEvent() uses the Servers ID, whereas ReceiveNetworkData() needs the CLIENTS ID (from EventClient()).
It's also not very good to allocate and free a memory buffer all the time, you might have to rethink about that approach.
bsilver
User
User
Posts: 27
Joined: Tue Jun 27, 2023 3:36 pm

Re: Reading data received via UDP

Post by bsilver »

That seems to be the fix! Thanks, HeX0R! I replaced intConnectionID in ReceiveNetworkData() with EventID() as the parameter and I started getting the UDP broadcast message!

Regarding the buffer part, could you elaborate on that? I was freeing it to prevent having garbage data being re-read or wasting memory, but you're saying this could cause problems?
User avatar
HeX0R
Addict
Addict
Posts: 1189
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Reading data received via UDP

Post by HeX0R »

Not problems, but it is ineffective.
You are always allocating the very same size, so why not do it once outside the loop and free it at the end?
Then get the string by its received length, via:

Code: Select all

PeekS(NetBuffer, intNetReadLen , #PB_UTF8 | #PB_ByteLength)
Post Reply