Socket programming (3) broadcast Multicast

Source: Internet
Author: User
Tags htons

Broadcast

Broadcast refers to sending information to all online nodes in a LAN. This is a type of UDP connection.

A broadcast has a broadcast group, that is, only nodes in a broadcast group can receive information sent to this broadcast group. What determines a broadcast group? The port number is a node in the LAN. If the broadcast attribute is set and the port number A is listened to, the broadcast group A is added, all the information sent to broadcast port A in the LAN is received. In broadcast implementation, if a node wants to accept group a broadcast information, it must first bind it to the address and port A, and then set the socket attribute to the broadcast attribute. If a node does not want to accept broadcast information but only sends broadcast information, you do not need to bind a port. You only need to set broadcast properties for the socket and then send UDP information to port a of the inaddr_broadcast broadcast address. 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 this socket wants to receive the information, it needs to bind the address and the port number of this group of broadcasts. If you only want to send the broadcast information, this step is not required.

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 receives all the broadcast messages sent to port 11114 in the LAN.

4. Set the socket property 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 information.

This is acceptable. This is a blocking operation.
Ret = recvfrom (S, Data, 1000,0, (sockaddr *) & sender, & senferaddsize );

Here is the message sent like this broadcast group. Note that the sent address is the broadcast address inaddr_broadcast, and the port number is the port number 11114 of the reorganized broadcast.

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

Different from broadcasting, multicasting means that a piece of information is transmitted to a limited number of nodes in the LAN. Broadcasting sends broadcast information to a node regardless of whether a node is in a specified group, this can easily cause serious network burden.

Multicast is implemented by multicast groups. In the LAN, a multicast address uniquely defines a multicast group (with any port number ), the available multicast addresses are specified from 224.0.0.0-ranges, but some of them cannot be used. They are used for special purposes: 224.0.0.0-224.0.0.2 224.0.1.1 224.0.0.9 224.0.1.24. If a node wants to accept information from a multicast group or send messages to a multicast group, it must first join the multicast group and then send messages to UDP. The following is a detailed code implementation.

1. Initialization

Wsastartup (makeword (2, 2), & wsad );

2. Create a socket for multicast communication. 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. binding a socket to a local address and port is different from broadcasting. In multicast, either sending or receiving end must be bound to a local address, this address is the port for processing information during 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 multicast group address
Multicastgroup. sin_family = af_inet;
Multicastgroup. sin_port = htons (1111); the port here is arbitrary, and each node can be set to a different
Multicastgroup. sin_addr.s_addr = inet_addr ("224.0.0.3"); Use the multicast address specified in the preceding CIDR block.

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

Socket sockm = wsajoinleaf (S, (sockaddr *) & multicastgroup, sizeof (multicastgroup), null, jl_both );

6. Use recvfrom to receive 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. Close cleanup.
Closesocket (sockm );
Closesocket (s );
Wsacleanup ();

 

Others:

1) In multicast groups, by default, a node that sends multicast information also receives the information sent by itself. This is called multicast loop. You can disable multicast loop:

Bool val = false;

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

2) When multicasting, you usually need to set the appropriate TTL (what is the TTL value, then the multicast information can go through the number of routers, each passing through a vro, the TTL value is automatically reduced by 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.