UDP broadcast

UDP broadcast

Home Forums Networking Software UDP broadcast

This topic contains 1 reply, has 2 voices, and was last updated by  Yanko Sosa 1 week ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #27006

    Eli Cohen
    Participant

    Hi,
    How can I transmit an UDP broadcast with the socket option “SO_BROADCAST” unsupported?
    Thanks

    #27151

    Yanko Sosa
    Keymaster

    Hi Eli,
    You’d have to set the socket address (in the case for IPv4) to “255.255.255.255”. You can do this by using the NetApp_SetSockAddr() API after opening the socket and passing this API a pointer to a variable of type NET_IPv4_ADDR that contains the broadcast address (‘server_addr’ in the following example):

        
    #define  TX_BUF_SIZE          256u
    #define  UDP_SERVER_PORT    10001u
    
    CPU_BOOLEAN UDP_Client_Pseudocode(void)
    {
        NET_SOCK_ADDR             server_sock_addr;    
        NET_IPv4_ADDR             server_addr;
        NET_SOCK_ID               sock;
        NET_ERR                   err;
        CPU_CHAR                  tx_buf[TX_BUF_SIZE];
        NET_SOCK_DATA_SIZE        tx_rem;
        OS_ERR                    os_err;
    
        NetASCII_Str_to_IP("255.255.255.255",
                           &server_addr,
                            sizeof(server_addr),
                           &err);
    
        sock = NetSock_Open(NET_SOCK_PROTOCOL_FAMILY_IP_V4,
                            NET_SOCK_TYPE_DATAGRAM,
                            NET_SOCK_PROTOCOL_UDP,
                           &err);
    
        NetApp_SetSockAddr(        &server_sock_addr,
                                    NET_SOCK_ADDR_FAMILY_IP_V4,
                                    UDP_SERVER_PORT,
                           (void *)&server_addr,
                                    NET_IPv4_ADDR_SIZE,
                                   &err);
    
    //optionally you could bind 'sock' here to a specific local port and/or interface number
    
        tx_rem = TX_BUF_SIZE;
        p_buf  = tx_buf;
        do {
            tx_size = NetSock_TxDataTo(                  sock,
                                                         tx_buf,
                                                         tx_rem,
                                                         NET_SOCK_FLAG_NONE,
                                       (NET_SOCK_ADDR *)&server_sock_addr,
                                                         NET_SOCK_ADDR_SIZE,
                                                        &err);
            switch (err) {
                case NET_SOCK_ERR_NONE:
                     tx_rem -= tx_size;
                    *(CPU_INT08U *)p_buf  += tx_size;
                     break;
    
                case NET_ERR_FAULT_LOCK_ACQUIRE:
                case NET_ERR_TX:
                case NET_ERR_IF_LINK_DOWN:
                     OS_TimeDly(1u, OS_OPT_TIME_DLY, &os_err);
                     break;
    
                default:
                     NetSock_Close(sock, &err);
                     return (DEF_FAIL);
                }
                if (tx_rem == 0u) {
                    break;
                }
            } while (1u);
    
            NetSock_Close(sock, &err);
            return (DEF_OK);
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

View the complete site map

x
Loading...