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.

UDP Socket Sendto Error .

Other Parts Discussed in Thread: EK-TM4C1294XL, SYSBIOS

TI-RTOS for TivaC 2.10.1.38

XDCtools 3.30.4.52

2 interfaces EK-TM4C1294XL

hi,
One of the interface are as UDP socket server (example UDPecho) and works well with udpSendReceive.exe.
The other interface is as UDP client socket to communicate with the UDP server (UDPecho), but gives an error in the function:

bytes_sent = sendto (client, buffer, buffSize, 0, (struct sockaddr *) & servAddr, sizeof (servAddr));

That is the function always returns -1.

The client code for the following:

#define NDK_UDPPORT 1025
#define SERVER_IP "192.168.1.133"
#define SLEEPTIME 1000
#define Sleep Task_sleep

Void echoFxnClient(UArg arg0, UArg arg1)
{
    int i;
    SOCKET client;
    int status = 0;
    int sleepTime = SLEEPTIME;
    struct sockaddr_in bindAddr;
    struct sockaddr_in servAddr;
    int bytes_read, bytes_sent;
    char *buffer;
    struct sockaddr_in fromAddr;
    int fromAddrLen = sizeof(fromAddr);
    fd_set readfds;
    struct timeval timeout;
    int count = 0;
    int id;
    time_t start;
    unsigned int buffSize  = MAXBUF;

    fdOpenSession(TaskSelf());

    buffer = malloc(buffSize);
    if (!buffer) {
        goto QUIT;
    }
    memset(buffer, 0, buffSize);

    /* initialize destination address */
    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family = AF_INET;
    inet_aton(SERVER_IP, &servAddr.sin_addr);
    servAddr.sin_port = htons(SERVER_UDPPORT);

    // # # # # 1) 
    client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    memset(&bindAddr, 0, sizeof(bindAddr));
    bzero((char *)&bindAddr,sizeof(bindAddr));
    bindAddr.sin_family = AF_INET;
    bindAddr.sin_addr.s_addr =  htonl(INADDR_ANY);
    bindAddr.sin_port = htons(NDK_UDPPORT);

    // # # # # 2) 
    if (bind(client, (struct sockaddr *)&bindAddr, sizeof(bindAddr)) < 0) {
        goto QUIT;
    }

    // # # # # 3) 
    /* loop */
    i = 0;
    while (1) {
        buffer[0] = (char)(++i);
        buffer[buffSize - 1] = (char)~i;

        /* send the data */
        bytes_sent = sendto(client, buffer, buffSize, 0, (struct sockaddr *)&servAddr, sizeof(servAddr));

        if (bytes_sent < 0 || bytes_sent != buffSize) {	//    #########  ERROR ############
        	GPIO_write(Board_LED1, Board_LED_ON);   // <--
            goto QUIT;
        }

        // # # # # 3B) 
        bytes_read = recvfrom(client, buffer, buffSize, MSG_WAITALL,
               (struct sockaddr *)&fromAddr, &fromAddrLen);

        if (bytes_read < 0 || bytes_read != buffSize) {	// Se recebe a mensagem
        	GPIO_write(Board_LED1, Board_LED_OFF);
            goto QUIT;
        }
        else {
        }

        count++;
        if (count % 1000 == 0) {
        }

        /* Sleep specified time */
        Sleep(sleepTime / 1000);

        if ((buffer[0] != (char)i) || (buffer[buffSize - 1] != (char)~i)) {
        }
    }

QUIT:
	// # # # # 5) 
    if (client) {
        fdClose(client);
    }
    free(buffer);
    fdCloseSession(TaskSelf());
}

My doubt is because sendto returns -1?

 

Regards

Aquino

  • Hi Aquino,

    Can you add a call to fdError() after sendto() and check the error type ? fdError() will return the error code. The errors are defined in "NDK_INSTALL_DIR\packages\ti\ndk\inc\serrno.h" file.

    Best,
    Ashish
  • Hi Ashish,
    thank you for your response.

    The error it gives is the following:

    ERROR: (6)
    ti.sysbios.family.arm.m3.Hwi: line 1036: E_hardFault: FORCED
    ti.sysbios.family.arm.m3.Hwi: line 1113: E_busFault: PRECISERR: Immediate Bus Fault, exact addr known, address: fffffffd
    Exception occurred in background thread at PC = 0x00001958.
    ...

    According to serro.h:
    ENXIO 6 /* Device not configured */

    Device not configured? What's missing?


    Regards
    Aquino
  • Hi Aquino,

    Let me check what all issues can cause Error 6 and get back. I have some more questions and comments that I have shared below.

    Aquino said:

    The error it gives is the following:

    ERROR: (6)
    ti.sysbios.family.arm.m3.Hwi: line 1036: E_hardFault: FORCED
    ti.sysbios.family.arm.m3.Hwi: line 1113: E_busFault: PRECISERR: Immediate Bus Fault, exact addr known, address: fffffffd
    Exception occurred in background thread at PC = 0x00001958.
    ...

    The last 3 lines are part of an exception dump. Were you getting an exception before also or this is new ? Can you share the complete exception dump and also check what function is running at 0x00001958 ?

    I also wanted to point out that bind() is not required. I see you are calling it from within the client.

    Best,

    Ashish

  • Hi Ashish
    I already have the problem resolved relatively.

    In exception dump I changed the printf by system_printf ...

    The though BIND is in the code, bindAddr is never called. Already removed the code to avoid problems.

    To resolve the error "6" in sendto in the .cfg file had to by:
    Ip.address = "ipaddress";
    Ip.mask = "255.255.255.0";
    Ip.ifIdx = 1;

    So if I have the active DHCP and not know my ip how I communicate as a client?

    Because I had to say in .cfg I have that ip? I'm not understand!

    Is there any other solution?

    Why is the operation in this mode?

    Regards
    Aquino

  • Hi Aquino,

    I believe all NDK apps use DHCP to determine their IP address by default and therefore you should set IP.address to "".

    Do you have a DHCP server in your setup ? If not, then the app may not be getting an IP address and hence the error.

    Best,
    Ashish
  • Hi Ashish,
    Thank you for your answer.
    I have DHCP enabled on the interface. The problem would not be there.
    Now I can connection (send sockets) from client to server, but the server to the client the most part does not receive the socket. So in another question I did:

    e2e.ti.com/.../422233

    I asked if it was posivel UDP socket non-blocking.

    regards
    Aquino