gpt4 book ai didi

java - Java UDP多线程通信的一个问题

转载 作者:行者123 更新时间:2023-12-02 10:33:54 25 4
gpt4 key购买 nike

我现在正在学习java网络编程,我写了一个程序,客户端向服务器发送当前时间的30倍,服务器将创建一个新线程来解析接收到的UDP数据包,如果服务器收到UDP则反馈给客户端数据包。

问题是,在我运行代码后,服务器可以接收UDP数据包并创建一个新线程,但似乎DatagramSocketDatagramPacket没有传递给线程。因此该线程无法向客户端反馈,客户端在发送第一个UDP数据包后将一直等待。

我的代码在这里:

服务器

public class MulUDPServer {
public static void main(String[] args) {
DatagramSocket socket = null;
DatagramPacket receivedPacket;
final int PORT = 10010;
byte[] b = new byte[1024];
receivedPacket = new DatagramPacket(b, b.length);
try {
socket = new DatagramSocket(PORT);
System.out.println("Server start!");
while (true) {
// receive the packet from server
socket.receive(receivedPacket);
// to check if Server get the packet
System.out.println(new String(receivedPacket.getData(), 0, receivedPacket.getLength()));
// start the thread to handle the packet we have got
Thread thread = new Thread(new LogicThread(socket, receivedPacket));
thread.start();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// close the connection
socket.close();
} catch (Exception e) {
}
}
}

话题

public class LogicThread implements Runnable {

DatagramSocket socket = null;
DatagramPacket receivedPacket = null;

public LogicThread(DatagramSocket socket, DatagramPacket receivedPacket) {
this.socket = socket;
this.receivedPacket = receivedPacket;
}

public void run() {
try {
// to test if a thread have been set up
System.out.println("a thread have been set up");
byte[] data = receivedPacket.getData();
int len = receivedPacket.getLength();
// get the client IP
InetAddress clientAddress = receivedPacket.getAddress();
// get the client port
int clientPort = receivedPacket.getPort();
// print the info about received packet
System.out.println("Client's IP:" + clientAddress.getHostAddress());
System.out.println("Client's port:" + clientPort);
System.out.println("The info:" + new String(data, 0, len));
// feedback to Client
byte[] b = "OK".getBytes();
DatagramPacket sendPacket = new DatagramPacket(b, b.length, clientAddress, clientPort);
// send
socket.send(sendPacket);
} catch (Exception e) {
e.printStackTrace();
}
}
}

客户端

public class MulUDPClient {
public static void main(String[] args) {
DatagramSocket socket = null;
DatagramPacket sendPacket;
DatagramPacket receivedPacket;
String serverHost = "localhost";
int serverPort = 10010;
try {
socket = new DatagramSocket();
InetAddress address = InetAddress.getByName(serverHost);
byte[] b = new byte[1024];
receivedPacket = new DatagramPacket(b, b.length);
System.out.println("Client ready!");
for (int i = 0; i < 30; i++) {
// get the current time
Date d = new Date();
String content = d.toString();
byte[] data = content.getBytes();
sendPacket = new DatagramPacket(data, data.length, address, serverPort);
socket.send(sendPacket);
System.out.println("already send time");
Thread.sleep(10);

// receive packet from Server
socket.receive(receivedPacket);
byte[] response = receivedPacket.getData();
int len = receivedPacket.getLength();
String s = new String(response, 0, len);
System.out.println("the feedback from Server:" + s);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// close the connection
socket.close();
} catch (Exception e) {
}
}
}
}

在两个终端分别运行Server和Client后的**结果**:

服务器

Server start!
Fri Nov 23 14:52:02 CST 2018
a thread have been set up

客户端

Client ready!
already send time

从结果中我们可以知道,客户端发送了一个UDP数据包,服务器正确解析了它并创建了一个线程。然后程序正在等待...

困扰了我几天,谁能帮我解决一下? :)。谢谢!

编辑

客户端

**不能**工作

for (int i = 0; i < 30; i++) {
Thread writerWorker = new WriterWorker(socket);
writerWorker.start();
Thread readerWorker = new ReaderWorker(socket);
readerWorker.start();
}

**可以**工作

for (int i = 0; i < 30; i++) {
Date d = new Date();
String content = d.toString();
byte[] data = content.getBytes();
sendPacket = new DatagramPacket(data, data.length, address, serverPort);
socket.send(sendPacket);
Thread.sleep(10);
Thread readerWorker = new ReaderWorker(socket);
readerWorker.start();
}

作家 worker

public class WriterWorker extends Thread {
DatagramSocket socket;
String serverHost = "localhost";
int serverPort = 10000;
DatagramPacket sendPacket;

public WriterWorker(DatagramSocket socket) {
this.socket = socket;
}

@Override
public void run() {
try {
InetAddress address = InetAddress.getByName(serverHost);
Date d = new Date();
String content = d.toString();
byte[] data = content.getBytes();
sendPacket = new DatagramPacket(data, data.length, address, serverPort);
socket.send(sendPacket);
System.out.println("already send time");

} catch (Exception e) {
// TODO: handle exception
}
}
}

最佳答案

你的逻辑有一个缺陷,当你执行socket.receive(receivedPacket);时,这会让工作线程等待,直到一些数据报包到达......

通过套接字处理异步通信的正确方法如下(这里是客户端的示例)

socket = new DatagramSocket();
Thread readerWorker = new ReaderWorker(socket);
readerWorker.start();

Thread writerWorker = new WriterWorker(socket);
writerWorker.start();

这样你就可以将读取和写入分离到不同的线程中,并且阻塞方法 socket.receive(...) 不会停止你的写入线程...

每个工作人员都会实现自己的工作循环

作者循环:

while(true){
if (sendPacket!= null){
socket.send(sendPacket);
}
Thread.sleep(10);
}

读者循环:

while(true){
socket.receive(receivedPacket);
handlePacket(receivedPacket);
}

注意:

该代码完全是出于我的想法而编写的,我没有检查正确的语法

关于java - Java UDP多线程通信的一个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53442273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com