【问题标题】:Can't connect Java sockets无法连接 Java 套接字
【发布时间】:2021-10-28 04:09:35
【问题描述】:

我正在尝试连接两个简单的 Java 套接字,但无论我输入什么端口号,我都会收到相同的错误:地址已在使用中:JVM_Bind

现在我发现我通过使用 0 作为 ServerSocket 构造函数的参数,然后调用 getLocalPort 方法来获取第一个可用端口,然后将其作为参数传递给 Socket 构造函数中的客户端类,从而解决了这个问题。

所以,在 NetBeans IDE 中,我首先运行服务器,从控制台获取可用端口,复制数字并手动将其作为“localhost”之后的第二个参数输入到 Socket 构造函数并运行客户端。

现在预期的输出将是“已连接”,因为服务器已接受客户端,但相反,我得到的可用端口号增加了 1。

为什么会这样?似乎当我在 client.java 文件中单击运行时,我再次启动服务器而不是客户端。

服务器.java

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class server {

    public static void main(String[] args) throws IOException {

        ServerSocket s1 = new ServerSocket(58801);/I manually add the available port number here 

        System.out.println(s1.getLocalPort());

        Socket ss = s1.accept();

        System.out.println("Client connected");

    }

}

client.java:

import java.io.IOException;
import java.net.Socket;


public class client {

    public static void main(String[] args) throws IOException {

        Socket s = new Socket("localhost", 58801); // I here manually add the available port number 

    }

}

【问题讨论】:

  • 你使用0,作为ServerSocket(0);中的一个端口,这个端口是保留的,你不能用。
  • @GiorgiTsiklauri 哦,我只是错误地将其保留为 0,我也确实在那里传递了可用端口号,但我总是得到同样的错误:地址已在使用中:JVM_Bind
  • 这很好用。我无法重现该问题。最有可能的是,您使用该端口.. 通过netstat 检查。
  • 您的代码在我的机器上运行良好,只要我在客户端之前运行服务器。
  • @GonenI 你是对的。通过按下 NetBeans 中的运行按钮,我实际上是在再次运行同一个文件。我只是右键单击客户端,选择运行,我得到了想要的结果。谢谢。

标签: java sockets serversocket


【解决方案1】:

在 OP 已经弄清楚之后,一个有点迟的答案:

一个可能的原因是在 IDE 中错误地运行了服务器两次。第一次运行抓取端口,第二次运行服务器会发现该端口已被使用并产生错误。

【讨论】:

    【解决方案2】:

    Server 客户端套接字示例:

    服务器类:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class GreetServer {
        private ServerSocket serverSocket;
        private Socket clientSocket;
        private PrintWriter out;
        private BufferedReader in;
    
        public void start(int port) throws IOException {
            serverSocket = new ServerSocket(port);
            clientSocket = serverSocket.accept();
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            String greeting = in.readLine();
            if ("hello server".equals(greeting)) {
                out.println("hello client");
            } else {
                out.println("unrecognised greeting");
            }
        }
    
        public void stop() throws IOException {
            in.close();
            out.close();
            clientSocket.close();
            serverSocket.close();
        }
    
        public static void main(String[] args) throws IOException {
            GreetServer server = new GreetServer();
            server.start(6666);
        }
    }
    

    客户端类:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    public class GreetClient {
        private Socket clientSocket;
        private PrintWriter out;
        private BufferedReader in;
    
        public void startConnection(String ip, int port) throws IOException {
            clientSocket = new Socket(ip, port);
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        }
    
        public String sendMessage(String msg) throws IOException {
            out.println(msg);
            String resp = in.readLine();
            return resp;
        }
    
        public void stopConnection() throws IOException {
            in.close();
            out.close();
            clientSocket.close();
        }
    
        public static void main(String[] args) throws IOException {
            GreetClient client = new GreetClient();
            client.startConnection("127.0.0.1", 6666);
    
            String response = client.sendMessage("hello server");
            System.out.println("Response from server: " + response);
        }
    }
    

    示例响应:

    Response from server: hello client
    

    【讨论】:

    • 这里没有回答 OP 的问题“为什么会发生这种情况?”
    猜你喜欢
    • 2014-03-01
    • 2018-03-12
    • 2016-03-10
    • 1970-01-01
    • 2013-02-02
    • 2011-10-03
    • 2018-01-03
    • 2021-05-10
    相关资源
    最近更新 更多