【问题标题】:Server client chat room in JavaJava中的服务器客户端聊天室
【发布时间】:2013-12-27 21:16:45
【问题描述】:

我正在制作一个聊天室项目,其中服务器接受许多客户端,客户端写入的任何内容都会到达其他客户端,依此类推。 不幸的是,服务器最多接受 2 个客户端,并且在一个客户端写入输入后,它会出错。

public class Server2 {

    private static ArrayList<Socket> clients;
    private ServerSocket server;
    DataOutputStream os;
    DataInputStream in;
    Socket s;

    public Server2() throws IOException {
        server = new ServerSocket(5000);
        clients = new ArrayList<Socket>();
        System.out.println("Waiting for connections...");
        runOutput();
    }

    public void addClient() throws IOException {
        s = server.accept();
        clients.add(s);
        System.out.println("A new Client has joined");
    }

    public void runOutput() {

        Thread n = new Thread(new Runnable() {
            public void run() {
                try {
                    addClient();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        n.start(); 

        Thread input = new Thread(new Runnable() {
            public void run() {
                try {
                    addClient();
                    in = new DataInputStream(s.getInputStream());
                    os = new DataOutputStream(s.getOutputStream());
                    String st = in.readLine();
                    System.out.println(st);
                    os.writeBytes(st);
                    for(int i = 0; i < clients.size(); i++)
                    {
                        DataOutputStream oo = new DataOutputStream(clients.get(i).getOutputStream());
                        oo.writeBytes(st);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        input.start();
    }

    public static void main(String[] args) throws IOException {
        Server2 server = new Server2();
    }
}

和客户端类:

public class Client2 {
    final public static String host = "localhost";
    final public static int port = 5000;
    Socket socket;
    DataInputStream in;
    DataOutputStream ou;
    Scanner chat;
    boolean run;
    String name;

    public Client2(String n) throws IOException {
        name = n ;
        socket = new Socket(host , port);
        System.out.println("Connection Successful");
        run = true;
        runOutput();
    }

    public void runOutput() {
        Thread input = new Thread(new Runnable() {
            public void run() {
                while (run) {
                    try {
                        in = new DataInputStream(socket.getInputStream());
                        String s = in.readLine();
                        System.out.println(s);
                        if(chat.nextLine().compareTo("QUIT") == 0)
                            run = false;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        input.start();

        Thread t = new Thread(new Runnable() {
            public void run() {
                while (run) {
                    try {
                        ou = new DataOutputStream(socket.getOutputStream());
                        chat = new Scanner(System.in);
                        ou.writeBytes(name + " says :" + chat.nextLine() + "\n");

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        t.start(); 
    } 

    public static void main(String[] args) throws IOException {
        Client2 client = new Client2("Ahmed");
    }   
}

【问题讨论】:

  • 请发布您遇到的错误。另外,据我所知,您调用runOutput() 一次,并且没有循环,这意味着s = ss.accept() 只会被调用一次
  • 看看我编辑的答案。希望对你有所帮助

标签: java client-server chat


【解决方案1】:

你的 'n' 线程没有循环,这意味着它会运行一次(接受连接),然后死掉。

Thread n = new Thread(new Runnable() {
    public void run() {
        while(true) { //control this with your own boolean, or it will run forever
            try {
                addClient();
            }catch(IOException e) { }
        }
    }
});

别担心,你的线程会在 'ss.accept()' 处暂停,因为它会等到一个套接字被接受后再继续。

Thread thread = new Thread(new Runnable() {
    public void run() {
        while(running) {
             String input;

             while((input = inputstream.readUTF()) != null) {
                  //handle input
             }
         }
    }
});

对不起,写得真快,没有太多时间。如果没有帮助,我会稍后再将其与您的代码相关联。

您需要某种方式不断地从服务器的输入流中检索输入,然后您可以在其中处理输入。

每次您从 inputstream.readUTF() 检索某些内容时,该循环都会循环(当然不是 null)。希望该示例对您有所帮助


在实际阅读您的代码并对其进行测试后,我注意到您的结构似乎有点不对劲。首先,您的服务器中的Thread n 负责接受连接(addClient()),但您在Thread input 中也将其称为第一件事?

Thread n 正在处理接受连接,所以有了循环,Thread n 就可以了。 Thread input 正在处理从客户端检索到的输入。这就是您感到困惑的地方。

每个客户端都应该有自己的服务器端 InputStream 和 OutputStream。与您拥有 Sockets 列表的方式类似(因为您将在服务器端创建多个 Sockets),您也需要多个 Streams。为此,我建议创建一个 User 类,如果有的话,将 ArrayList 用于 User,而不是 Socket。

public class User extends Thread { //create a Thread for each connection to handle data seperately
    DataInputStream in;
    DataInputStream out;

    Socket socket;
    public User(Socket socket) { //creates a stream for the socket that the user connected with
        this.socket = socket;

        initStream(); 
    }

    public void initStream() { //inits streams after socket is initiated (through ss.accept())
        try {
            out = new DataOutputStream(socket.getOutputStream());
            in = new DataInputStream(socket.getInputStream());
        }catch(IOException e) { e.printStackTrace(); }
    }

    public void closeStream() throws IOException { //throws exception since i have a chance to catch it in my run() method
            out.close(); in.close();
            socket.close();
    }

    public void sendMessage(String string) {
        try {
            out.writeUTF(string);
        }catch(IOException e) { e.printStackTrace(); }
    }

    public void run() { //when thread starts, accept/handle data
        String input;

        try {
            while((input = in.readUTF()) != null) {
                //handle input
            }
            closeStream();
        }catch(IOException e) { e.printStackTrace(); }
    }
}

in.readLine() 已折旧,这意味着它已过时且不应使用(以避免错误)。 现在您已经设置了处理数据的 User 类,您现在可以在每次收到连接时创建一个新用户。

public class Server {
    private static boolean running = true;

    public static ArrayList<User> userlist;

    public static synchronized void stop() { //synchronized in-case another thread other than the main thread stops the server
        if(!running) return;
        running = false;
    }

    public static void main(String[] args) {
        /* uses the JVM thread (main thread for applications), so if you dont wanna
        ** dedicate your entire server to accepting connections only (since Users are in new thread)
        ** create a new thread for the code in this void method. */

        try {
            ServerSocket ss = new ServerSocket(8000);

            userlist = new ArrayList<User>();
            User user; //holds the user so we can put it in the list, then start
        while(running) {
            user = new User(ss.accept());

            list.add(user);
            user.start();
        }
        } catch(IOException e) {
            e.printStackTrace(); }
    }
}

希望这能让您更好地了解服务器的运行方式。

至于您的客户……嗯,您应该意识到很多事情:

首先,大问题,您要在每个循环中重新初始化 DataOutputStream,这将导致流服务器端关闭。必须把它从循环中拿出来,可能把它放在前面(或者看看我的服务器示例以获得更好的处理流初始化的想法)。

其次,名称应该保存在服务器上,而不是客户端上。这样,它很容易遍历名称,检查它是否被采用,等等..

第三,与网络无关,但看到你从不引用Thread nThread input,除了启动它,你应该删除引用变量。 (尽管我很确定垃圾收集器会拾取本地变量,但看起来仍然更好)。示例:

public void method() {
    new Thread(new Runnable() {
        public void run() {
            //code
        }
    }).start();

   new Thread(new Runnable() {
        //run method
   }).start();
}

希望这可以帮助您了解多线程服务器的工作原理! :)

PS:这是一个非常基本的服务器,真的不应该用于大规模应用程序。 创建新线程 (new User()) 会导致大量开销,因此最好使用 ExecutorService 或其他类型的线程池服务来处理连接。祝你好运!

【讨论】:

  • 这确实有助于接受许多客户端,但客户端写入的内容仍然无法到达服务器:\
  • 非常感谢兄弟,但仍然没有成功(虽然没有错误)
  • @VinceEmigh 使用您的服务器类时,我收到错误“无法将运行解析为变量”。你知道为什么会这样吗?
  • @Jacob 你需要声明一个running 变量。我有 edited my answer,虽然这是一个表达 horrid 设计的旧答案,所以我不建议效仿它。
【解决方案2】:

我认为你应该重新考虑很多服务器代码的逻辑。您应该有一个线程来处理连接客户端,并为它们分配自己的输入线程。然后,您可以在该输入读取线程上使用一个方法,该方法将遍历向它们发送消息的客户端。

当我第一次开始编写客户端-服务器聊天程序时,我发现 this 小 Oracle 文档非常有用。希望对你也有帮助。

您应该特别注意文档的这一部分:

客户端连接请求在端口排队,因此服务器必须按顺序接受连接。但是,服务器可以通过使用线程同时为它们提供服务——每个客户端连接一个线程。

这种服务器的基本逻辑流程是这样的:

while (true) { accept a connection; create a thread to deal with the client; }

【讨论】:

    猜你喜欢
    • 2016-04-09
    • 1970-01-01
    • 2017-07-06
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 2018-10-04
    相关资源
    最近更新 更多