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.

RTOS/TM4C1294NCZAD: UDP sendto Broadcast address

Part Number: TM4C1294NCZAD

Tool/software: TI-RTOS

Hello,

I've just started working with TI-RTOS on the TM4C, and let me first say what a great job the TI development team has done with it.  I've done quite a lot of development work with eCos and FreeRTOS, and transitioning to TI-RTOS has been a remarkably pleasant experience.  I've ported a rather complex application to it with very few difficulties.  Well done.

I've got one problem, however, that I just can't seem to solve.  One of the requirements of this application is the occasional need to send a UDP packet to the global broadcast address (255.255.255.255), and no matter what I do, I've not just been able to get that to happen.  FreeRTOS uses a fairly similar UDP stack, and this code works fine under that OS (admittedly on a completely different processor with a completely different network driver).

Has anyone had any luck transmitting to global broadcast?  In case it's of interest, I can RECEIVE packets sent to the broadcast address, but they seem to just disappear when SENT to it.  I've wiresharked at the ethernet level, and there is absolutely nothing being transmitted.

Here, essentially is the code.  I've made minor changes from my operational code to remove some #define constants, but this is it in a nutshell.

I've tried running the debugger, and I don't really see anything happening that would drop the packet.

Has anyone had any luck transmitting to global broadcast?

Thanks for any pointers!
Ben

Void udpBroadcastHandler(UArg arg0, UArg arg1)
{
    int                bytesRcvd;
    int                bytesSent;
    int                status;
    int                server;
    fd_set             readSet;
    struct sockaddr_in localAddr;
    struct sockaddr_in clientAddr;
    struct sockaddr_in BroadcastAddr;
    socklen_t          addrlen;
    char               buffer[512];
    char               udp_write_buf[512];
    unsigned int       udp_write_buf_size;

    server = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (server == -1) {
        System_printf("Error: socket not created.\n");
        goto shutdown;
    }

    memset(&localAddr, 0, sizeof(localAddr));
    localAddr.sin_family = AF_INET;
    //Have also tried binding localaddr.sin_addr.s_addr to my IP address
    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    localAddr.sin_port = htons(65001);

    memset(&BroadcastAddr, 0, sizeof(BroadcastAddr));
    BroadcastAddr.sin_family = AF_INET;
    BroadcastAddr.sin_addr.s_addr = htonl(0xFFFFFFFF); //htonl not necessary since reverse byte order is ==, but it's not hurting anything
    BroadcastAddr.sin_port = htons(65002);

    status = bind(server, (struct sockaddr *)&localAddr, sizeof(localAddr));
    if (status == -1) {
        System_printf("Error: bind failed.\n");
        goto shutdown;
    }

    do {
        FD_ZERO(&readSet);
        FD_SET(server, &readSet);
        addrlen = sizeof(clientAddr);

        status = select(server + 1, &readSet, NULL, NULL, NULL);
        if (status > 0) {
            if (FD_ISSET(server, &readSet)) {
                bytesRcvd = recvfrom(server, buffer, UDPPACKETSIZE, 0,
                        (struct sockaddr *)&clientAddr, &addrlen);

                if (bytesRcvd > 0) {
                    System_printf("GotBytes!");
                    //Packet is received just fine whether it's sent directly to my IP address or to the broadcast address
                    if (buffer[0] == 0xAA) {/* Upstream Device Broadcast */
                        udp_write_buf_size = System_sprintf(udp_write_buf, "Alive");
                        //Send back to the IP Address that Broadcasted to us on 65002
                        //This works as expected
                        clientAddr.sin_port=htons(65002);
                        bytesSent = sendto(server, udp_write_buf, udp_write_buf_size, 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr));
                        if (bytesSent != udp_write_buf_size) {
                            System_printf("Error sending to Broadcast client\n");
                        }
                        //Also send back to the global Broadcast address in case we're on a different subnet
                        //This does not send anything, but bytesSent does equal udp_write_buf_size and thus it doesn't trigger the error
                        //The OS seems to believe that the packet was sent, even though it absolutely was not
                        bytesSent = sendto(server, udp_write_buf, udp_write_buf_size, 0, (struct sockaddr *)&BroadcastAddr, sizeof(BroadcastAddr));
                        if (bytesSent != udp_write_buf_size) {
                            System_printf("Error sending to Broadcast Address\n");
                        }
                        udp_write_buf_size = 0;
                             
                    
                    }
                }
            }
        }
    } while (status > 0);

shutdown:
    if (server > 0) {
        close(server);
    }
} //End Task