简体   繁体   English

Java 客户端服务器套接字错误:java.net.BindException:地址已在使用:绑定

[英]Java Client Server Socket Error: java.net.BindException: Address already in use: bind

This is the server side code I'm pretty sure the error is that the Server Socket Isn't closing the port?这是服务器端代码我很确定错误是服务器套接字没有关闭端口? There is no problem when I run the program but, When I run it again I get this Error:运行程序时没有问题,但是,当我再次运行它时,出现此错误:

Exception in thread "main" java.net.BindException: Address already in use: bind
    at java.base/sun.nio.ch.Net.bind0(Native Method)
    at java.base/sun.nio.ch.Net.bind(Net.java:550)
    at java.base/sun.nio.ch.Net.bind(Net.java:539)
    at java.base/sun.nio.ch.NioSocketImpl.bind(NioSocketImpl.java:643)
    at java.base/java.net.ServerSocket.bind(ServerSocket.java:396)
    at java.base/java.net.ServerSocket.<init>(ServerSocket.java:282)
    at java.base/java.net.ServerSocket.<init>(ServerSocket.java:173)
    at internet.server.main(server.java:11)

But if I change the port number it fixes for that one time running.但是,如果我更改端口号,它会修复一次运行。 Someone please help also I haven't learned java server sockets or client sockets yet soo I need help as far as I know no one else is having this problem有人也请帮助我还没有学会 java 服务器 sockets 或客户端 sockets 但据我所知我需要帮助没有其他人有这个问题

package internet;

import java.io.IOException;
import java.net.*;
import java.io.*;

public class server {

    public static void main(String[] args) throws IOException {
        println("Compiled");
        ServerSocket ss = new ServerSocket(5502);
        Socket s = ss.accept();
        println("Client Connected!");
        InputStreamReader in = new InputStreamReader(s.getInputStream());
        BufferedReader bf = new BufferedReader(in);
        String str = bf.readLine();
        println("Client: " + str);
        
        s.close();
        ss.close();
            
    }
    
    public static void println(String args) {
        System.out.println(args);
    }
}

This will works fine as long as you terminate your previous execution before you re run it.只要您在重新运行之前终止先前的执行,这就可以正常工作。

Try closing those streams, InputStreamReader and BufferedReader as well.尝试关闭这些流、InputStreamReader 和 BufferedReader。 Close them before socket and ServerSocket.在 socket 和 ServerSocket 之前关闭它们。

bf.close();
in.close();

暂无
暂无

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

相关问题 遇到java.net.BindException:服务器客户端套接字应用程序上已在使用的地址(绑定失败) - Running into an java.net.BindException: Address already in use (Bind failed) on server- client socket app java.net.BindException:已使用的地址:JVM_Bind多客户机服务器 - java.net.BindException: Address already in use: JVM_Bind Multi Client Server java.net.BindException:地址已在使用中:JVM_Bind - java.net.BindException: Address already in use: JVM_Bind java.net.BindException:已在使用的地址:无法绑定 - java.net.BindException: Address already in use: Cannot bind 为什么即使在关闭套接字后我也会收到此错误(java.net.BindException:地址已在使用:NET_Bind)? - Why am i getting this error (java.net.BindException: Address already in use: NET_Bind) even after closing the socket? java.net.BindException:地址已在使用中 - java.net.BindException: Address already in use jenkins 更新错误:java.net.BindException:地址已在使用中 - jenkins update error: java.net.BindException: Address already in use “java.net.BindException: Address already in use”错误的解决方案? - Solution to "java.net.BindException: Address already in use" error? 如何修复错误引起的:java.net.BindException:地址已经在使用中:在Quarkus中绑定? - How to fix the error Caused by: java.net.BindException: Address already in use: bind in Quarkus? 如何解决“java.net.BindException:地址已在使用:JVM_Bind”错误? - How do I resolve the "java.net.BindException: Address already in use: JVM_Bind" error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM