为什么服务器没有看到客户端发送数据?
Why server didn't see the clients sending data?
为什么 serverSocket.accept() 什么也没发生?服务器从客户端获取数据,但程序在方法 serverSocket.accept()
上什么都不做
public void start() {
try {
Socket socket= serverSocket.accept();
System.out.println("Client accepted");
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
String line = "";
try {
line = in.readUTF();
System.out.println(line);
} catch (IOException i) {
System.out.println(i);
}
} catch (IOException e) {
System.out.println(e);
}
}
serverSocket.accept();
在连接套接字之前阻塞。
Socket s = serverSocket.accept();
in = new DataInputStream(new BufferedInputStream(s.getInputStream()));
或
socket = serverSocket.accept();
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
编辑 - 29.03.2019 11.54
所以我们知道:
serverSocket.accept();
阻止您的线程。你在你的主线程中调用它。
...
successfulThreads = new AtomicInteger(0);
latch = new CountDownLatch(testcaseThreads);
gate = new CyclicBarrier(testcaseThreads + 1);
server = new Server(SOCKET_PORT);
server.start(); //<-- Your program blocks!!!
// ||
// || STOP! No execution of the code.
// \//
// \/
for (File testcaseFile : testcaseFiles) {
new Thread(new MockClient(this, testcaseFile)).start();
}
为什么 serverSocket.accept() 什么也没发生?服务器从客户端获取数据,但程序在方法 serverSocket.accept()
上什么都不做public void start() {
try {
Socket socket= serverSocket.accept();
System.out.println("Client accepted");
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
String line = "";
try {
line = in.readUTF();
System.out.println(line);
} catch (IOException i) {
System.out.println(i);
}
} catch (IOException e) {
System.out.println(e);
}
}
serverSocket.accept();
在连接套接字之前阻塞。
Socket s = serverSocket.accept();
in = new DataInputStream(new BufferedInputStream(s.getInputStream()));
或
socket = serverSocket.accept();
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
编辑 - 29.03.2019 11.54
所以我们知道:
serverSocket.accept();
阻止您的线程。你在你的主线程中调用它。
...
successfulThreads = new AtomicInteger(0);
latch = new CountDownLatch(testcaseThreads);
gate = new CyclicBarrier(testcaseThreads + 1);
server = new Server(SOCKET_PORT);
server.start(); //<-- Your program blocks!!!
// ||
// || STOP! No execution of the code.
// \//
// \/
for (File testcaseFile : testcaseFiles) {
new Thread(new MockClient(this, testcaseFile)).start();
}