gpt4 book ai didi

java - 客户端/服务器 java 应用程序中的 JVM_Bind 错误

转载 作者:行者123 更新时间:2023-12-01 15:04:10 30 4
gpt4 key购买 nike

我正在尝试构建一个java微博应用程序我已经完成了代码,但由于以下错误而无法连接到自己的计算机(我google了一下,有人说我需要更改端口号。我更改了端口号,但没有任何反应)

Exception in thread "Thread-4" java.net.BindException: Address already in use: JVM_Bind

下面是服务器的代码:

public class server extends Thread {
public Socket client = null;
public ServerSocket server = null;
private int port = 4444;

public void run(){
while (true){ //loop waits for incomming connection
try { //start the server and listen to a port
server = new ServerSocket(port);
}catch (IOException e){
System.err.println(e);
System.exit(-1);
}

try { //establish a connection when receiving request
client = server.accept();
}catch (IOException e){
System.err.println(e);
System.exit(1);
}

Thread t = new Thread(new Connection(client));
t.start();
}
}
}

这是启动服务器并监听端口 4444 的代码

Server server = new Server();
server.start(); //to listen to a port

谢谢

最佳答案

您必须在进入循环之前创建 ServerSocket。目前,您尝试在每次迭代中创建它,这是没有意义的,并且您没有关闭它,因此第二次创建失败。

关于java - 客户端/服务器 java 应用程序中的 JVM_Bind 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13205017/

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