[英]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.