Java多线程UDP服务器错误

Java多线程UDP服务器错误,java,udp,Java,Udp,我用Java编写UDP服务器和客户端,以便将消息从客户端发送到服务器。在我的例子中,来自客户端的每条消息都将存储在数据库中。我尝试使用固定线程池创建多线程UDP服务器。但现在我遇到了一个问题,我发现来自客户端的一条消息可以被多个线程处理。我的服务器如下所示: DatagramSocket serverSocket = new DatagramSocket(9876); Map<String, Integer> retryMap = new ConcurrentHash


我用Java编写UDP服务器和客户端,以便将消息从客户端发送到服务器。在我的例子中,来自客户端的每条消息都将存储在数据库中。我尝试使用固定线程池创建多线程UDP服务器。但现在我遇到了一个问题,我发现来自客户端的一条消息可以被多个线程处理。我的服务器如下所示:

    DatagramSocket serverSocket = new DatagramSocket(9876);
    Map<String, Integer> retryMap = new ConcurrentHashMap<>();
    byte[] receiveData = new byte[1024];

    ExecutorService executor = Executors.newFixedThreadPool(2);

    while(true){
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        executor.execute(new ThreadServer(receivePacket, retryMap));
    }
public class UDPClient1{
String message;
public UDPClient1(String message){
    this.message = message;
    try{
        byte[] sendData;

        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName("localhost");

        sendData = message.getBytes("UTF-8");

        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
        clientSocket.send(sendPacket);

        clientSocket.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

public static void main(String args[]) throws InterruptedException{
    int i = 0;
    while(i < 1000){
        new UDPClient1("T000"+i+"_"+i);
        System.out.println(i);
        i++;
        Thread.sleep(2);
    }
}}
我的客户是这样的:

    DatagramSocket serverSocket = new DatagramSocket(9876);
    Map<String, Integer> retryMap = new ConcurrentHashMap<>();
    byte[] receiveData = new byte[1024];

    ExecutorService executor = Executors.newFixedThreadPool(2);

    while(true){
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        executor.execute(new ThreadServer(receivePacket, retryMap));
    }
public class UDPClient1{
String message;
public UDPClient1(String message){
    this.message = message;
    try{
        byte[] sendData;

        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName("localhost");

        sendData = message.getBytes("UTF-8");

        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
        clientSocket.send(sendPacket);

        clientSocket.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

public static void main(String args[]) throws InterruptedException{
    int i = 0;
    while(i < 1000){
        new UDPClient1("T000"+i+"_"+i);
        System.out.println(i);
        i++;
        Thread.sleep(2);
    }
}}
消息T000975由线程9和18处理。。有谁能帮我解决这个问题吗?:)


您在所有
数据包中共享相同的字节数组。
将这一行移到接收循环中。

谢谢,它起作用了:)您能解释一下将ReceiveData声明放在循环外和循环内有什么区别吗?我已经回答了您在所有
数据包之间共享相同的字节数组。
'如果将其移动到循环中,则每个数据包都有一个新的字节数组。
byte[] receiveData = new byte[1024];