UDP broadcast and Multicast

Source: Internet
Author: User
Tags sendmsg
Source: http://158067568.iteye.com/blog/901052udpbroadcast and Multicast

Author: Legend

QQ: 158067568

We recommend that you do not connect to UDP before transmitting information. In other words, the client sends information to the server. The client only needs to give the Server IP address and port number, and then encapsulate the information in a message to be sent and send it out. The client does not care whether the server exists or whether it can receive the message.

Generally, the UDP programs we discuss are one-to-one unicast programs. This chapter discusses one-to-many services: Broadcast and multicast ). For broadcast, all hosts in the network receive a copy of data. For multicast, a message is only sent to a multicast address. network knowledge is used to distribute data to the host that indicates the data to be received and sent to the multicast address. In general, only UDP sockets allow broadcast or multicast.

UDP Broadcast

The difference between broadcast UDP and unicast UDP is that the IP address is different. Broadcast uses the broadcast address 255.255.255.255 to send messages to each host on the same broadcast network. It is worth noting that:The local broadcast information is not forwarded by the router.. Of course, this is very easy to understand, because if the router forwards broadcast information, it will inevitably cause network paralysis. This is also why IP protocol designers intentionally did not define the Internet-wide broadcast mechanism.

Broadcast addresses are usually used to exchange status information between gamers on the same local network in online games. The broadcast will not write the demo program. You can change the IP address of the ECHO program to the broadcast address.

In fact, as the name implies, broadcast is intended for all people in the LAN to speak,However, the broadcast still needs to specify the receiver's port numberBecause it is impossible for all the receiver ports to listen to the broadcast.

UDP multicast

Similarly, UDP multicast must specify the receiver's port number. Similarly, the difference between multicast and unicast lies in the address. The multicast address range in IPv4 is:224.0.0.0 to 239.255.255.255. In Java, multicast is very well implemented. To implement multicast, The multicastsocket class is used. In fact, this class is a subclass of the datagramsocket. In addition to some of its own features, use it as the initramsocket class. Below is a simple example of receiving data from Multicast:

Java code
  1. <Strong> package cn.edu. heut. ZCL. multicast;
  2. Import java.net. datagrampacket;
  3. Import java.net. inetaddress;
  4. Import java.net. multicastsocket;
  5. Public class udpmulticastserver {
  6. Final Static int receive_length = 1024;
  7. Static string multicasthost = "224.0.0.1 ";
  8. Static int localport = 9998;
  9. Public static void main (string [] ARGs) throws exception {
  10. Inetaddress receiveaddress = inetaddress. getbyname (multicasthost );
  11. If (! Receiveaddress. ismulticastaddress () {// test whether the multicast address is used
  12. Throw new exception ("use multicast address ");
  13. }
  14. Int Port = localport;
  15. Multicastsocket extends emulticast = new multicastsocket (port );
  16. Receivemulticast. joingroup (receiveaddress );
  17. Datagrampacket dp = new datagrampacket (New byte [receive_length], receive_length );
  18. Receivemulticast. Receive (DP );
  19. System. Out. println (new string (DP. getdata (). Trim ());
  20. Receivemulticast. Close ();
  21. }
  22. }
  23. </Strong>

 

 

 

 

 

The following code implements the multicast Sender:

Java code
  1. Package cn.edu. heut. ZCL. multicast;
  2. Import java.net. datagrampacket;
  3. Import java.net. inetaddress;
  4. Import java.net. multicastsocket;
  5. Public class udpmulticastclient {
  6. Static string destaddressstr = "224.0.0.1 ";
  7. Static int destportint= 9998;
  8. Static int ttltime = 4;
  9. Public static void main (string [] ARGs) throws exception {
  10. Inetaddress destaddress = inetaddress. getbyname (destaddressstr );
  11. If (! Destaddress. ismulticastaddress () {// checks whether the address is a multicast address.
  12. Throw new exception ("the address is not a multicast address ");
  13. }
  14. Int destport = destportint;
  15. Int TTL = ttltime;
  16. Multicastsocket multisocket = new multicastsocket ();
  17. Multisocket. settimetolive (TTL );
  18. Byte [] sendmsg = "11 # MSG". getbytes ();
  19. Datagrampacket dp = new datagrampacket (sendmsg, sendmsg. length, destaddress, destport );
  20. Multisocket. Send (DP );
  21. Multisocket. Close ();
  22. }
  23. }

 

 

The TTL value (time to live) is set in multicast. Each IP data packet contains a TTL value. When a router forwards the message, the TTL value is reduced by 1 and the value is reduced to 0, when the lifecycle ends, the message does not arrive at the destination immediately, and the message is declared dead immediately. Of course, in Java, TTL is not very accurate. I once introduced in a book that the packet propagation distance will not exceed the value set by TTL.

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.