JAVA unicast, multicast (multicast), broadcast

Source: Internet
Author: User

The three terms unicast "(Unicast)," Multicast "(multicast), and" broadcast "(broadcast) are terms used to describe how network nodes communicate with each other. So what do these terms mean? What's the difference?

1. Unicast: Communication between network nodes is like a conversation between people. If a person speaks to another person, the term "unicast" is described in terms of network technology, when the information is received and transmitted only between two nodes. Unicast has been widely used in the network, most of the data on the network is transmitted in the form of unicast, but the general network users do not know. For example, when you are sending and receiving e-mails, browsing Web pages, you must establish a connection with the mail server, the Web server, and use the unicast data transfer method. However, it is common to use point-to-point communication instead of unicast, because unicast is generally used relative to multicast and broadcast.

2. Multicast: "Multicast" can also be called "Multicast", the application of network technology is not many, on-line video conferencing, online video-on-demand is particularly suitable for the use of multicast mode. Because if the unicast mode, node by section, how many target nodes, there will be the number of transmission process, this method is obviously very inefficient, is not desirable, if the use of non-differentiated target, all sent broadcast mode, although the data can be transmitted at one time, but it is clear that the purpose of differentiating the specific data receiving object. By using multicast, the data of all the target nodes can be transferred at once, and the data can only be transferred to specific objects. IP network multicast is generally implemented by multicast IP addresses. The multicast IP address is the class D IP address, which is the IP address between 224.0.0.0 and 239.255.255.255. DHCP Manager in Windows 2000 supports automatic assignment of multicast IP addresses.

3. Broadcast: "Broadcast" in the network more applications, such as the client through DHCP automatic IP address process is achieved through the broadcast. However, compared to unicast and multicast, broadcast consumes almost all the bandwidth of the network within the subnet. Take the meeting for example, there can only be one person speaking at the meeting, imagine if all the people at the same time with the microphone to speak, the venue will be chaos into porridge. Hub because it works to determine the impossibility of filtering the broadcast storm, the general switch does not have this function, but now some network switches (such as the Omni-Direction QS series switch) also has the filtering broadcast storm function, the router itself has the role of isolating broadcast storm. Broadcast storms can not be completely eliminated, but only in the same subnet spread, as if the sound of the horn can only be spread in the same venue, so in the hundreds of or even thousands of computers made up of large and medium-sized LAN, the general sub-network division, like a hall with walls isolated into many small halls, To achieve the purpose of isolating the broadcast storm. In an IP network, the broadcast address is represented by the IP address "255.255.255.255", which represents all IP addresses within the same subnet.

Multicast Sample code:

package com.goma.test;import java.net.datagrampacket;import java.net.inetaddress;import  Java.net.multicastsocket;public class multicastsend {public static void main ( String[]args) {try {/****************** Send multicast data ****************///Specify multicast ipinetaddress ip =  Inetaddress.getbyname ("228.5.6.7"); Multicastsocket s = new multicastsocket ();//Join the Group  s.joingroup (IP);//Set the TTL value in multicast (time  to live), each IP data message contains a ttl,//the TTL is reduced by 1 whenever a router forwards the message, knowing that it is reduced to 0 o'clock, the end of the life cycle, the message does not reach the destination immediately, and/or the death is instantly declared. Of course, in Java, the TTL is not very accurate,//once in a book that the transmission distance of the message will not exceed the value set by the TTL. S.settimetolive (1); string msg =  "ip:123123123; port:1231 ";D Atagrampacket hi = new datagrampacket (Msg.getbytes (),  msg.length (), IP,  6789); S.send (hi);/****************** send multicast Data ****************/s.close ();}  catch  (exception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}}} Package com.goma.Test;import java.net.datagrampacket;import java.net.inetaddress;import java.net.multicastsocket ;p Ublic class multicastreceive {public static void main (String[]args) {try { IP Group inetaddress ip = inetaddress.getbyname ("228.5.6.7");     // Multicast Listener Port Multicastsocket s = new multicastsocket (6789);//Join the Group  s.joingroup (IP); byte[]  arb = new byte[24];while  (True)  {datagrampacket datagrampacket =  new datagrampacket (arb,arb.length); s.receive (Datagrampacket); System.out.println (New string (Datagrampacket.getdata (),  0, datagrampacket.getlength ()); System.out.println (arb.length); System.out.println (New string (ARB));}}  catch  (exception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}}}

Note: If you are running the support of a router that needs to support multicast on the Internet (it is not possible for each client to install the appropriate router, multicast can be said to support LAN only.) )


Java Broadcast Sample code:

Package com.goma.test;import java.net.datagrampacket;import java.net.datagramsocket;import  java.net.InetAddress;public class BroadCastSend {/** *  @param  args */ Public static void main (String[] args)  {// todo auto-generated method  stubtry {inetaddress ip = inetaddress.getbyname ("255.255.255.255");D Atagramsocket  ds = new datagramsocket (); string str =  "Goma god like.";D Atagrampacket dp = new datagrampacket (Str.getbytes (), str.getbytes () .length, ip,  3001);         ds.send (DP);         ds.close ();}  catch  (exception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}   Create socket   }}package com.goma.test;import java.net.datagrampacket;import for sending datagram packets  java.net.datagramsocket;public class broadcastreceive {/** *  @param  args */ Public static void main (String[] args)  {// todo auto-generated method  stubtry{        DatagramSocket ds = new  Datagramsocket (3001);//  creates a receive datagram socket and binds it to the specified port on the local host            byte[] buf = new byte[1024];          Datagrampacket dp = new datagrampacket (buf, buf.length);         ds.receive (DP);   //        string  strrecv = new string (Dp.getdata (),  0, dp.getlength ())  +  " from   "   + dp.getaddress (). Gethostaddress ()  + ": " + dp.getport ();      &nbsP;   system.out.println (new string (BUF));  }catch  (Exception e)  {  todo: handle exceptione.printstacktrace ();}}}

This article is from the "OMA" blog, so be sure to keep this source http://oma1989.blog.51cto.com/1931488/1559626

JAVA unicast, multicast (multicast), broadcast

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.