我现在正在研究Java网络编程,我编写了一个程序,客户端向服务器发送当前时间的30倍,服务器将创建一个新线程来解析接收到的UDP数据包,并在服务器收到UDP时向客户端提供反馈包。
问题是,在我运行代码之后,服务器可以接收UDP数据包并创建一个新线程,但是似乎DatagramSocket
和DatagramPacket
没有传递给该线程。因此,线程无法向客户端提供反馈,因此客户端在发送第一个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 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();
}
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
}
}
}
答案 0 :(得分:1)
您的逻辑中存在缺陷,当您执行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);
}
该代码完全是出于我的想法而编写的,我没有检查正确的语法
答案 1 :(得分:0)
在客户端中删除“ Thread.sleep(10)”