简体   繁体   English

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

[英]Simple Server/Client Socket connection?

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)

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 :) 希望我能帮助到其他人,请在评论中回复。

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 ) 只是意味着“端口”已准备就绪(几乎来自您启动的同一类)

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

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. 这将项目添加回了Tomcat 7,但没有收到BindException错误。 This was due to closing eclipse the last time you used without stopping the Tomcat server. 这是由于上次关闭Eclipse而没有停止Tomcat服务器所致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM