We are developing a product using Linux BSP on a ColdFire 5475. I can send messages to specific IP addresses over UDP; however, when I attempt to send broadcast messages, I get the following error:
"Network is unreachable"
I have seen the same problem when I run our software on the "ColdFire Lite Development Kit" board from Freescale.
Except for having a broadcast thread ( the udp_inquire_thread ), I have the same code working correctly on my Linux PC ( running Fedora Core 4). Using /sbin/ifconfig I have checked both my Linux PC and the development board ( Linux BSP on the ColdFire ). /sbin/ifconf shows the following for both:
Bcast: 192.168.3.255 Mask: 255.255.252.0
On the development board (Linux BSP) I have two threads: udp_listen_thread and udp_inquire_thread.
The udp_listen_thread creates a UDP socket, sets the BROADCAST option, binds the socket, and then waits for receive data in a while(1) loop.
int udp_sock; int on; socklen_t optlen; socklen_t addr_len; struct sockaddr_in addr_host;
udp_sock = socket( AF_INET, SOCK_DGRAM, 0 );
on = 1; optlen = sizeof( on ); setsockopt( udp_sock, SOL_SOCKET, SO_BROADCAST, &on, optlen );
addr_len = sizeof( addr_host ); addr_host.sin_family = AF_INET; addr_host.sin_port = htons( 18001 ); addr_host.sin_addr.s_addr = htonl( INADDR_ANY );
bind( udp_sock, (struct sockaddr *) &addr_host, addr_len );
while (1) { // Read Socket ===> recvfrom( udp_sock, ... );
// Process Msg }
The udp_inquire_thread sends a specific message out the UDP socket to get info about other devices.
socklen_t addr_len; struct soctaddr_in addr_clnt;
//////////////////////////////////////////////////////////////////////////// // Building Inquire Message ( inquire_msg ) here.
////////////////////////////////////////////////////////////////////////////
addr_len = sizeof( struct sockaddr_in ); memset( &addr_clnt, 0, addr_len ); addr_clnt.sin_family = AF_INET; addr_clnt.sin_port = htons( 18001 ); addr_clnt.sin_addr.s_addr = htonl( INADDR_BROADCAST );
while (1) { sleep (10);
pthread_mutex_lock( &udp_mutex ); if ( -1 == sendto( udp_sock, &inquire_msg, msg_len, 0, (struct sockaddr *) &addr_clnt, addr_len ) ) { perror( "sending datagram message" ); //