Socket communication based on UDP protocol
Introduction to this section:
This section brings you the last section of Socket: Socket communication based on UDP protocol. In the first section, we have detailed Comparing the differences between the two, the biggest difference between TCP and UDP is whether the client needs to establish a connection with the server before proceeding. Data transmission, if you have learned TCP in the first two sections, open the server first before transmission, accept, wait for the client to access, and then obtain The client socket then performs IO operations, but UDP does not. UDP uses datagrams as the transmission carrier of data. When transmitting First, the transmitted data must be defined as a datagram (Datagram), and the Socket (host address) to which the data should arrive is specified in the datagram. and port number), and then send the data in the form of datagrams, and then there is nothing more. If the server cannot receive it, I will I don’t know, unless the server sends me a confirmation datagram after receiving it~ I won’t write another Android example due to time constraints. Directly enter the Java code~
1. Server-side implementation steps:
Step 1: Create DatagramSocket and specify the port number
Step 2: Create DatagramPacket
Step 3: Receive data information sent by the client
Step 4: Read data
Sample code:
public class UPDServer { public static void main(String[] args) throws IOException { /* * 接收客户端发送的数据 */ // 1.创建服务器端DatagramSocket,指定端口 DatagramSocket socket = new DatagramSocket(12345); // 2.创建数据报,用于接收客户端发送的数据 byte[] data = new byte[1024];// 创建字节数组,指定接收的数据包的大小 DatagramPacket packet = new DatagramPacket(data, data.length); // 3.接收客户端发送的数据 System.out.println("****服务器端已经启动,等待客户端发送数据"); socket.receive(packet);// 此方法在接收到数据报之前会一直阻塞 // 4.读取数据 String info = new String(data, 0, packet.getLength()); System.out.println("我是服务器,客户端说:" + info); /* * 向客户端响应数据 */ // 1.定义客户端的地址、端口号、数据 InetAddress address = packet.getAddress(); int port = packet.getPort(); byte[] data2 = "欢迎您!".getBytes(); // 2.创建数据报,包含响应的数据信息 DatagramPacket packet2 = new DatagramPacket(data2, data2.length, address, port); // 3.响应客户端 socket.send(packet2); // 4.关闭资源 socket.close(); } }
2. Client implementation steps:
Step 1: Definition Send information
Step 2: Create DatagramPacket, containing the information to be sent
Step 3: Create DatagramSocket
Step 4: Send data
public class UDPClient { public static void main(String[] args) throws IOException { /* * 向服务器端发送数据 */ // 1.定义服务器的地址、端口号、数据 InetAddress address = InetAddress.getByName("localhost"); int port = 8800; byte[] data = "用户名:admin;密码:123".getBytes(); // 2.创建数据报,包含发送的数据信息 DatagramPacket packet = new DatagramPacket(data, data.length, address, port); // 3.创建DatagramSocket对象 DatagramSocket socket = new DatagramSocket(); // 4.向服务器端发送数据报 socket.send(packet); /* * 接收服务器端响应的数据 */ // 1.创建数据报,用于接收服务器端响应的数据 byte[] data2 = new byte[1024]; DatagramPacket packet2 = new DatagramPacket(data2, data2.length); // 2.接收服务器响应的数据 socket.receive(packet2); // 3.读取数据 String reply = new String(data2, 0, packet2.getLength()); System.out.println("我是客户端,服务器说:" + reply); // 4.关闭资源 socket.close(); } }
Summary of this section:
The content of this section is relatively simple. It is nothing more than converting the data into bytes and then putting it into the DatagramPacket(datagram package) ), sent When receiving, bring the IP address and port number of the recipient, and when receiving, use a byte array to cache! When sending, you need to create a DatagramSocket (end-to-end communication class) object, and then call the send method to send the datagram packet to the recipient~ The code in this section comes from a JavaSocket tutorial on Muke.com~ If you are interested, you can take a look: Java Socket Application---This is how communication is practiced