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 UDP Client Error.

Other Parts Discussed in Thread: TM4C129ENCPDT

Hi,
IT-RTOS - 2.10.01.38
NDK 2.24.01.18
TM4C129ENCPDT

I have the interface to work properly with UDP server (eg UDP ECHO).

When I put it to work as a UDP client displays the following error message in the sendto function:

EHOSTUNREACH (65)

It seems he does not find a UDP server on the network, but the server is present on the network. code for the client is as follows:

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 server */
    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family = AF_INET;
    //servAddr.sin_addr.s_addr = inet_addr("192.168.1.82");
    inet_aton("192.168.1.82", &servAddr.sin_addr);
    servAddr.sin_port = htons(59974);

    client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (client < 0) {    // error
        System_printf("ERROR SOCKET: (%d)\n", fdError());
        System_flush();
        goto QUIT;
    }

    while (1) {

        GPIO_write(Board_LED0, Board_LED_OFF);
        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) { //
            System_printf("ERROR SENDTO: (%d)\n", fdError());
            System_flush();
            GPIO_write(Board_LED1, Board_LED_ON);
            goto QUIT;
        }

        bytes_read = recvfrom(client, buffer, buffSize, 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:
  
    if (client) {
        GPIO_write(Board_LED0, Board_LED_OFF);
        fdClose(client);
        Reset_Main();
    }

    fdCloseSession(TaskSelf());
}


Because it can not find the server on the network?

Regards

Aquino

  • Hi Aquino,

          Here's a working UDP client example that goes with the UDP echo example server. Let's make sure that this works for you first and then we can see what you're doing differently.

    7178.udpSendRecv.c

    Regards,

    Moses

  • Hi Moses,
    thanks for your help.
    The example udp client 7115.3857.udpSendRecv.c works with UDP echo!
    But when I walk the same example for my code, and begins to give sendto error 65 or error 6.
    From what I see, when I start UDPSocketClient before fdOpenSession, I have an open session. But closing the previously opened session and restart a new session with client udp. As I show in the code above. I think that's what makes you the error.
    I do not know how I can fix the problem, the error appears to have originated in fdOpenSession.

    Other questions:

    In the exemple that sent you there is #include <_stack.h>, where is this _stack.h file? In TI-RTOS 2.10.01.38 not find the respective file.

    Examples udp server uses #include <sys / socket.h> and the client uses include <ti / ndk / inc / netmain.h> what's the difference?

    How can I solve the problem reported in fdOpenSession?

    My code is more than a simple UDP client, so somewhere there is a fdOpenSession.

    I'll see what I can do more ....

    Regards
    Aquino
  • Hi,
    The error that existed in fdOpenSession, it was because the priority of UDPSocket_Client () function was higher than the priority of NDK functions.
    I lowered the priority to 1 and no longer gives this error. However still continues:

    [ID-1] stopping test. sendto returned -1 (error 65)

    Regards
    Aquino
  • Hi Moses,
    I know the source of the problem to:
    [ID-1] stopping test. sendto returned -1 (error 65).

    If you are running disabled DHCP, and a way of IP "static" the problem is present.
    To the problem disappear and communicate well the client with the server have to activate the DHCP client.

    This is if I have the newIpAddr function in "Network open hook" and "Network IP address hook" function "MyNetWorkIPAddHook "

    My question is why?
    How can a stable communication without DHCP?

    I need the client works with DHCP disabled !!!

    Another thing I observed was that removing the "BIND", does not communicate with the server. If is a client why to bind?

    Regards
    Aquino

  • Hi Aquino,

          Can I see your cfg file where you setup static IP? Also the bind code is workaround for a known issue. We have a bug filed for this that we're tracking internally.

    Regards,

    Moses

  • Hi Moses,
    The problem I was writing error Mask address of a variable. So I gave the error 65 "No route to host". This problem has been fixed.

    But the UDP client has to work with BIND connected, or else UDP client does not work!

    Regards
    Aquino