do you have sample code for UDP broadcast?

Discussion to talk about software related topics only.
Post Reply
leetehui
Posts: 10
Joined: Thu Aug 27, 2009 2:50 am

do you have sample code for UDP broadcast?

Post by leetehui »

I hope to send UDP broadcast message to subnet.
I met problem using multicast example. Can someone provide sample code to me? :?:
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: do you have sample code for UDP broadcast?

Post by seulater »

I pulled this out of one of my programs that works using UDP broadcast.
some things need to be changes like my vaiables to your, but you should be able to figure it out.


IPADDR multicast_ip; // used for UDP multicast
OS_FIFO fifoo; // used for UDP multicast
SHORT len;
WORD udp_port = 21700;


// Place the below in main, anywhere after "StartHTTP();"
// i noticed that it depending on where i placed this it would or would not work.

multicast_ip = AsciiToIp( "224.000.000.001" );
OSFifoInit( &fifoo );

//Register to listen for Multi packets on port number 'port'
RegisterMulticastFifo( multicast_ip, udp_port, &fifoo );



// This is what you will use for your Transmit Broadcast.
// use Broadcast to send it out
{
UDPPacket pkt;
pkt.SetSourcePort( udp_port );
pkt.SetDestinationPort( udp_port );
pkt.AddData( tx_ptr, buffer_tx_size );
pkt.Send( multicast_ip );
}



// This is what you can use for your Listen for the Broadcast.
// This is the udp broadcast listener.
while(1)
{
UDPPacket upkt( &fifoo, 10 );

//Did we get a valid packet? or just time out?
if ( upkt.Validate() )
{
len = upkt.GetDataSize();

memcpy(rx_ptr,upkt.GetDataBuffer(),len); // rx_ptr, change this to your pointer
}
}
Post Reply