java unicast broadcast multicast details and example code

  • 2020-06-07 04:26:43
  • OfStack

java unicast, broadcast, multicast details and example code

In current network communication (TCP/IP is no exception), there are three communication modes: unicast, broadcast and multicast (also called multicast, I feel it is a little inappropriate to describe multicast), among which multicast appears the latest, but it has the advantages of both unicast and broadcast and has the most promising development prospect.

1. Classification of communication methods:

1. Unicast: communication between a single host and a single host;

2. Broadcast: communication between a single host and all hosts in the network;

3. Multicast: communication between a single host and a selected group of hosts;

2. The unicast:

Unicast is the most common type of network communication. The communication between network nodes is just like the conversation between people. If one person speaks to another person,

In network terms, this is called unicast, where information is received and transmitted only between two nodes.

1. Advantages of unicast:

(1) The server and the response of the client;

(2) The server can send different responses to different requests of each client, making it easy to display personalized services;

2. Disadvantages of unicast:

(1) The server sends data flow for each client, the server traffic = the number of clients × the client traffic; In the streaming media application with large number of clients and large flow of each client, the server is overwhelmed.

3. Application Scenario:

Unicast has been widely used in the network where most of the data is transmitted in the form of unicast. For example, when sending and receiving E-mail, surfing the Web, you must communicate with the mail server,

The server establishes the connection, at this time USES the unicast communication mode;

3. The radio:

