gpt4 book ai didi

java - BindException 多线程服务器

转载 作者:行者123 更新时间:2023-12-01 13:22:09 25 4
gpt4 key购买 nike

我有 3 个服务器,我在这些服务器中运行以下代码。然而其中之一给出了错误:

Exception in thread "main" java.net.BindException: Address already in use
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at MultiThreadServer.main(MultiThreadServer.java:22)

即使我使用命令“ps -ef | grep java | grep -v grep | awk '{print $2}' | xargs Kill -9”停止运行程序,它仍然给出相同的异常。代码如下:

public class MultiThreadServer implements Runnable {
Socket csocket;
MultiThreadServer(Socket csocket) {
this.csocket = csocket;
}

public static void main(String args[])
throws Exception {
ServerSocket ssock = new ServerSocket(8879);
System.out.println("Listening");
while (true) {
Socket sock = ssock.accept();
System.out.println("Connected");
new Thread(new MultiThreadServer(sock)).start();
}

}
public void run() {..}

最佳答案

该端口可能已被使用。尝试使用不同的端口。

关于java - BindException 多线程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21962529/

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