Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在创建多客户端聊天服务器时未获得所需的输出?_Java_Sockets - Fatal编程技术网

Java 在创建多客户端聊天服务器时未获得所需的输出?

Java 在创建多客户端聊天服务器时未获得所需的输出?,java,sockets,Java,Sockets,我正在尝试创建一个多客户端聊天服务器,其中有多个客户端连接到服务器,无论客户端输入什么消息,它都会显示给所有客户端(包括发送消息的客户端)。我没有得到这个输出,相反,消息只在发送方客户端上回响,没有其他客户端。代码相当长,所以我将显示我认为可以帮助您理解错误的代码片段。如果这还不够,只需说明您需要哪一部分。提前谢谢。我被困在这个大约一个半小时,所以我感谢任何帮助,我会得到 服务器类 public class Multiserver { ServerSocket serversocket; So

我正在尝试创建一个多客户端聊天服务器,其中有多个客户端连接到服务器,无论客户端输入什么消息,它都会显示给所有客户端(包括发送消息的客户端)。我没有得到这个输出,相反,消息只在发送方客户端上回响,没有其他客户端。代码相当长,所以我将显示我认为可以帮助您理解错误的代码片段。如果这还不够,只需说明您需要哪一部分。提前谢谢。我被困在这个大约一个半小时,所以我感谢任何帮助,我会得到

服务器类

public class Multiserver {

ServerSocket serversocket;
Socket socket;
ArrayList<Socket> al = new ArrayList<Socket>();
DataInputStream dis;
DataOutputStream dos;

Multiserver() throws IOException
{

     serversocket = new ServerSocket(1036);
     System.out.println("Server started on port 1036");

    while(true)
    {

        socket = serversocket.accept();
        System.out.println(socket);
        al.add(socket);
        Mythread  thread = new Mythread(socket, al);
        thread.start();
    } 
}
客户端类

public class Mythread extends Thread{

Socket socket;
ArrayList al;
DataInputStream dis;
DataOutputStream dos;

Mythread(Socket socket, ArrayList al) 
{
     this.socket = socket;
     this.al = al;}

     public void run()
     {
         try{

             String data ="";
             dis = new DataInputStream(socket.getInputStream());
             data = dis.readUTF();
             if(!data.equals("stop"))
             {
                 broadcast(data);
             }
             else
             {
                 dos = new DataOutputStream(socket.getOutputStream());
                 // data = dos.readUTF();
                 dos.writeUTF(data);
                 dos.flush();
                 //dos.close();
             }

         }
         catch(Exception e){
             System.out.println("Run "+e);
         }


   }
   public void broadcast(String data)
   {
       try{
           Iterator it = al.iterator();
           while(it.hasNext())
           {
              Socket socket1 = (Socket)it.next();
               dos = new DataOutputStream(socket1.getOutputStream());
               dos.writeUTF(data);
               dos.flush();


           }
       }
       catch(Exception e){
               System.out.println("Broadcast running "+ e);
       }
   }
}
public class Multiclient {

Socket socket;
DataInputStream dis;
DataOutputStream dos;
Multiclient() throws IOException
{
    socket = new Socket("127.0.0.1", 1036);
    System.out.println(socket);
    Mythreadc my = new Mythreadc(socket);
    my.start();
}
public class Mythreadc extends Thread{

DataInputStream dis;
DataOutputStream dos;
Socket socket;
Mythreadc(Socket socket)throws IOException
{
    this.socket = socket;}

public void run()
{
    BufferedReader br = null; 
    try{
       br = new BufferedReader(new InputStreamReader (System.in));
        dos = new DataOutputStream(socket.getOutputStream());
      String data = "";
      do{

          data = br.readLine();
          dos.writeUTF(data);
          System.out.println(data);
          dos.flush();
      }
      while(!data.equals("stop"));
    }
    catch(Exception e)
    {
        System.out.println("Client input "+e);
    }
    finally{
        try{
            br.close();
            dis.close();
            dos.close();
        }
        catch(Exception e)
        {
            System.out.println("Closing "+e);
        }
    }
}    
}
public class Multiclient {

Socket socket;
DataInputStream dis;
DataOutputStream dos;
Multiclient() throws IOException
{
    socket = new Socket("127.0.0.1", 1036);
    System.out.println(socket);
    Mythreadc my = new Mythreadc(socket);
    my.start();
    /**
     * Here write out the code for taking input from Standard Console
     */
    BufferedReader br = null; 

   try{
   br = new BufferedReader(new InputStreamReader (System.in));
    dos = new DataOutputStream(socket.getOutputStream());
  String data = "";
  do{

      data = br.readLine();
      dos.writeUTF(data);
      System.out.println(data);
      dos.flush();
  }
  while(!data.equals("stop"));
}
catch(Exception e)
{
    System.out.println("Client input "+e);
}
}
客户端类中使用的线程

public class Mythread extends Thread{

Socket socket;
ArrayList al;
DataInputStream dis;
DataOutputStream dos;

Mythread(Socket socket, ArrayList al) 
{
     this.socket = socket;
     this.al = al;}

     public void run()
     {
         try{

             String data ="";
             dis = new DataInputStream(socket.getInputStream());
             data = dis.readUTF();
             if(!data.equals("stop"))
             {
                 broadcast(data);
             }
             else
             {
                 dos = new DataOutputStream(socket.getOutputStream());
                 // data = dos.readUTF();
                 dos.writeUTF(data);
                 dos.flush();
                 //dos.close();
             }

         }
         catch(Exception e){
             System.out.println("Run "+e);
         }


   }
   public void broadcast(String data)
   {
       try{
           Iterator it = al.iterator();
           while(it.hasNext())
           {
              Socket socket1 = (Socket)it.next();
               dos = new DataOutputStream(socket1.getOutputStream());
               dos.writeUTF(data);
               dos.flush();


           }
       }
       catch(Exception e){
               System.out.println("Broadcast running "+ e);
       }
   }
}
public class Multiclient {

Socket socket;
DataInputStream dis;
DataOutputStream dos;
Multiclient() throws IOException
{
    socket = new Socket("127.0.0.1", 1036);
    System.out.println(socket);
    Mythreadc my = new Mythreadc(socket);
    my.start();
}
public class Mythreadc extends Thread{

DataInputStream dis;
DataOutputStream dos;
Socket socket;
Mythreadc(Socket socket)throws IOException
{
    this.socket = socket;}

public void run()
{
    BufferedReader br = null; 
    try{
       br = new BufferedReader(new InputStreamReader (System.in));
        dos = new DataOutputStream(socket.getOutputStream());
      String data = "";
      do{

          data = br.readLine();
          dos.writeUTF(data);
          System.out.println(data);
          dos.flush();
      }
      while(!data.equals("stop"));
    }
    catch(Exception e)
    {
        System.out.println("Client input "+e);
    }
    finally{
        try{
            br.close();
            dis.close();
            dos.close();
        }
        catch(Exception e)
        {
            System.out.println("Closing "+e);
        }
    }
}    
}
public class Multiclient {

Socket socket;
DataInputStream dis;
DataOutputStream dos;
Multiclient() throws IOException
{
    socket = new Socket("127.0.0.1", 1036);
    System.out.println(socket);
    Mythreadc my = new Mythreadc(socket);
    my.start();
    /**
     * Here write out the code for taking input from Standard Console
     */
    BufferedReader br = null; 

   try{
   br = new BufferedReader(new InputStreamReader (System.in));
    dos = new DataOutputStream(socket.getOutputStream());
  String data = "";
  do{

      data = br.readLine();
      dos.writeUTF(data);
      System.out.println(data);
      dos.flush();
  }
  while(!data.equals("stop"));
}
catch(Exception e)
{
    System.out.println("Client input "+e);
}
}
很抱歉,我放了这么长的代码,几乎所有的程序。但我觉得有必要了解问题所在。我已经尝试过,我认为问题在于我们在客户端线程类中显示写入客户端套接字的数据,但我不知道它是什么


#编辑:忘记提及。当客户端发送消息“Stop”时,客户端停止

我认为您没有将当前连接到服务器的套接字用户的数组列表传递到线程

而不是发布你的服务器类,你只是发布了2次客户端程序

您的服务器类应按以下方式构建:-

只要ServerClass接收到来自任何客户端的请求,Server Class就应该将套接字添加到ArrayList中,并创建新线程,然后将两者都传递给MyThread类

编辑: 您似乎还没有编写代码来显示将从服务器获取的数据

在用于发送消息的客户端,您可以在客户端类的主方法下的主线程中简单地编写该消息

实际上,客户端需要线程,而不是用于发送消息,而是用于侦听来自服务器的消息, 因为你永远不知道什么时候任何人可以向你发送消息,但你总是知道什么时候你想向连接到此聊天应用的任何人发送消息

现在进入编码部分:

客户端类

public class Mythread extends Thread{

Socket socket;
ArrayList al;
DataInputStream dis;
DataOutputStream dos;

Mythread(Socket socket, ArrayList al) 
{
     this.socket = socket;
     this.al = al;}

     public void run()
     {
         try{

             String data ="";
             dis = new DataInputStream(socket.getInputStream());
             data = dis.readUTF();
             if(!data.equals("stop"))
             {
                 broadcast(data);
             }
             else
             {
                 dos = new DataOutputStream(socket.getOutputStream());
                 // data = dos.readUTF();
                 dos.writeUTF(data);
                 dos.flush();
                 //dos.close();
             }

         }
         catch(Exception e){
             System.out.println("Run "+e);
         }


   }
   public void broadcast(String data)
   {
       try{
           Iterator it = al.iterator();
           while(it.hasNext())
           {
              Socket socket1 = (Socket)it.next();
               dos = new DataOutputStream(socket1.getOutputStream());
               dos.writeUTF(data);
               dos.flush();


           }
       }
       catch(Exception e){
               System.out.println("Broadcast running "+ e);
       }
   }
}
public class Multiclient {

Socket socket;
DataInputStream dis;
DataOutputStream dos;
Multiclient() throws IOException
{
    socket = new Socket("127.0.0.1", 1036);
    System.out.println(socket);
    Mythreadc my = new Mythreadc(socket);
    my.start();
}
public class Mythreadc extends Thread{

DataInputStream dis;
DataOutputStream dos;
Socket socket;
Mythreadc(Socket socket)throws IOException
{
    this.socket = socket;}

public void run()
{
    BufferedReader br = null; 
    try{
       br = new BufferedReader(new InputStreamReader (System.in));
        dos = new DataOutputStream(socket.getOutputStream());
      String data = "";
      do{

          data = br.readLine();
          dos.writeUTF(data);
          System.out.println(data);
          dos.flush();
      }
      while(!data.equals("stop"));
    }
    catch(Exception e)
    {
        System.out.println("Client input "+e);
    }
    finally{
        try{
            br.close();
            dis.close();
            dos.close();
        }
        catch(Exception e)
        {
            System.out.println("Closing "+e);
        }
    }
}    
}
public class Multiclient {

Socket socket;
DataInputStream dis;
DataOutputStream dos;
Multiclient() throws IOException
{
    socket = new Socket("127.0.0.1", 1036);
    System.out.println(socket);
    Mythreadc my = new Mythreadc(socket);
    my.start();
    /**
     * Here write out the code for taking input from Standard Console
     */
    BufferedReader br = null; 

   try{
   br = new BufferedReader(new InputStreamReader (System.in));
    dos = new DataOutputStream(socket.getOutputStream());
  String data = "";
  do{

      data = br.readLine();
      dos.writeUTF(data);
      System.out.println(data);
      dos.flush();
  }
  while(!data.equals("stop"));
}
catch(Exception e)
{
    System.out.println("Client input "+e);
}
}
客户端线程

        try{

         String data ="";
         dis = new DataInputStream(socket.getInputStream());
        while(data.equalsIgnorCase("stop")){
         data = dis.readUTF();
         System.out.println("Server Message : "+data);
    }
     }
     catch(Exception e){
         System.out.println("Run "+e);
     }
客户端线程不完整,但我认为这些信息足够了


希望它能帮到你,你的问题确实让我想起了大学时代:)

你的代码有两个问题阻止客户端显示多条消息

问题一:您的客户端代码从未实际显示或打印它从服务器收到的消息。线路

dos = new DataOutputStream(socket.getOutputStream());
创建可用于将数据写入套接字的输出流,即向服务器发送消息。但您从不使用套接字的InputStream,这是从套接字读取数据(即从服务器接收消息)所需的操作。当您看到客户机上打印的消息时,实际上您只是看到

System.out.println(data);
让你的客户打印刚刚发送的信息

为了让客户端接受来自用户的输入并同时从服务器读取消息,您可能应该在客户端上使用两个线程。一个线程可以是您已经编写的客户端线程,因为它负责接受用户的输入。另一个线程应该如下所示:

public class ClientReaderThread extends Thread {

Socket socket;

ClientReaderThread(Socket socket) {
    this.socket = socket;
}

public void run() {
    try (BufferedReader serverReader = new BufferedReader(
            new InputStreamReader(socket.getInputStream()))){

        String fromServer = serverReader.readLine();;
        while(fromServer != null) {
             if (fromServer.equals("stop")) 
                break;
            System.out.println(fromServer);
            fromServer = serverReader.readLine();
        }
    } catch (IOException e) {
        System.out.println("Client error! Got exception: " + e);
    }
}

}
(请注意,我使用该语句来构造读取器,它负责在客户端停止时关闭它)

然后在主客户端类中,使用相同的套接字启动两个线程:

Multiclient() throws IOException
{
    socket = new Socket("127.0.0.1", 1036);
    System.out.println(socket);
    Mythreadc my = new Mythreadc(socket);
    ClientReaderThread reader = new ClientReaderThread(socket);
    my.start();
    reader.start();
}
问题二:您的服务器只从每个客户端读取并回显一行,因为处理每个客户端的套接字线程(
Mythread
)不包含循环。在为每个客户端创建单个线程的设置中,
run()
只为每个客户端调用一次,因此
run()
方法需要处理客户端发送的每个消息

以下是服务器线程中的
run()
方法的外观:

public void run() {
    try (BufferedReader inStream = new BufferedReader(
        new InputStreamReader(socket.getInputStream()))){

        String data = inStream.readLine();
        while(data != null) {
            if(data.equals("stop"))
                break;
            broadcast(data);
            data = inStream.readLine();
        }
    }
    catch(Exception e){
        System.out.println("Run exception "+e);
    } finally {
        al.remove(socket); //This is important to do
    }

}
我在这里做了另外一个重要的更改:在
run()
方法的末尾,当客户端断开连接或发生异常时,线程将其套接字从ArrayList中删除。这确保了所有引用相同ArrayList的其他服务器线程,不要尝试广播到已断开连接的客户端的套接字。如果忽略此操作,则在另一个客户端断开连接后,当客户端向服务器发送消息时,将出现异常

杂项注释

public class Mythread extends Thread{

Socket socket;
ArrayList al;
DataInputStream dis;
DataOutputStream dos;

Mythread(Socket socket, ArrayList al) 
{
     this.socket = socket;
     this.al = al;}

     public void run()
     {
         try{

             String data ="";
             dis = new DataInputStream(socket.getInputStream());
             data = dis.readUTF();
             if(!data.equals("stop"))
             {
                 broadcast(data);
             }
             else
             {
                 dos = new DataOutputStream(socket.getOutputStream());
                 // data = dos.readUTF();
                 dos.writeUTF(data);
                 dos.flush();
                 //dos.close();
             }

         }
         catch(Exception e){
             System.out.println("Run "+e);
         }


   }
   public void broadcast(String data)
   {
       try{
           Iterator it = al.iterator();
           while(it.hasNext())
           {
              Socket socket1 = (Socket)it.next();
               dos = new DataOutputStream(socket1.getOutputStream());
               dos.writeUTF(data);
               dos.flush();


           }
       }
       catch(Exception e){
               System.out.println("Broadcast running "+ e);
       }
   }
}
public class Multiclient {

Socket socket;
DataInputStream dis;
DataOutputStream dos;
Multiclient() throws IOException
{
    socket = new Socket("127.0.0.1", 1036);
    System.out.println(socket);
    Mythreadc my = new Mythreadc(socket);
    my.start();
}
public class Mythreadc extends Thread{

DataInputStream dis;
DataOutputStream dos;
Socket socket;
Mythreadc(Socket socket)throws IOException
{
    this.socket = socket;}

public void run()
{
    BufferedReader br = null; 
    try{
       br = new BufferedReader(new InputStreamReader (System.in));
        dos = new DataOutputStream(socket.getOutputStream());
      String data = "";
      do{

          data = br.readLine();
          dos.writeUTF(data);
          System.out.println(data);
          dos.flush();
      }
      while(!data.equals("stop"));
    }
    catch(Exception e)
    {
        System.out.println("Client input "+e);
    }
    finally{
        try{
            br.close();
            dis.close();
            dos.close();
        }
        catch(Exception e)
        {
            System.out.println("Closing "+e);
        }
    }
}    
}
public class Multiclient {

Socket socket;
DataInputStream dis;
DataOutputStream dos;
Multiclient() throws IOException
{
    socket = new Socket("127.0.0.1", 1036);
    System.out.println(socket);
    Mythreadc my = new Mythreadc(socket);
    my.start();
    /**
     * Here write out the code for taking input from Standard Console
     */
    BufferedReader br = null; 

   try{
   br = new BufferedReader(new InputStreamReader (System.in));
    dos = new DataOutputStream(socket.getOutputStream());
  String data = "";
  do{

      data = br.readLine();
      dos.writeUTF(data);
      System.out.println(data);
      dos.flush();
  }
  while(!data.equals("stop"));
}
catch(Exception e)
{
    System.out.println("Client input "+e);
}
}
  • 正如我在评论中提到的,您应该在thread类中为
    al
    提供一种类型的
    ArrayList
    ,并使用for-each循环而不是迭代器在
    broadcast()中对其进行迭代
    
  • 我正在使用
    BufferedReader
    而不是
    DataInputStream
    从套接字读取数据。这是因为
    DataInputStream.readUTF()
    writeUTF()
    已被弃用,并已被
    BufferedReader.readLine()
    PrintWriter.println()取代
    
  • dis
    dos
    这样的流不需要是线程类中的实例变量,因为它们只在
    run()
    方法中使用。它们可以是
    run()
    中的局部变量,就像我在新的
    run()
    方法中对
    inStream
    所做的那样

对不起,我在服务器类中错误地输入了错误的代码。现在编辑!过来看!查看编辑后的答案,希望能有所帮助为什么您仍然在为套接字的ArrayList使用非泛型迭代器?通过编写代码,您可以简化代码并获得类型安全性<