简体   繁体   English

多线程服务器从客户端Java接收数据

[英]Multithreading server receives data from clients java

I'm working on project where i have a multithreading server who handle multiple clients but there are two type of clients(administrator and conferencier) and i'm stuck with a problem i have created a server and when a client is accepted there are condition if the client is administrator or conferencier beacause each of this different clients has his own handler classe (function) but the code work only with the first condition it's not working for both 我在一个项目中工作,我有一个处理多个客户端的多线程服务器,但是有两种类型的客户端(管理员和会议者),但我遇到了问题,我已经创建了服务器,并且当一个客户端被接受时,情况如果客户端是管理员或协商者,因为每个不同的客户端都有自己的处理程序类(函数),但是代码仅在第一个条件下起作用,因此不适用于两者

My server code: 我的服务器代码:

try {

  ss = new ServerSocket(portnb);
  System.out.println("le serveur wainting for client");

} catch (Exception e) {
  System.out.println(e.getMessage());
}

while (true) {
  try {        
    clientsocket = ss.accept();
    BufferedReader in = new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
    if (in.readLine().equals("calculeID"))// administrator function{
      Handler h = new Handler(clientsocket);
      new Thread(h).start();
      System.out.println("administrator is here ");
      in.close();
    }else{  
      if (in.readLine().equals("start"))// conferencier function{            
        Handlerconf hc= new Handlerconf(clientsocket);
        new Thread (hc).start();
        System.out.println("conferencier is here ");
        in.close();
      }
    }
  } catch (Exception e) {
    System.out.println(e.getMessage());
  }
  ss.close();
}

Each call to readLine() reads a new line. 每次调用readLine()读取一个行。 Call readLine() once , and store the result in a variable. 调用一次 readLine() ,然后将结果存储在变量中。 Then test the value of this variable. 然后测试此变量的值。

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

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