Introduction to This Section:
This section brings you the last part of Socket: Socket communication based on the UDP protocol. In the first section, we have already compared the differences between the two in detail. The biggest difference between TCP and UDP is whether the client and server need to establish a connection before data transmission. If you have learned TCP from the previous two sections, before transmission you start the server, accept, wait for the client to connect, then obtain the client socket and perform IO operations. But UDP does not need this. UDP uses datagrams as the carrier for data transmission. When transmitting, you first define the data to be transmitted as a datagram (Datagram), specify the Socket (host address and port number) that the data needs to reach in the datagram, then send the data out in the form of a datagram. And then there is nothing more. I don't know whether the server receives it or not, unless the server sends back a confirmation datagram after receiving it~ Due to time constraints, I won't write a separate Android example, so here is the Java code directly~
1. Server-side implementation steps:
Step 1: Create a DatagramSocket and specify the port number
Step 2: Create a DatagramPacket
Step 3: Receive data information sent by the client
Step 4: Read the data
Example 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-side implementation steps:
Step 1: Define the sending information
Step 2: Create a DatagramPacket containing the information to be sent
Step 3: Create a DatagramSocket
Step 4: Send the 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:
Step 1: Define the sending information
Step 2: Create a DatagramPacket containing the information to be sent
Step 3: Create a DatagramSocket
Step 4: Send the data
The content of this section is relatively simple. It is just converting data into bytes, then putting them intoDatagramPacket(the datagram packet). When sending, include the recipient's IP address and port number; when receiving, use a byte array to buffer! When sending, you need to create aDatagramSocket(the class for end-to-end communication) object, then call the send method to send the datagram packet to the recipient~ The code in this section comes from a Java Socket tutorial on Muke Network. If you are interested, you can take a look:Java Socket Application --- Communication Is Practiced Like This