Problem with UDP server

Hello.

I am trying to make a function that receives a frame in UDP protocol and responds with another one.

The problem is that with the 6 or 7 first trams, there is an answer, but from these, it stops responding.

I have verified that after receiving the 6 or 7 first frames, the function

status = nx_udp_socket_receive (& socket_echo, & sDrivers.packet_ptr, NX_WAIT_FOREVER);

does not return.

I enclose my code in case someone can check it and tell me where the problem may be.

Thanks.

 

Regards

Fernando

3051.UDP.txt
/* Http Server Thread entry function UDP */
void http_server_thread_entry(void)
{
    UINT status;
    ULONG flags;
    NX_TCP_SOCKET  socket_echo;
//    NX_PACKET       *packet_ptr;
    CHAR    *pointer;

    ULONG           source_ip_address;
    UINT           source_udp_port;

        flags=0;

        status = nx_http_server_start(&g_http_server0);
        if(status)
        {
             err_counter++;
        }

        // Creo socket UDP
        /* Create a socket.  */
        status =  nx_udp_socket_create(&g_ip0, &socket_echo, "Echo Server Socket",
                                    NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 512);

        /* Check for error.  */
        if (status)
            err_counter++;

        status =  nx_udp_socket_bind(&socket_echo, 7, NX_WAIT_FOREVER);
        /* Check for error.  */
        if (status)
            err_counter++;


        /* TODO: add your own code here */
        while(1)
        {
            /* Receive a UDP message from the socket.  */
            status =  nx_udp_socket_receive(&socket_echo, &sDrivers.packet_ptr, NX_WAIT_FOREVER);

            /* Check for error.  */
            if (status)
            {
                /* most likely got disconnected */
                err_counter++;

            }
            else
            {

                nx_udp_source_extract(sDrivers.packet_ptr, &source_ip_address, &source_udp_port);

                vTratComEth();

                //                    memcpy(sDrivers.packet_ptr->nx_packet_prepend_ptr,sDrivers.send_buffer,sDrivers.niLong);
                //                    sDrivers.packet_ptr->nx_packet_length=sDrivers.niLong;


                // Sumo la cabecera
                sDrivers.niLong = sDrivers.niLong + 9;

                /* Allocate a packet.  */
                status =  nx_packet_allocate(&g_packet_pool0, &sDrivers.packet_ptr, NX_TCP_PACKET, NX_WAIT_FOREVER);
                /* Check for error.  */
                if (status)
                    err_counter++;

                nx_packet_data_append(sDrivers.packet_ptr, sDrivers.send_buffer, sDrivers.niLong, &g_packet_pool0,
                                        NX_WAIT_FOREVER);

                /* Echo back the received data this will also release the packet */
                status =  nx_udp_socket_send(&socket_echo, sDrivers.packet_ptr, source_ip_address, source_udp_port);
                /* Check for error.  */
                if (status) {
                    status =  nx_packet_release(sDrivers.packet_ptr);
                    err_counter++;
                    break;
                }

            }
        }
}

  • Problem solved.

    I call the function
    status = nx_packet_release(sDrivers.packet_ptr);
    before calling the function
    status = nx_packet_allocate(&g_packet_pool0, &sDrivers.packet_ptr, NX_TCP_PACKET, NX_WAIT_FOREVER);

    Thanks
  • In reply to feraro:

    the pool is overflowed. so there were no room for new packets.
  • In reply to marinayelken:

    Hi Fernando,

    If you successfully receive the packet, you should release it when it's no longer needed. You don't have to release the packet if it has been successfully sent.

    Regards,
    adboc
  • In reply to adboc:

    Hi adboc.

    Thanks for your help.

    I do not understand very well what you tell me. Do you see that I'm doing something incorrectly? With this solution I have tested my communications and they work perfectly for me.

    Best Regards
    Fernando
  • In reply to feraro:

    Hi Fernando,

    You said you called nx_release_packet before nx_packet_allocate. This may work fine in your case, however I wonder if this is the right way. I'm just thinking of the recommended way of handling NetX packets:

    Sending a packet:
    1) allocate a packet using nx_packet_allocate
    2) send a packet (e.g. nx_tcp_socket_send, nx_udp_socket_send)
    3) if it has been successfully sent, nothing more has to be done, if not, call nx_packet_release

    or

    Receiving a packet:
    1) receive a packet (note here you should not allocate a packet when using functions like nx_tcp_socket_receive, nx_udp_socket_receive)
    2) if it has been successfully received, process it/extract data/etc. and call nx_packet_release, if not, nothing more has to be done

    This will prevent from leaking NetX packets.

    Regards,
    adboc
  • In reply to adboc:

    Hi adboc

    I will study how to do it following your recommendation. Thank you very much once more

    Regards
    Fernando
  • In reply to adboc:

    : Thanks for the tips above. Followed them as is and it all works very stable. Saved me a lot of troubleshooting.