简体   繁体   English

多线程服务器和客户端Java指定客户端线程

[英]Multi threaded server and client Java specify the client thread

I made a UDP server client architecture with multi-thread the problem is when I send from the server to the client another client thread get the packet and so for all .. how can I specify the thread in which UDP should send back the packet ??我使用多线程创建了一个 UDP 服务器客户端架构,问题是当我从服务器向客户端发送另一个客户端线程时,另一个客户端线程会获取数据包等等。 ?

 Client

 public UDPClient(int port) throws SocketException
{
    this.socket = new DatagramSocket();
    this.arg1 = (int) (Math.random() * 1000) ;
    this.arg2 = (int) (Math.random() * 1000) ;
    this.port = port ;
}
    public void run()
{
    try{
        String x = arg1 + " + " + arg2;
        BUFFER = x.getBytes();
        InetAddress ip = InetAddress.getByName("127.0.0.1");
        packet = new DatagramPacket(BUFFER , BUFFER.length,ip,port);
        printOutput(new String("Client send" + x));
        socket.send(packet);
        socket.receive(packet);
        String output = new String(packet.getData(),0,packet.getLength());
        printOutput(new String("receive " + x + "=" + output));
    }
    catch(IOException e)
    {
        System.out.println("UDP sending problem " + e.getMessage());
    }
}

Server服务器

public void run()
{
    while(true)
    {
        try{
            packet = new DatagramPacket(BUFFER,BUFFER.length);
            socket.receive(packet);
            executor.execute(new UDPServerCore(socket,packet,BUFFER));
        }
        catch(IOException e)
        {
            System.out.println("UDP receiving packet problem "
            + e.getMessage());
        }
    }
}

ServerCore服务器核心

@Override
public void run(){
    String x = new String(packet.getData(),0,packet.getLength());
    String y = parseString(x);
    BUFFER = y.getBytes();
    //packet.setData(y.getBytes());
    DatagramPacket res = new DatagramPacket(BUFFER , BUFFER.length 
    ,packet.getAddress(),packet.getPort());
    try{
    socket.send(res);
    }
    catch(IOException e)
    {
        System.out.println("Something went wrong " + e.getMessage());
    }
}

synchronized private static String parseString(String x )
{
    String arr[] = x.split(" ");
    int z = Integer.parseInt(arr[0]);
    int y = Integer.parseInt(arr[2]);
    y = y + z;
    writeServer.append(x+ "=" + y +"\n");
    return String.valueOf(y);
}

I solved it as the problem was when i created the port of the thread i should have create all the client threads with specified port first then starting each thread.我解决了它,因为问题是当我创建线程的端口时,我应该首先创建具有指定端口的所有客户端线程,然后启动每个线程。

Y̶o̶u̶ ̶c̶o̶u̶l̶d̶ ̶t̶r̶y̶ ̶t̶o̶ ̶a̶s̶s̶i̶g̶n̶ ̶a̶n̶ ̶I̶D̶ ̶t̶o̶ ̶t̶h̶e̶ ̶p̶a̶c̶k̶e̶t̶ ̶t̶o̶ ̶t̶r̶a̶c̶k̶ ̶t̶h̶e̶ ̶c̶u̶r̶r̶e̶n̶t̶ ̶s̶e̶s̶s̶i̶o̶n̶ ̶i̶t̶ ̶ b̶e̶l̶o̶n̶g̶s̶ ̶t̶o.你可以尝试分配一个ID,包跟踪当前会话它̶属于。 UDP as opposed to TCP is a no confirmation and no connection protocol. UDP 与 TCP 相对,是一种无确认且无连接的协议。 UDP does not support "streams" of data so you have to find a way to dispatch each packet to the corresponding "session handler". UDP 不支持数据“流”,因此您必须找到一种方法将每个数据包分派到相应的“会话处理程序”。

If you could explain more what you're trying to accomplish I think anyone here could help you more.如果你能解释更多你想要完成的事情,我认为这里的任何人都可以帮助你更多。

edit: Each udp packet contains source address and port.编辑:每个 udp 数据包都包含源地址和端口。 You can use those to recognize and store data for every client that connects.您可以使用它们来识别和存储连接的每个客户端的数据。 Hint: you could use a map / dictionary and when receiving a packet retrieve your session object using client address and port as key提示:您可以使用地图/字典,并在接收数据包时使用客户端地址和端口作为键检索您的会话对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM