eclipse 简单的服务器/客户端套接字连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12820503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:10:16  来源:igfitidea点击:

Simple Server/Client Socket connection?

javaeclipsesocketstcpclient

提问by maximus

I am writing a java socket where a server runs and a client connects to the server and the server can read from the clients command line.

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

First I wanted to connect to the server, here`s the code:

首先我想连接到服务器,这是代码:

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

}

and here is my client:

这是我的客户:

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

When I start the server I get "Could not listen on port: 4444."

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

Why?

为什么?

UPDATE:

更新:

Right now I cannot restart the server again, because I get

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

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)

What can I do against that?(I am using windows7)

我该怎么做?(我使用的是 windows7)

回答by Marc Rasmussen

it seems to me that your server is not listening on port 4444 but instead listening on port 10000 however your client tries to connect to port 4444.

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

To answer your 2nd problem if you close down the application make sure you actually terminate the it. sometimes if you start the application again you have 2 running and can have a hard time see that 1 of the applications are still open.

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

Hope i helped else please reply in a comment il check back on you late :)

希望我能帮到其他人,请在评论中回复我晚点回来看看你:)

回答by Andrea Bori

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

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

usually came out when you haven't terminate properly last execution like in eclipse if you run then run again without terminate you need to "terminate" the process in the console.

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

Simply mean that the "port" is allready used ( allmost from the same class you launched )

只是意味着“端口”已被使用(几乎来自您启动的同一类)

回答by Mauro de Palma

If you are using Eclipse just press the red square on the console tile to stop it and than you can reconnect

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

回答by Lucky

Exception: java.net.BindException: Address already in use: JVM_Bind

例外java.net.BindException: Address already in use: JVM_Bind

The solution was to remove the project from the servers tab and right click and run the project as Run on server. This added the project back to the Tomcat 7 and I din't get the BindException error. This was due to closing eclipse the last time you used without stopping the Tomcat server.

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