Broadcast and multicast detailed (i)

Source: Internet
Author: User
Tags bind join socket port number htons

Broadcast

Broadcast refers to sending information to all network nodes in a local area network. This is a type of UDP connection

Broadcasts have a broadcast group that only nodes within a broadcast group can receive information that is sent to this broadcast group. What determines a broadcast group, that is, the port number, a node in the LAN, if the broadcast properties are set and listen to the port number A, then he joined the group A broadcast, the LAN all the information sent to the broadcast port a he received. In the broadcast implementation, if a node wants to accept a set of broadcast information, it is necessary to bind him to address and port A, and then set the properties of the socket to broadcast properties. If a node does not want to accept broadcast information, but only wants to send broadcast information, then do not have to bind the port, only need to set the broadcast property for the socket, send UDP information to a port of broadcast address Inaddr_broadcast. The detailed program implementation is as follows:

1. Initialization

WSAStartup (Makeword (2,2), &wsad);

2. Create a UDP socket
S=socket (af_inet,sock_dgram,0);

3. If the socket wants to receive information, it will need to bind the address and the port number of this set of broadcasts, if you just want to send broadcast information, you do not need this step

Sockaddr_in Udpadress,sender;
int senferaddsize=sizeof (sender);
Udpadress.sin_family=af_inet;
Udpadress.sin_port=htons (11114);
UDPADRESS.SIN_ADDR.S_ADDR=INET_ADDR ("10.11.131.32");
Bind (S, (sockaddr*) &udpadress,sizeof (udpadress));

This node can receive all broadcast messages destined for Port 11114 in the LAN.

4. Set the socket's properties to broadcast
BOOL Optval=true;
SetSockOpt (S,sol_socket,so_broadcast, (char*) &optval,sizeof (bool));

5. You can use Recvfrom or sendto to send and receive broadcast messages.

Here is accepted, this is a blocking operation
Ret=recvfrom (s,data,1000,0, (sockaddr*) &sender,&senferaddsize);

Here is a message like this broadcast Group, note that the address sent is the broadcast address Inaddr_broadcast, the port number is the port number of the reorganization broadcast 11114

Sockaddr_in Dstadd;
Dstadd.sin_family=af_inet;
Dstadd.sin_port=htons (11114);
Dstadd.sin_addr.s_addr=inaddr_broadcast;
SendTo (S,data (), totalbyte,0, (sockaddr*) &dstadd,sizeof (sockaddr));

Multicast

Multicast and broadcast, multicast refers to a piece of information to a limited number of nodes in the LAN transmission, and broadcast is whether a node in the development group, will be sent to this node broadcast information, easy to cause network burden serious.

Multicast implementation is based on the multicast group, in the local area network, a multicast address unique definition of a multicast group (port number arbitrary), can use the multicast address is specified, from the 224.0.0.0-239.255.255.255, but some of the addresses can not be used, It is used for special purposes: 224.0.0.0–224.0.0.2 224.0.1.1 224.0.0.9 224.0.1.24. A node if you want to accept from a multicast group or send information to a multicast group, you must first join the multicast group and then send the UDP. The following is a detailed code implementation.

1. Initialization

WSAStartup (Makeword (2,2), &wsad);

2. A socket for multicast communication is built here, note that the socket parameter is set to multicast
S=wsasocket (af_inet,sock_dgram,0,null,0,wsa_flag_multipoint_c_leaf| Wsa_flag_multipoint_d_leaf| wsa_flag_overlapped);

3. Bind the socket to a local address, port, and broadcast, in multicast, both the sending and receiving end must be bound to a local address, which is the port of processing information when multicast communication
Udpadress.sin_family=af_inet;
Udpadress.sin_port=htons (22222);
UDPADRESS.SIN_ADDR.S_ADDR=INET_ADDR ("10.11.131.32");
Bind (S, (sockaddr*) &udpadress,sizeof (udpadress));

4. Define the address of the multicast group
Multicastgroup.sin_family=af_inet;
Multicastgroup.sin_port=htons (1111); Here the port is arbitrary, each node can be set to a different
MULTICASTGROUP.SIN_ADDR.S_ADDR=INET_ADDR ("224.0.0.3"); Use the multicast address in the address section specified above

5. Join the multicast group. Note that the function here returns a socket, which is not responsible for communication, but only when it is detached from the multicast group.

SOCKET Sockm=wsajoinleaf (S, (sockaddr*) &multicastgroup,sizeof (Multicastgroup), null,null,null,null,jl_both);

6. Use Recvfrom to accept multicast information, or use SendTo to send multicast information

Ret=recvfrom (s,data,1000,0, (sockaddr*) &sender,&senferaddsize);

SendTo (S,data (), totalbyte,0, (sockaddr*) &multicastgroup,sizeof (Multicastgroup));

7. Finally close the cleanup
Closesocket (SOCKM);
Closesocket (s);
WSACleanup ();

Other:

1) In a multicast group, by default, a node that emits multicast information receives the information it sends itself, called a multicast loopback, which can turn off multicast loopback:

BOOL Val=false;

Setsocket (S,ipproto_ip,ip_multicast_loop, (char*) val,sizeof (Val));

2) in multicast, usually to set the appropriate TTL (the value of the TTL is how much, then the multicast information can go through how many routers, each through a router, the value of TTL automatically minus 1):

int val=3;

Setsocket (S,ipproto_ip,ip_multicast_ttl, (char*) val,sizeof (int);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.