为什么在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();
}