Broadcast, multicast
Foreword
In network programming, a transmitting end can be transmitted by broadcasting and multicast, and a plurality of receiving ends are received.
broadcast
Since TCP is connected, it cannot be used to send broadcast messages. Send broadcast messages, you must use UDP, and UDP can send messages without establishing a connection. The destination IP address of the broadcast message is a special IP address called a broadcast address. Broadcast addresses are composed of a full 1 host suffix, such as: 192.168.1.255 is a broadcast address of the network of 130.168.25.255 is 130.168.0.0. If you send a message to all IP addresses (255.255.255.255), then theoretically, all networking computers all over the world can be received. However, it is actually not the case, and the general router is set to abandon such a package, which is only broadcast in the local network, so the effect and the broadcast address to the local network are the same.
The process of sending broadcast messages in C # is as follows, pay attention to calling the setsockoption function, otherwise you have to throw an exception:
Socket Sock = New Socket (AddressFamily.InterNetwork, SocketType.dgram,
Protocoltype.ud);
IpendPoint IEP1 = New IpendPoint (iPaddress.Broadcast, 9050); // 255.255.255.255
IpendPoint IEP2 = New IpendPoint (iPaddress.Parse ("192.168.1.255"), 9050);
String hostname = dns.gethostname ();
Byte [] Data = Encoding.ascii.getbytes (Hostname);
Sock.SetSocketoption (socketoptionledLevel.socket,
Socketoptionname.broadcast, 1);
Sock.sendto (Data, IEP1);
Sock.sendto (Data, IEP2);
Sock.close ();
The process of receiving broadcast messages in C # is as follows, nothing special:
Socket Sock = New Socket (AddressFamily.InterneTwork,
Sockettype.dgram, protocoltype.udp);
IpendPoint IEP = New IpendPoint (iPaddress.Any, 9050);
SOCK.BIND (IEP);
Endpoint EP = (endpoint) IEP;
Console.WriteLine ("Ready to Receive ...");
BYTE [] DATA = New byte [1024];
Int Recv = Sock.ReceiveFrom (Data, Ref EP);
String stringData = encoding.ascii.getstring (data, 0, rv);
Console.writeline ("Received: {0} from: {1}",
StringData, EP.TOString ());
Data = new byte [1024];
Recv = sock.receivefrom (data, ref ep);
StringData = encoding.ascii.getstring (DATA, 0, RECV);
Console.writeline ("Received: {0} from: {1}",
StringData, EP.TOString ());
Sock.close ();
Attentions:
1 Broadcast can be used by a client program to notify the service programs in the subnet, your location.
2 When sending a broadcast message, the specified port is also a role. If the UDP socket of the receiving end can receive a message if bind is in this interface. (If you don't bind, you can receive the message of all ports ??)
3 The remote IP address displayed in the package received by the receiving end is the address of the sender. That is, the broadcast address will not display the source IP address location of a package (Loopback address does not display the destination IP address of the package)
4 You can use the thread to constantly send and receive broadcast messages, confirm the position of both ends and proof your existence
One disadvantage of broadcasting will affect the computer in all subnets, even if the broadcast message is not interested in the computer. Multicast can solve this problem.
Multicast adopts advancement technology (browsing webpage belonging to drawing technology, which is also a transmission email service). Multicast is also called multicast. If the user joins a multicast group, it can receive data sent to this group.
Multicast Application D IP Address (224.0.0.0-239.255.255.255), but not to say that the computer that receives data from each multicast is to have a D IP address. The multicast group requires a D class IP address to indicate. The D IP address is divided into several broken, some of which have special purposes.
There are two models of multicast. One is any of a group of users to send information, and the rest of the users can receive, and the position of each user is equivalent. The other is only one user issued a message, and the rest of the users are only responsible for receiving information.
The multicast topology is a tree structure.
Multicast requires support for multicast hardware, supports multicast TCP / IP protocol stack, and supports multicast software. Join Leave a multicast group to use the SGMP (SIMPLE Group Management Protocol) protocol. Sending multicast information There is also a TTL (TIME TO LIVE) value that makes the multicast information will not pass a lot of subnet boundaries. The default TTL value is 1, that is, only the local subnet is valid.
C # For multicast support multicast programming requires UDP, there are two class support multicast network programming socket, and udpclient. A computer is to join a group, receive information sent to a group. The Socket class is called the SetSocketOption function to join and leave a group. The UDPCLIENT class has direct access and the member function that leaves a group can be called. In order to issue information, there is nothing special, just set the destination address of the sending data to the multicast address.
Question: Q: One UDP Server joins a multicast group, then bound a port, start receiving data, which data can you receive? A: You can receive data sent to this multicast group, you can receive broadcast data sent to this port, which can receive unicast data dedicated to this port.