Java unicast, broadcast, multicast (multicast)

Source: Internet
Author: User
Tags website server

I. Classification of communication methods

There are three communication modes in the current network traffic: unicast, broadcast, and multicast (multicast), where multicasting occurs at the latest, with the advantages of unicast and broadcast.

    • Unicast: Communication between a single host and a single host
    • Broadcast: When a host communicates with all hosts in the network
    • Multicast: When a host communicates with a selected group of hosts
Second, unicast

Unicast is the most common form of network communication, and communication between network nodes is like a conversation between people. If a person speaks to another person,

So the term "unicast" is described in terms of network technology, when information is received and transmitted only between two nodes.

1. Advantages of unicast:

(1) Server and response to client requests;

(2) The server can send different responses for each client's request, and it is easy to display the personalized service;

2. The disadvantages of unicast:

The server sends data streams for each client, server traffic = number of clients x client traffic, and servers are overwhelmed in streaming applications where the number of clients is large and each client is heavily traffic;

3. Application Scenario:

Unicast is widely used in the network, and most of the data on the network is transmitted in the form of unicast. For example: To send and receive e-mail, visit the Web page, must be connected with the mail server, the website server, at this time using unicast communication mode;

Third, broadcasting

"Broadcast" can be used for example: a person speaks to the whole audience by means of a radio speaker (He doesn't care if you're willing to listen). In other words: A broadcast is a host that sends a datagram packet to all hosts on a network. This network could be a network, a subnet, and possibly all subnets.

There are two types of broadcasts: Local broadcast and directed broadcast

    • Directed broadcast: sends a datagram packet to all hosts on a specific network outside the network, however, because most routers on the Internet do not forward the directed broadcast message, there is no in-depth introduction
    • Local broadcast: Sends a datagram packet to all hosts on the local network, IPV4 's local broadcast address is "255.255.255.255", and the router does not forward this broadcast;

1. Advantages of broadcasting:

(1) The communication efficiency is high, the information can be passed to all the hosts on a network at once.

(2) Because the server does not send data to each client separately, the server traffic is lower than the load;

2. Disadvantages of broadcasting:

(1) The bandwidth of the network is very occupied;

(2) Lack of pertinence, and whether the host does not really need to receive the data, it is mandatory to receive data;

3. Application Scenario:

(1) Cable TV is a typical broadcast network

Java Broadcast Example:

Client Send Program

//send-side program Public classbroadcasttest{ Public Static voidMain (string[] args) {//Broadcast implementation: broadcast by the client, server-side receiveString host = "255.255.255.255";//Broadcast Address        intPort = 9999;//destination port for broadcastString message = "Test";//the string to send        Try{inetaddress adds=Inetaddress.getbyname (host); Datagramsocket DS=NewDatagramsocket (); Datagrampacket DP=NewDatagrampacket (Message.getbytes (), message.length (), adds, port);            Ds.send (DP);        Ds.close (); }         Catch(Exception e) {e.printstacktrace (); }    }}

Server-side Receive program

//server-side receive program Public classbroadcastserver{ Public Static voidMain (string[] args) {intPort = 9999;//Open the Listening portDatagramsocket ds =NULL; Datagrampacket DP=NULL; byte[] buf =New byte[1024];//store the messages that are sentStringBuffer sbuf =NewStringBuffer (); Try             {                //of the bound portDS =NewDatagramsocket (port); DP=NewDatagrampacket (buf, buf.length); System.out.println ("Listen for broadcast port open:");                Ds.receive (DP);                Ds.close (); inti;  for(i=0;i<1024;i++)                {                    if(Buf[i] = = 0)                    {                         Break; } sbuf.append ((Char) buf[i]); } System.out.println ("Received broadcast message:" +sbuf.tostring ()); }            Catch(Exception e) {e.printstacktrace (); }         }}
Iv. Multicast (multicast)

"Multicast" can be used for example: you shout to the street: "is a man to come, a person hair 100," then the man came over, the woman will not come over, because no money to send her ignore you (multicast: where all the boys are a group), in other words: Multicast is a host to a specified set of hosts to send datagrams, 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 obviously not to distinguish the purpose of a particular data receiving object, It also consumes network bandwidth. By using multicast, you can achieve a single transfer

Data from the target node can also be reached for the purpose of transmitting data only to specific objects.

Multicast IP networks are 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.

1. Advantages of Multicast:

(1) Have all the advantages of broadcasting;

(2) compared with unicast, it provides the efficiency of sending datagram packets, and reduces network traffic compared with broadcast.

2. The disadvantages of multicast:

(1) There is no error correction mechanism compared with unicast protocol, it is difficult to make up after packet loss, but it can be compensated by some fault-tolerant mechanism and QoS.

A simple example of multicast:

Client sends a message

//send-side program Public classsendudp{ Public Static voidMain (string[] args)throwsIOException {MulticastSocket ms=NULL; Datagrampacket Datapacket=NULL; Ms=NewMulticastSocket (); Ms.settimetolive (32); //The IP of this machine (which can write dynamically acquired IP) address in the packet, in fact, the server side received the packet can also get to the employer's IP         byte[] data = "Multicast Test". GetBytes (); InetAddress Address= Inetaddress.getbyname ("239.0.0.255"); Datapacket=NewDatagrampacket (data, Data.length, address,8899);           Ms.send (Datapacket);       Ms.close (); }}

Server-side Receive program:

//server-side programs Public classtestmain{Private StaticMulticastSocket ds; StaticString multicasthost= "239.0.0.255"; Staticinetaddress receiveaddress;  Public Static voidMain (string[] args)throwsIOException {ds=NewMulticastSocket (8899); Receiveaddress=Inetaddress.getbyname (multicasthost);          Ds.joingroup (receiveaddress); NewThread (Newudprunnable (DS)). Start (); }}    classUdprunnableImplementsRunnable {multicastsocket ds;  Publicudprunnable (MulticastSocket ds) { This. ds=ds; }         Public voidrun () {byteBuf[] =New byte[1024]; Datagrampacket DP=NewDatagrampacket (BUF, 1024);  while(true)              {                      Try{ds.receive (DP); System.out.println ("Receive Client message:" +NewString (buf, 0, Dp.getlength ())); }                     Catch(Exception e) {e.printstacktrace (); }                  }                      }}

Operation Result:

  

V. References

1, http://www.2cto.com/kf/201312/264488.html

Java unicast, broadcast, multicast (multicast)

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.