Java learning summary (thirteen) - network programming based on UDP protocol

one. UDP network programming
1. Connectionless data transmission, unreliable, but high efficiency (audio, video, etc.).
2. The data sent by UDP at one time cannot exceed 64kb.
3. The classes required for UDP programming
(1) DatagramSocket This The class represents the socket used to send and receive datagram packets
(2) DatagramPacket This class represents the datagram packet
method: DatagramPacket(byte[] buf, int length, InetAddress address, int port)
Parameter representation: buf - packet data
length - Packet length address - Destination
address
port - Destination port 5. Common methods of DatagramPacket (1) Construction method: public DatagramPacket(byte[] buf, int length) This construction method is used to receive data (2) public DatagramPacket(byte[] buf, int length, InetAddress address, int port) This The constructor is used to assemble the data to be sent








(3) public InetAddress getAddress(): returns the IP of the ignor to be sent or the IP of the received data
(4) public byte[] getData(): returns the data buffer to be sent or the data buffer that has been received
( 5) public int
getLength
( ): returns the length of the data to be sent or the length of the data received Establish a connection; #. When the connection is established, there is a primary and secondary distinction between the two parties
(2) Socket programming based on UDP protocol: #. Double communication does not need to establish a connection; #. Both parties are completely equal
7. Socket programming based on UDP protocol
(1 ) UDP Socket communication model:
Java learning summary (thirteen) - network programming based on UDP protocol
Example (UDP example):
Information sending thread class:
package udp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java .net.DatagramSocket;
import java.net.InetAddress;
//Send thread class
public class SendRunnable implements Runnable{
private String destHost;
private int port;
private DatagramSocket ds;

public SendRunnable(String destHost, int port, DatagramSocket ds) {
    this.destHost = destHost;
    this.port = port;
    this.ds = ds;
}

@Override
public void run() {
    while(true){
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print(Thread.currentThread().getName()+":");
        String say;
        DatagramPacket dp=null;
        try {
            say=br.readLine();
            dp=new DatagramPacket(say.getBytes(),say.getBytes().length ,InetAddress.getByName(destHost), port);
            ds.send(dp); 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

Information receiving thread class:
package udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/接收线程类
/
public class ReceiveRunnable implements Runnable {
private DatagramSocket ds;

public ReceiveRunnable(DatagramSocket ds) {
    this.ds = ds;
}

@Override
public void run() {
    while (true) {
        byte[] b = new byte[1024];
        DatagramPacket dp = new DatagramPacket(b, b.length);
        try {
            ds.receive(dp);// 接收之前一直阻塞
            String ip = dp.getAddress().getHostName();// 获取发送方的IP
            int post = dp.getPort();// 获取发送方的端口号
            System.out.println(ip + ":" + post + "说:"
                    + new String(dp.getData(), 0, dp.getLength()));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

}

Chat port one:
package udp;

import java.net.DatagramSocket;
import java.net.SocketException;
class Chat1 {

public static void main(String[] args) throws SocketException {
    DatagramSocket ds=new DatagramSocket(9999);
    new Thread(new ReceiveRunnable(ds)).start();
    new Thread(new SendRunnable("localhost", 8888, ds),"白雪公主").start();
}

}

Chat port two:
package udp;

import java.net.DatagramSocket;
import java.net.SocketException;

public class Chat2 {

public static void main(String[] args) throws SocketException {
    DatagramSocket ds=new DatagramSocket(8888);
    new Thread(new ReceiveRunnable(ds)).start();
    new Thread(new SendRunnable("localhost", 9999, ds),"青蛙王子").start();
}

}

Running result Snow White :
127.0.0.1:8888 said : hello
, princess Okay, princess frog prince: 127.0.0.1:9999 said: hello, what are you doing, prince, princess frog prince: 127.0.0.1:9999 said: I'm chatting with you







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324489944&siteId=291194637