我正在尝试为我的一个项目编写一个简单的套接字服务器,但它只接受一个请求,然后不接受来自任何地方的任何其他请求。
public void run() {
println("Socket server running...");
try {
sock = new ServerSocket(20424);
while(true) {
clientSocket = sock.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String data;
while((data = in.readLine()) != null) {
println("[SocketServer] " + data);
if(data.equalsIgnoreCase("COUNT(TRIGGERS)")) {
out.println(getTriggerCount());
}
}
out.println("endOfStream");
out.close();
in.close();
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
它将接受一个请求并以正确数量的数据进行响应,但在那之后就没有任何效果了。这是我用来连接的客户端:
public static void main(String[] args) {
try {
socket = new Socket("localhost", 20424);
out = new PrintWriter(socket.getOutputStream(),
true);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out.println("COUNT(TRIGGERS)");
String data;
while((data = in.readLine()) != null) {
System.out.println(data);
}
socket.close();
} catch (UnknownHostException e) {
System.out.println("Unknown host");
System.exit(1);
} catch (IOException e) {
System.out.println("No I/O");
System.exit(1);
}
}