【问题标题】:is it possible to put input data from user into one thread and input data from server into another(UDP)?是否可以将用户的输入数据放入一个线程并将服务器的数据输入另一个(UDP)?
【发布时间】:2012-04-05 15:01:39
【问题描述】:

我有 UDPserver 等待客户端的请求并发送响应给他们。为了检查客户是否在线,我需要向他们发送消息。(检查)。 但是!在客户端的电脑上,用户可以选择一些消息并将其发送到服务器,然后等待服务器的响应。如何制作这样的东西 - 用户可以向服务器发送消息,但同时客户端可以从服务器接收消息?将客户端的 readLine 和 @987654322 放在一个线程中@ 变成另一个?

这里是客户端的主循环(ds - DatagramSocket.Client - 我自己的类)

while(true){
        try{
            //variable client we use it in every condition
            Client obj = null;
            ds = null;
            //REGR - registration FNDI - find ip FNDM - find mask GETL - get all CHKA - check all
            System.out.println("PORT - ");
            int port = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
            ds = new DatagramSocket(port);
            //wait for user's choice
            System.out.println("Commands - REGR,FNDI,FNDM,GETL,CHKA");
            String cmd = new BufferedReader(new InputStreamReader(System.in)).readLine();
            if (cmd.equals("REGR"))
            {
                //sending data to server and waiting for its response
            }
            else if (cmd.equals("FNDI"))
            {
                //same
            }   
            else if (cmd.equals("FNDM"))
            {
                //same
            }
            else if (cmd.equals("GETL"))
            { 
                //same
            }
            else if(cmd.equals("CHKA"))//CHKA
            {
                //same
            }
        }catch(Exception exc)
        {
            System.out.println(exc);
            return;
        }
        finally
        {
            if (ds!=null)
                ds.close();
        }
}

主服务器的循环

  while(!stop){
        myfile.write(aam.InputMsg.name());
        System.out.println(aam.InputMsg);
        DatagramPacket pack = new DatagramPacket(buf,buf.length);
        socket.receive(pack);
        //ports.add(pack.getPort());
        //object from pack
        Object o = Deserialize.Deserialization(buf);
        //ok. first - REGR - so check on Client
        if (o instanceof Client)//registration
        {
            //perform some task
        }
        else if (o instanceof InetAddress)//find client by IP
        {
            //same
        }
        else if (o instanceof String)//different messages - name to find, start test.
        {   
            //same
        }
        else
        {
            throw new Exception("SOME WRONG DATA FROM CLIENT");
        }
    }
    System.out.println("Server stopped");
}
catch(Exception exc)
{
    System.out.println(exc);
}
finally{
    if (socket!=null){
        socket.close();
    }
}
}

【问题讨论】:

    标签: java udp


    【解决方案1】:

    在 C/C++ 套接字中,您通常会使用“select()”调用来处理发生在文本中的任何输入 - 键盘、文件或网络。

    在 Java 套接字中,您可能会考虑查看“nio”:

    http://tutorials.jenkov.com/java-nio/nio-vs-io.html

    【讨论】:

      【解决方案2】:

      是的,您至少需要将客户端的一部分放到另一个线程中。

      可以通过将客户端输入循环放入单独的线程来开始。

      Thread t_input = new Thread() {
          public void run() {
              BufferedReader br_in = new BufferedReader(new InputStreamReader(System.in));
              String cmd = br_in.readLine();
              while(cmd != null) {
                  if(cmd.equals("exit")) { //stop the user input
                      client_Shutdown();
                      break;
                  }
                  //handle other user input
                  cmd = br_in.readLine();
              }
          }
      };
      
      t_input.start();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-19
        • 2011-08-28
        • 2023-02-10
        相关资源
        最近更新 更多