我需要一些帮助,我想我遇到了一个与网络相关的简单问题。它还可以帮助我更好地了解这一切是如何工作的,因为它知道什么不是 .close()'ed。我敢肯定这很简单,但对我来说它都是全新的。这是客户端程序。如果我能解决这个问题,我很可能会附加服务器。谢谢
public class Server {
public static void main(String[] args) {
start();
}
static int start = 0;
public static void start() {
try {
ServerSocket serverSocket = new ServerSocket(4567);
Socket socket = serverSocket.accept();
//1) Take and echo input (In this case a message)
BufferedReader bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message = bf.readLine();
System.out.println("Message recieved from Client:" + message);
//2) Response of client message
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
printWriter.println("Server echoing back the message ' " + message + " ' from Client");
} catch (IOException e) {
System.out.println("e " + e);
System.exit(-1);
}
start++;
clearUp();
if (start < 5) {
System.out.println("Closing binds and Restarting" + start);
start();
}
}
public void clearUp(){
//How would I clear the stuff that is left bound
so I can restart via start() and avoid the
java.net.BindException: Address already in use: JVM_Bind ?
}
}
我将如何清除被绑定的东西,以便我可以通过 start() 重新启动并避免 java.net.BindException: Address already in use: JVM_Bind ?