每当我通过调用创建ServerSocket
并观察套接字地址时getLocalSocketAddress()
,我都会看到:
0.0.0.0/0.0.0.0:xxxx(xxxx是随机端口号)
我的服务器代码是:
try{
Boolean end = false;
ServerSocket ss = new ServerSocket(0);
System.out.println("Program running, Server address:" + ss.getLocalSocketAddress().toString());
while(!end){
//Server is waiting for client here, if needed
Socket s = ss.accept();
System.out.println("Socket Connected !");
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
String st = input.readLine();
System.out.println("Tcp Example From client: "+st);
output.println("Good bye and thanks for all the fish :)");
s.close();
}
ss.close();
} catch (Exception ex) {
ex.printStackTrace();
}