【问题标题】:Simple Server/Client Socket connection?简单的服务器/客户端套接字连接?
【发布时间】:2012-10-10 13:27:24
【问题描述】:

我正在编写一个 java 套接字,服务器在其中运行,客户端连接到服务器,服务器可以从客户端命令行读取。

首先我想连接到服务器,代码如下:

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

        ServerSocket serverSocket = null;

        try {
            serverSocket = new ServerSocket(10000);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            e.printStackTrace();
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine, outputLine;

        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }

}

这是我的客户:

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

        Socket socket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            socket = new Socket("taranis", 4444);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;
        String fromUser;

        while ((fromServer = in.readLine()) != null) {
            System.out.println("Server: " + fromServer);
            if (fromServer.equals("Bye."))
                break;

            fromUser = stdIn.readLine();
            if (fromUser != null) {
                System.out.println("Client: " + fromUser);
                out.println(fromUser);
            }
        }

        out.close();
        in.close();
        stdIn.close();
        socket.close();
    }
}

当我启动服务器时,我得到“无法侦听端口:4444。”

为什么?

更新:

现在我无法再次重新启动服务器,因为我得到了

java.net.BindException: Address already in use: JVM_Bind
    at java.net.DualStackPlainSocketImpl.bind0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
    at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
    at java.net.PlainSocketImpl.bind(Unknown Source)
    at java.net.ServerSocket.bind(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)

我能做些什么呢?(我正在使用 windows7)

【问题讨论】:

标签: java eclipse sockets tcp client


【解决方案1】:

在我看来,您的服务器并未监听端口 4444,而是监听端口 10000,但是您的客户端尝试连接到端口 4444。

如果您关闭应用程序,要回答您的第二个问题,请确保您确实终止了它。有时,如果您再次启动应用程序,您有 2 个正在运行,并且很难看到其中 1 个应用程序仍处于打开状态。

希望我对您有所帮助,请在评论中回复,稍后再回来查看您:)

【讨论】:

    【解决方案2】:

    java.net.BindException:地址已在使用中:JVM_Bind

    通常在您上次执行未正确终止时出现 就像在eclipse中一样,如果你运行然后再次运行而不终止,你需要在控制台中“终止”进程。

    仅表示“端口”已被使用(几乎来自您启动的同一类)

    【讨论】:

      【解决方案3】:

      如果您使用的是 Eclipse,只需按控制台磁贴上的红色方块即可停止它,然后您可以重新连接

      【讨论】:

        【解决方案4】:

        异常java.net.BindException: Address already in use: JVM_Bind

        解决方案是从服务器选项卡中删除项目,然后右键单击并在服务器上运行项目。这将项目添加回 Tomcat 7 并且我没有收到 BindException 错误。这是由于您上次使用时关闭了 eclipse 而没有停止 Tomcat 服务器。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-05-24
          • 1970-01-01
          • 2017-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-21
          相关资源
          最近更新 更多