Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
线程“中的绑定服务器套接字异常”;“主要”;java.net.BindException:地址已在使用:JVM\u Bind java_Java_Sockets_Network Programming - Fatal编程技术网

线程“中的绑定服务器套接字异常”;“主要”;java.net.BindException:地址已在使用:JVM\u Bind java

线程“中的绑定服务器套接字异常”;“主要”;java.net.BindException:地址已在使用:JVM\u Bind java,java,sockets,network-programming,Java,Sockets,Network Programming,我正在尝试创建一个Java程序,以使用套接字编程从客户端向服务器发送消息并接收响应,但出现以下错误: 线程“main”java.net.BindException中的异常:地址已在使用中:JVM\u Bind 位于java.net.DualStackPlainSocketImpl.bind0(本机方法) 位于java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106) 位于java.net.Abst

我正在尝试创建一个Java程序,以使用套接字编程从客户端向服务器发送消息并接收响应,但出现以下错误:

线程“main”java.net.BindException中的异常:地址已在使用中:JVM\u Bind 位于java.net.DualStackPlainSocketImpl.bind0(本机方法) 位于java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106) 位于java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387) 位于java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190) 位于java.net.ServerSocket.bind(ServerSocket.java:375) 位于java.net.ServerSocket.(ServerSocket.java:237) 位于java.net.ServerSocket。(ServerSocket.java:128) 在Bob.main(Bob.java:9)

客户端:

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

    InetAddress addr = InetAddress.getByName("127.0.0.1") ; //set the address of the server to be connected to. Any IP address can be set here as a string
    //If the name is saved in your "etc/hosts" file, then the name can be used instead of the ip address
    //Alternative method: InetAddress.getByAddress -see "stackoverflow" explanation if interested

    System.out.println("addr = " + addr);

    Socket socket = new Socket(addr, 8080); //attempt to connect to the server by creating a socket using the IP address and Port number of the server

    try {
        System.out.println("socket = " + socket);
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //create an input variable for the socket- accepts input from the socket

        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); // create an output variable for the socket- writes output to the socket

        //this loop simply writes to the socket 10 times waits for a reply from the socket and then prints the reply received
        for(int i = 0; i < 10; i ++) {
            out.println("I am it: " + i); //write to socket
            String str = in.readLine(); //read from socket
            System.out.println(str);    //print to console line read from socket
        }

        out.println("END"); //send string that indicates end of transmission

    }catch( SocketException e ) {
        System.out.println("Server closed connection");
    }finally{
        System.out.println("closing...");
        socket.close(); //close the connection
    }
}

java.net.BindException
表示您正试图绑定到已占用的端口


您可能有两个服务器实例正在运行,或者其他一些程序已经在使用端口8080。

java.net.BindException
表示您正在尝试绑定到一个已经占用的端口


您可能有两个服务器实例正在运行,或者其他一些程序已经在使用端口8080。

您是在同一台机器上运行它吗?客户端和服务器部分使用相同的端口。是的,我正在同一台计算机上运行它。这与连接客户端无关。运行服务器时会发生这种情况
SocketException
并不意味着“客户端关闭连接”,而且您没有检查流结束,这是真的。您是在同一台计算机上运行它吗?客户端和服务器部分使用相同的端口。是的,我正在同一台计算机上运行它。这与连接客户端无关。运行服务器时会发生这种情况
SocketException
并不意味着“客户端已关闭连接”,而且您没有检查流的结尾,这是真的。将端口更改为5050,但仍然给我相同的错误您正在运行两个实例吗?在启动新服务器之前先关闭旧服务器。我只有一台服务器正在运行。另外,请查看链接问题:修复了,谢谢:)将端口更改为5050,但仍然给我相同的错误。您正在运行两个实例吗?在启动新服务器之前先关闭旧服务器。我只有一台服务器正在运行。另外,请查看链接问题:修复了。谢谢:)
    public static final int PORT = 8080;
    public static void main(String[] args) throws IOException {

    ServerSocket s = new ServerSocket(PORT); //create server socket object
    System.out.println("Started: " + s);

    try {
        Socket socket = s.accept(); //accept a socket connection using the Server Socket created previously
        try {
            System.out.println("Connection accepted: "+ socket);

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //create an input variable for the socket- accepts input from the socket

            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);   // create an output variable for the socket- writes output to the socket


            //this loop accepts input from the socket, prints out what it receives anc echoes it back into the socket until it recives the word "END" indicating the end of transmission
            while (true) {
                String str = in.readLine(); //read line from socket
                if (str.equals("END")) break;
                System.out.println("Echoing: " + str);  //print line to console
                out.println("You said: " + str); //echo line read back into socket
            }


        }catch ( SocketException  e ) {
            System.out.println("Client closed connection");
        }finally{               //close the present connection
            System.out.println("closing...");
            socket.close();
        }

    }catch( SocketException e ){
        System.out.println("Server socket closed unexpectedly");
    }finally{               //close the server socket. i.e. stop listening for connections
        s.close();          //without this line, the server would continue waiting for another connection
    }
}