尝试运行回显服务器时无法读取/写入数据到套接字。没有抛出异常,也没有对控制台输入的响应。代码中有什么不正确的地方?
public class Server {
static ServerSocket server;
public static void main(String[] args) throws IOException {
String hostname = "127.0.0.1";
try{
server = new ServerSocket(8888);
} catch (IOException e) {
System.out.println("Could not listen on port 8888");
System.exit(-1);
}
Socket theSocket = null;
try {
theSocket = new Socket(hostname, 8888);
BufferedReader networkIn = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(theSocket.getOutputStream(), true);
System.out.println("Connected to echo server");
while (true) {
String theLine = userIn.readLine();
if (theLine.equals("."))
break;
out.println(theLine);
out.flush();
System.out.println("networkIn: "+networkIn.readLine());
}
networkIn.close();
out.close();
System.out.println("out.close();");
} catch (IOException e) {
e.printStackTrace();
}
}
}