简体   繁体   English

修复java.net.BindException:已使用的地址:JVM_Bind

[英]Fixing java.net.BindException: Address already in use: JVM_Bind

I am creating a program that will have 1 server and multiple clients. 我正在创建一个具有1个服务器和多个客户端的程序。 So what I am trying to do is accept any incoming client connection to the same port, but when I do so I get the exception: java.net.BindException: Address already in use: JVM_Bind . 所以我想做的是接受到同一端口的任何传入客户端连接,但是当我这样做时,出现异常: java.net.BindException: Address already in use: JVM_Bind

I am also trying to keep track of each individual client so I can send out messages to a single client, hence me wanting to add the socket to an ArrayList after being connected. 我还试图跟踪每个单独的客户端,以便可以向单个客户端发送消息,因此我想在连接后将套接字添加到ArrayList中。

private static ServerSocket socket;
private static ArrayList<Socket> arraySocket = new ArrayList<Socket>();


...


    public static void StartServer() {

                while(true){
                //for (int i = 0; i < Main.nucs.size(); i++) {
                    try {
                        socket = new ServerSocket(Constants.PORT_NUMBER);  
                        socket.setReuseAddress(true);
                        Logger.Log("Waiting for first client");
                        arraySocket.add(socket.accept());
                        Logger.Log("New Client: " + arraySocket.get(count).getInetAddress().toString());
                        (new Thread(new ClientHandler(arraySocket.get(count)))).start();
                        count++;
                    } catch (IOException e) {
                        Logger.Log("Server:IOException:e: " + e);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException ex) {
                            java.util.logging.Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }

    }

Seems like you want to listen once and accept many times -maybe something like this: 似乎您想听一次并接受很多次-也许像这样:

public static void StartServer() {

            socket = new ServerSocket(Constants.PORT_NUMBER);  
            socket.setReuseAddress(true);
            Logger.Log("Waiting for first client");

            while(true){
                try {
                    arraySocket.add(socket.accept());
                    Logger.Log("New Client: " + arraySocket.get(count).getInetAddress().toString());
                    (new Thread(new ClientHandler(arraySocket.get(count)))).start();
                    count++;
                } catch (IOException e) {
                    Logger.Log("Server:IOException:e: " + e);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        java.util.logging.Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }

}

暂无
暂无

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

相关问题 java.net.BindException:地址已在使用中:JVM_Bind - java.net.BindException: Address already in use: JVM_Bind java.net.BindException:已使用的地址:JVM_Bind多客户机服务器 - java.net.BindException: Address already in use: JVM_Bind Multi Client Server java.net.BindException:已在使用的地址:JVM_Bind <null>:80 - java.net.BindException: Address already in use: JVM_Bind <null>:80 线程“ Thread-0” java.net.BindException中的异常:已经使用的地址:JVM_Bind位于 - Exception in thread “Thread-0” java.net.BindException: Address already in use: JVM_Bind at 当我在Eclipse Mars中启动Jetty时,“ java.net.BindException:地址已在使用中:JVM_Bind” - “java.net.BindException: Address already in use: JVM_Bind” when I start Jetty in eclipse mars 如何捕获JVM_Bind异常:java.net.BindException:地址已在使用中 - How to catch JVM_Bind exception: java.net.BindException: Address already in use java.net.BindException:地址已在使用中:JVM_Bind但没有端口在使用它 - java.net.BindException: Address already in use: JVM_Bind BUT NO port using it ServerSocket端口问题(java.net.BindException:已在使用的地址:JVM_Bind) - ServerSocket port issue (java.net.BindException: Address already in use: JVM_Bind) 如何解决“java.net.BindException:地址已在使用:JVM_Bind”错误? - How do I resolve the "java.net.BindException: Address already in use: JVM_Bind" error? Tomcat:java.net.BindException:地址已在使用:JVM_Bind - Tomcat : java.net.BindException: Address already in use: JVM_Bind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM