This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TI-RTOS socket UDP non-blocking

Other Parts Discussed in Thread: TM4C129ENCPDT

hi,
is there any way by working communication UDP socket (client) non-blocking?

I tried with:

sockblock = ioctl(client, FIONBIO, 1);

but gave error in FIONBIO.

* * *

Micro:TM4C129ENCPDT

TI-RTOS: 2.12.1.33

XDC-Tools: 3.31.1.33_core

Compiler-TI V5.2.4

Regards
Aquino

  • Ok for the socket is non-blocking in recvfrom have to go MSG_DONTWAIT. Thus:

    bytes_read = recvfrom(client, buffer, buffSize, MSG_DONTWAIT,
    (struct sockaddr *)&fromAddr, &fromAddrLen);

    Now give me the error:
    ERROR Recvfrom: (35)

    regards
    Aquino
  • Is 35 the returned value from recvfrom() or from errno?

    If it is errno, then 35 (EWOULDBLOCK) is expected when recvfrom() is used in non-blocking mode (i.e. with MSG_DONTWAIT flag). You can call recvfrom() again to retrieve the remaining bytes.

    Vikram
  • Hi Vikram Adiga
    Thank you for your response.
    35 is returned from errno. Means that the client does not receive the socket from the server.
    I have also called recvfrom again (3 times) but always returns "error 35". Always resumes value 35. I do not see how to solve. If you by pc (as client) behavior is already correct.

    Regards
    Aquino
  • Hi Aquino,

    From the NDK Programmers Guide, a successful operation for recvfrom() is described as:

    The function returns the length of the message on successful completion.
    For a datagram type socket, the receive operation always copies one packet's worth of
    data. If the buffer is too short to hold the entire packet, the data is truncated and lost.
    If no messages are available at the socket, it waits for a message to arrive, unless the
    socket is non-blocking. The function normally returns any data available, up to the
    requested amount, rather than waiting for receipt of the full amount requested; this
    behavior is affected by the options specified in flags as well as the socket-level options
    SO_RCVLOWAT and SO_RCVTIMEO described in getsockopt().

    Can you check if the buffer is sufficient to hold the packet and no data is lost? 

    You may have to try calling recvfrom() in a loop (just for testing) till you receive some data to make sure that you are indeed receiving some data.

    Vikram 

  • Hi Vikram

    The buffer looks fine, but already increased.
    I have created a loop on recvfrom () but still give the same error.
    I think the real problem is the customer does not receive the response from the server. As never get nothing for MSG_WAITALL the function blocks, you will always be waiting to receive a socket.
    Still unsolved.

    regards
    Aquino
  • If your client is not able to get the data with MSG_WAITALL flag from server, then there must be some problem with the server. Does the server respond when you ping it from your PC?

    The other thing I would suggest would be to setup Wireshark to view the client and server transactions.

    Vikram
  • Hi Vikram
    Yes when I put the client on the PC, the server responds well. And since checked with Wireshark and see the sockets. The problem happens when the client is another interface, can send the socket to the server, the server receives and sends the answer. That response never reaches the client. I think it has to do with the time that the server sends the socket and the client is ready to receive. Is there any timeout function?

    regards
    Aquino
  • Are you asking if there is a timeout value set for the socket? I believe there is a standard timeout value of 10 seconds which will cause an error if timed out. But from your earlier posts the error code is EWOULDBLOCK which is not actually an error.

    Vikram
  • Hi Vikram,
    Ok the problem is even if I am not receiving a response from the server. Only gives EWOULDBLOCK because MSG_DONTWAIT. But if by MSG_WAIT, is in an infinite loop waiting for the socket that never comes. But never gives any error more.
    The server works fine as I said, because I can communicate as PC client. The other interface as a client is receiving no response from the server.
    I honestly do not know what I can do more...

    .
    Aquino
  • Hi Aquino,

    I would suggest capturing Wireshark for the code that you run on the PC and for the code that you run on the target to compare the differences. It should provide you more info.

    Vikram
  • Hi Vikram,

    The PC code (running) is the TI udpSendReceive.exe for example, or else the Hercules.exe software..

    The client user interface code is as follows:

    #include <ti/ndk/inc/netmain.h>
    ...
    
    unsigned int SERVER_UDPPORT = 1000;
    #define UDPPACKETSIZE 256
    #define MAXBUF 256
    #define SERVER_IP "192.168.1.100"
    
    void UDPSocket_Client (UArg a0, UArg a1)
    {
    	    SOCKET client;
    	    struct sockaddr_in servAddr;
       	    int bytes_read;
    	    int bytessent;
       	    struct sockaddr_in fromAddr;
       	    int fromAddrLen = sizeof(fromAddr);
    	    unsigned int buffSize  = MAXBUF;
    	    char buffer[MAXBUF];
    	    int fdOpenResult = 0;
    
    	    System_printf ("UDP CLIENT.\n");
    	    System_flush();
    
    	    fdOpenResult = fdOpenSession(TaskSelf());
    	    while(fdOpenResult == 0)
    	    {
    	        System_printf ("fdOpenSession failed\n");
    	        System_flush();
    	        fdCloseSession(TaskSelf());
    	        Task_sleep(5);
    	        fdOpenResult = fdOpenSession(TaskSelf());
    	    }
    
    	    /* initialize destination address IP SERVIDOR */
    	    memset(&servAddr, 0, sizeof(servAddr));
    	    servAddr.sin_family = AF_INET;
    	    servAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
    	    servAddr.sin_port = htons(SERVER_UDPPORT);     
    
    	    // # # # # 1)
    	    client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    	    if (client < 0){    // error
    	    	System_printf("ERROR SOCKET: (%d)\n",fdError());
    	    	System_flush();
    	    	goto QUIT;
    	    }
    
    	    // # # # # 2) 
    
    	    // # # # # 3) 
    	    /* loop */
    	    Task_sleep(5000);	
    
    	    while (1) {
    
    	    	GPIO_write(Board_LED0, Board_LED_OFF);
    
    	        /* send the data */
    	        GPIO_write(Board_LED1, Board_LED_ON);
    	        Task_sleep(50);
    	        GPIO_write(Board_LED1, Board_LED_OFF);
    	        bytessent = sendto(client, buffer, buffSize, 0, (struct sockaddr *)&servAddr, sizeof(servAddr));
    	        if (bytessent < 0 || bytessent != buffSize) { // || bytessent != buffSize
    	        	System_printf("ERROR SENDTO: (%d)\n",fdError());
    	        	System_flush();
    	        	GPIO_write(Board_LED1, Board_LED_ON);
    	            goto QUIT;
    	        }
    
    
    	        // # # # # 3B)
    	        bytes_read = recvfrom(client, buffer, buffSize, MSG_DONTWAIT, //MSG_WAITALL or MSG_DONTWAIT
    	               (struct sockaddr *)&fromAddr, &fromAddrLen);
                if (bytes_read < 0 ) {       //  || bytes_read != buffSize
                    System_printf("ERROR RECVFROM: (%d)\n",fdError());
                    System_flush();
                    //goto QUIT;
                }
    	        else {
    	            System_printf ("Received %d bytes\n", bytes_read);
    	            System_flush();
    	        }
    
    	        GPIO_write(Board_LED0, Board_LED_ON);
    
    	    }
    
    	QUIT:
    	        // # # # # 5)
    	    if (client) {
    	    	GPIO_write(Board_LED0, Board_LED_OFF);
    	        fdClose(client);
    	        Reset_Main();
    	    }
    
    	    fdCloseSession(TaskSelf());
    }
    

    I do not know if it helps to detection of an error that I am committing.

    Thank you for trying to help me.

    Regards

    Aquino

  • Hi Aquino,

    I don't see anything obvious that is missing. Is the the network up before this task "UDPSocket_Client" is executed? What do you see in Wireshark? Can you see the send/recv packets?

    Vikram