How to implement BroadCast in vxworks
This article mainly introduces how to achieve BroadCast in vxworks, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
With unicast and multicast, broadcasting can't run away. The difference between broadcast, BroadCast and unicast is that the address is different. The address for broadcasting is 255.255.255.255, and the broadcast data will not be forwarded by the router and is limited to the local area network, otherwise it will be a network storm.
The code for broadcasting is actually simpler. Refer directly to the example of unicast / multicast to write a
Server is slightly modified on the basis of unicast
Call socket () to open a Socket
Call bind () to specify Port information
Call setsockopt () to join the broadcast
Call recvfrom () to block reception
Finally, call close () to close Socket
It's easier to broadcast Client.
Call socket () to open a Socket
Specify broadcast address and Port information
Call sendto () to send data
Finally, call close () to close Socket
The complete code is as follows
# include / * pritnf () * / # include / * malloc () * / # include / * socket () * / # include / * struct sockaddr_in * /
# define HOST_PORT 60000#define MIN_SIZE 10#define MIN_TIMES 10
Void broadServer (int port, int size, int times) {struct sockaddr_in localAddr; struct sockaddr_in peerAddr; int addrSize = sizeof (struct sockaddr); int opt = 1; int fd; int num; char * buffer; if (port)