Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.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_Multithreading_Sockets_Network Programming_Serversocket - Fatal编程技术网

使用java的简单多线程服务器聊天

使用java的简单多线程服务器聊天,java,multithreading,sockets,network-programming,serversocket,Java,Multithreading,Sockets,Network Programming,Serversocket,我正在创建一个java聊天服务器,它可以处理多个客户端。我使用这个简单的服务器代码 public class Server extends Thread { ServerSocket serverSocket = null; Socket socket = null; private int unique_id; ArrayList<Clients> cl; public Server(int port) { try {

我正在创建一个java聊天服务器,它可以处理多个客户端。我使用这个简单的服务器代码

public class Server extends Thread {
    ServerSocket serverSocket = null;
    Socket socket = null;
    private int unique_id;
    ArrayList<Clients> cl;


    public Server(int port) {
        try { 
            serverSocket = new ServerSocket(port);
            cl = new ArrayList<>();
            this.start();
        } catch (Exception e){
            System.out.println("Error 5");
            e.printStackTrace();
        }
    }
    @Override
    public void run(){
        System.out.println("Server Start");
            while (true){
                try {
                    socket = serverSocket.accept();
                    Clients t = new Clients(socket); // add it to thread
                    cl.add(t);
                    t.start();
                    System.out.println("Connected " + String.valueOf(cl.size())); // printed ok 
                }catch (Exception e){
                    System.out.println("Error 4");
                    e.printStackTrace();
                }
            }
    }
    public synchronized void SendToAll(String s){ // this function used by client when one of client socket send a message then server send it to all
        System.out.println("Sended is excuted"); // excuted normal each time i send a message from client but not send to all  
        for (int i = 0; i < cl.size(); i++){
            cl.get(i).WriteToSocket(s);
        }
    }

    public static void main(String args[]){
        int port = 5002;
        Server server = new Server(port); // start server
        //server.run(); // start connections wait for it
    }

    class Clients extends Thread { // class extends thread
        public Socket socket = null;
        DataInputStream input = null; // read input
        DataOutputStream output = null; // read output
        public int myid = 0; // unique id for each client

        public Clients(Socket soc) {
            socket = soc;
            try {
                input = new DataInputStream(socket.getInputStream());
                output = new DataOutputStream(socket.getOutputStream());
                myid = ++unique_id;
                System.out.println("Client Start Thread"); // printed ok !
            } catch (IOException e){
                System.out.println("Error 1");
                e.printStackTrace();
            }
        }
        public void WriteToSocket(String s) { // used to write a message to this socket
            try {
                output.write(s.getBytes());
            }catch (IOException e){
                System.out.println("Error 2");
                e.printStackTrace();
            }
        }
        @Override
        public void run() { // run thread function wait for messages from clients 
            while (true){
                try {
                    String s = input.readLine();
                    if (s.contains("quite")) {
                        socket.close();
                        input.close();
                        output.close();
                        cl.remove(this);
                        this.stop();
                    }
                    if (!s.isEmpty()) {
                        SendToAll(s);// when message come and not empty it use server function to send them to all clients 
                    }
                }catch (IOException e){
                    System.out.println("Error 3");
                    e.printStackTrace();
                }
            }
        } 
    }
}
编辑
当我在main中使用此代码时,SendToAll函数将消息发送到所有客户端!!为什么当我从客户端类使用线程时,它没有发送给所有人

public static void main(String args[]){
    int port = 5002;
    Server server = new Server(port); // start server
    //server.run(); // start connections wait for it
    while (true) {
        String s = in.next();
        server.SendToAll(s + "\n"); // message sended to all client !!
    }
}

我解决了问题,很抱歉,这是我的错,我忘了在sendToAll函数中添加\n,这就是问题的原因,因此没有\n客户端无法读取该行,因为我在DataInputStream中使用了readLine

无论如何,我尝试了另一种方法来读取字节,而不是读取行。我认为这样更好,尤其是当您接收到UTF-8字符,并且在这之后从字节变为字符串时。问题是,读取行读取直到找到文件结尾的行终止符。这就是为什么它与QT C++中的其他服务器一起工作,而不是与java服务器一起工作的原因。 请看这里:

请注意,DataInputStream中的readLine已被弃用。您应该使用来读取中指示的行(带有readLine)


因此,将“\n”添加到发送的字符串的末尾,它将起作用。

您遇到问题,并且正在捕获并忽略异常?尝试的第一件事-
e.printStackTrace()。我现在在每个cath中添加。printStackTrace();我没有遇到任何问题,但是当我关闭客户端并且服务器正在运行时,我在服务器$Clients.run(server.java:91)“当您声明变量但未创建对象时,会发生异常”那么我在哪一行做错了什么?为什么在我关闭客户端应用程序时出错?您能显示客户端代码吗?顺便说一句,在没有正确同步的情况下,从两个不同的线程访问cl时存在竞争条件。
public static void main(String args[]){
    int port = 5002;
    Server server = new Server(port); // start server
    //server.run(); // start connections wait for it
    while (true) {
        String s = in.next();
        server.SendToAll(s + "\n"); // message sended to all client !!
    }
}