To broadcast, for example, is to talk to an audience over a loudspeaker (he doesn't care if you want to listen or not). In other words: broadcast is when a host sends datagram packets to all hosts on a given network.

This network could be a network, it could be a subnet, and it could be all subnets.

There are two types of broadcasting: local and directed:

Directed broadcast: Sends datagram packets to all hosts on a specific network outside of the network, however, since most routers on the Internet do not forward directed broadcast messages, I won't go into that here

Local broadcast: send datagram packets to all hosts on the local network. The local broadcast address of IPv4 is "255.255.255.255". The router will not forward this broadcast.

1. Advantages of broadcasting:

(1) High efficiency of communication, information 1 down can be transmitted to a network of all the host.

(2) Because the server does not send data to each client separately, the server traffic is relatively low;

2. Disadvantages of broadcasting:

(1) It takes up a lot of network bandwidth;

(2) Lack of pertinence, and forced to receive data regardless of whether the host really needs to receive the data;

3. Application Scenario:

(1) Cable TV is a typical broadcast network

4. The multicast:

"Multicast" can for example is: you in the street Shouting: "is the man's to 1, 1 person to send 100 pieces", so the man and woman will not come, because no money she ignore you (multicast: including all of the boys is group 1), in other words: multicast is host to specify 1 set of host sends a datagram packet, because if used unicast way, each node transmission, how much a target node, how many times will be transfer process, this way is obviously efficiency is extremely low, it is not advisable; If we adopt the broadcast mode that does not distinguish the target and sends all the data, although the data can be transmitted once, it obviously cannot achieve the purpose of distinguishing the specific data receiving object, and will occupy the network bandwidth. The multicast method can realize one transmission station

Data with target nodes can also be transferred to specific objects.

Multicast of IP network is generally realized by multicast IP address. Multicast IP address is the D class IP address, which is the IP address between 224.0.0.0 and 239.255.255.255.

1. Advantages of multicast:

(1) All the advantages of broadcasting;

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

2. Disadvantages of multicast:

(1) Compared with unicast protocol, there is no error correction mechanism, so it is difficult to make up for packet loss, but it can be made up by the fault-tolerant mechanism and QOS.

5. Application Examples:

1. The example of UDP unicast


import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 
import java.net.InetSocketAddress; 
import java.net.SocketAddress; 
 
//  The client  
public class ClientTest 
{ 
 private static final int MAXRECEIVED = 255; 
 
 public static void main(String[] args) throws IOException 
 { 
  byte[] msg = new String("connect test successfully!!!").getBytes(); 
 
  DatagramSocket client = new DatagramSocket(); 
 
  InetAddress inetAddr = InetAddress.getLocalHost(); 
  SocketAddress socketAddr = new InetSocketAddress(inetAddr, 8888); 
 
  DatagramPacket sendPacket = new DatagramPacket(msg, msg.length, 
    socketAddr); 
 
  client.send(sendPacket); 
 
  client.close(); 
 } 
} 
 
 

Server:


import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.util.Arrays; 
 
// The service side  
public class ServerTest 
{ 
 private static final int MAXREV = 255; 
 
 public static void main(String[] args) throws IOException 
 { 
  DatagramSocket server = new DatagramSocket(8888); 
  DatagramPacket recvPacket = new DatagramPacket(new byte[MAXREV], MAXREV); 
 
  while (true) 
  { 
   server.receive(recvPacket); 
 
   byte[] receiveMsg = Arrays.copyOfRange(recvPacket.getData(), 
     recvPacket.getOffset(), 
     recvPacket.getOffset() + recvPacket.getLength()); 
 
   System.out.println("Handing at client " 
     + recvPacket.getAddress().getHostName() + " ip " 
     + recvPacket.getAddress().getHostAddress()); 
 
   System.out.println("Server Receive Data:" + new String(receiveMsg)); 
 
   server.send(recvPacket); 
 
  } 
 
 } 
} 

2. Examples of UDP broadcasting


import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 
 
// The client  
public class BroadcastSender 
{ 
 public static void main(String[] args) throws IOException 
 { 
  byte[] msg = new String("connection successfully!!!").getBytes(); 
  /* 
   *  in Java UDP The code for unicast and broadcast is the same , To implement a program with broadcast functionality, you only need to use the broadcast address ,  For example, the local broadcast address is used here  
   */ 
  InetAddress inetAddr = InetAddress.getByName("255.255.255.255"); 
  DatagramSocket client = new DatagramSocket(); 
 
  DatagramPacket sendPack = new DatagramPacket(msg, msg.length, inetAddr, 
    8888); 
 
  client.send(sendPack); 
  System.out.println("Client send msg complete"); 
  client.close(); 
 } 
} 

 
import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.util.Arrays; 
 
// The service side  
public class BroadcastReceive 
{ 
 public static void main(String[] args) throws IOException 
 { 
 
  DatagramPacket receive = new DatagramPacket(new byte[1024], 1024); 
  DatagramSocket server = new DatagramSocket(8888); 
 
  System.out.println("---------------------------------"); 
  System.out.println("Server current start......"); 
  System.out.println("---------------------------------"); 
 
  while (true) 
  { 
   server.receive(receive); 
 
   byte[] recvByte = Arrays.copyOfRange(receive.getData(), 0, 
     receive.getLength()); 
 
   System.out.println("Server receive msg:" + new String(recvByte)); 
  } 
 
 } 
} 

3. Example of UDP multicast


import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.InetAddress; 
import java.net.MulticastSocket; 
 
// The client  
public class MulticastSender 
{ 
 public static void main(String[] args) throws IOException 
 { 
  int port = 8888; 
  byte[] msg = "Connection successfully!!!".getBytes(); 
 
  InetAddress inetRemoteAddr = InetAddress.getByName("224.0.0.5"); 
 
  /* 
   * Java UDP Multicast applications are primarily through MulticastSocket Instance communication , It is a DatagramSocket is 1 A subclass , 
   *  It contains 1 Some additional properties that can control the multicast . 
   * 
   *  Note:  
   * 
   *  Multicast datagram packets can actually pass through DatagramSocket send , Simply specify 1 Multiple multicast addresses.  
   *  We'll use here MulticastSocket, Because it has DatagramSocket Ability to do without  
   */ 
  MulticastSocket client = new MulticastSocket(); 
 
  DatagramPacket sendPack = new DatagramPacket(msg, msg.length, 
    inetRemoteAddr, port); 
 
  client.send(sendPack); 
 
  System.out.println("Client send msg complete"); 
 
  client.close(); 
 
 } 
} 

 
 
import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.InetAddress; 
import java.net.MulticastSocket; 
import java.util.Arrays; 
 
// The service side  
public class MulticastReceive 
{ 
 public static void main(String[] args) throws IOException 
 { 
  InetAddress inetRemoteAddr = InetAddress.getByName("224.0.0.5"); 
 
  DatagramPacket recvPack = new DatagramPacket(new byte[1024], 1024); 
 
  MulticastSocket server = new MulticastSocket(8888); 
 
  /* 
   *  If you are sending datagram packets , You can not join a multicast group ;  If you are receiving datagram packets , You must join a multicast group ;  This is receiving datagram packets , So you have to join a multicast group ; 
   */ 
  server.joinGroup(inetRemoteAddr); 
 
  System.out.println("---------------------------------"); 
  System.out.println("Server current start......"); 
  System.out.println("---------------------------------"); 
 
  while (true) 
  { 
   server.receive(recvPack); 
 
   byte[] recvByte = Arrays.copyOfRange(recvPack.getData(), 0, 
     recvPack.getLength()); 
 
   System.out.println("Server receive msg:" + new String(recvByte)); 
  } 
 
 } 
} 

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: