gpt4 book ai didi

java - 修复 java.net.BindException : Address already in use: JVM_Bind

转载 作者:行者123 更新时间:2023-12-01 11:14:45 24 4
gpt4 key购买 nike

我正在创建一个程序,该程序将有 1 个服务器和多个客户端。所以我想做的是接受任何传入的客户端连接到同一端口,但是当我这样做时,我得到异常:java.net.BindException:地址已在使用中:JVM_Bind。 p>

我还试图跟踪每个单独的客户端,以便我可以向单个客户端发送消息,因此我想在连接后将套接字添加到 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);
}
}
}

}

最佳答案

似乎您想听一次并接受多次 - 也许是这样的:

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);
}
}
}

}

关于java - 修复 java.net.BindException : Address already in use: JVM_Bind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31972211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com