Java serialization 129 broadcast packets, network programming summary

1, Broadcast packet

1. characteristics

  • This kind of communication is similar to broadcasting. To realize this function, a special IP address needs to be used. To realize multicast or broadcast communication, the host must add a class D address. The decimal representation range of class D address is 224.0.0.0~239.255.255.255
  • The required class is `java Net Multicastsocket
  • Common building methods
    (1)MulticastSocket(int port) throws IOException
    Create a multicast socket and bind it to the port.
    (2)MuticastSocket(SocketAddress bindaddr) throws IOException
    Create a multicast socket and bind it to a specified socket address\
  • Common member methods:
    (1)public void joinGroup(InetAddress mcastaddr)
    Adds a multicast socket to the specified multicast group.
    (2)public void leaveGroup(InetAddress mcastaddr)
    Move a multicast socket out of a multicast group.
    (3)public void setTimeToLive(int ttl) throws IOException
    Set the default lifetime of multicast packets issued on this musicastsocket\
  • The ttl parameter sets the maximum number of networks a packet can span:
    (1) ttl=0, the packet should stay in the local host;
    (2) ttl=1, the data packet should be sent to the local LAN;
    (3) ttl=32, the data packet should be sent to the network of the site;
    (4) ttl=64, data packets should be kept in the local area;
    (5) ttl=128, data packets should be kept in the continent;
    (6) ttl=255, data packets should be sent to all places\
  • Write a sender first
package com.bjpowernode.java_learning;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class D129_1_BroadcastTest {
 public static void main(String[] args) {
  new D129_Broadcast().send();
 }
}
class D129_Broadcast {
 public void send() {
  String msg = "Hello,This is Broadcast Message";         //Multicast content
  int port = 6666;
  InetAddress group = null;
  MulticastSocket ms = null;
  
  try {
   group = InetAddress.getByName("224.1.1.1");         //Create multicast address
   ms = new MulticastSocket(port);                     //Create multicast socket
   ms.joinGroup(group);                                //Add socket to multicast address
   ms.setTimeToLive(1);                                //Set datagram sending range to local
   DatagramPacket dp = new DatagramPacket(msg.getBytes(),msg.length(),group,port);//Create datagrams to be sent
   ms.send(dp);                                        //Send datagram
  }catch(IOException e) {
   System.out.println(e);
  }finally {
   ms.close();                                         //Close socket
  }
  
 }
}
  • Write another receiver program
package com.bjpowernode.java_learning;

import java.net.DatagramPacket;

public class D129_2_ReceiverTest {
 public static void main(String[] args) {
  new D129Receiver().receive();
 }
}
class D129Receiver{
 public void receive() {
  byte[] data = new byte[1024];                //Data buffer
  int port = 6666;                             //Multicast port number
  InetAddress group = null;
  MuticastSocket ms = null;
  
  try {
   group = InetAddress.getName("224.1.1.1"); //Create multicast address
   ms = new MulticastSocket(port);           //Create multicast socket
   ms.joinGroup(group);                      //Add socket to multicast address
   DatagramPacket dp = new DatagramPacket(data,data,length,group,port);
               //Create datagrams to be received
   ms.receive(dp);                           //Receive datagram
   String msg = new String(dp.getData(),0,dp.getLength());
   System.out.println("The received data broadcast data is:"+msg);
   
   
  }catch(IOException e) {
   System.out.println(e);
  }finally {
   ms.close();                               //Close socket
  }
 }
}
  • First, run the receiving end, and then run the broadcasting end. The broadcasting will send "Hello,This is Broadcast Message", and the client will print the message after receiving it.

Summary:
(1) What are the differences between TCP and UDP? TCP is a connection oriented flow transmission control protocol with high reliability to ensure the correctness of the transmitted data. It has a verification and retransmission mechanism to ensure that the transmitted data will not be lost or out of order.
UDP is a connectionless data service. It does not check and modify the integrity of data. It does not need to wait for the other party's response. Packet loss, duplication and disorder may occur. However, it has high transmission efficiency and good real-time performance. It is suitable for internal transmission in LAN. The message protocol header is simple.
(2) Brief introduction of Socket network programming process
The first is the client. First, create a Socket instance, use the construction method to bind the address and port to be communicated, then communicate through the Socket I/O flow, and finally receive the communication. Call the member method close() to close the Socket;
Then on the server side, first create a ServerSocket instance, and use the construction method to bind the local IP address and port number to listen to the TCP requests that will be received. Then do the following loop: call the member method accept() to get the connection of the client, and return a Socekt instance. Create a thread for the returned Socket instance and use the I\O flow of the returned Socket to communicate with the client; Finally, use the member method to close.

3, Source code:

    • D129_1_BroadcastTest.java
    • D129_2_ReceiverTest.java
    • https://github.com/ruigege66/Java/blob/master/D129_1_BroadcastTest.java
    • https://github.com/ruigege66/Java/blob/master/D129_2_ReceiverTest.java
    • CSDN: https://blog.csdn.net/weixin_44630050
    • Blog Park: https://www.cnblogs.com/ruigege0000/
    • Welcome to wechat official account: Fourier transform, personal account. It is only used for technical exchange. The background replies to the "gift package" to obtain the Java big data learning video gift package

Tags: Java

Posted by christillis on Tue, 31 May 2022 02:43:54 +0530