【发布时间】:2012-07-02 20:57:40
【问题描述】:
每当我创建一个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();
}
【问题讨论】:
-
注意:1.你应该使用
Log而不是System.out.println()2.ss超出范围,例如编译器应该抱怨它是一个未知变量。 -
很抱歉没有粘贴整个代码。没有编译器错误,只是因为整个代码没有进入我的问题中的代码部分,这就是为什么我没有复制整个代码