DataInputStream Readert块

时间:2019-09-17 17:05:50

标签: java sockets

我正在打开的套接字上读取和写入字节。这是我的代码。

@Test
public void testSingleRequest() throws IOException {
    Server server = new Server(9000, 100);
    new Thread(server).start();
    Socket clientSocket = new Socket("localhost", 9000);
    DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
    String payload = "a";
    byte[] load = buildPushPayload(payload);
    out.write(load);
    DataInputStream in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
    byte[] buffer = new byte[128];
    for (int i = 0; i <128 ; i++) {
        int read = in.read(buffer, 0, 128);
    }
    out.close();
    server.stop();
    assertEquals(0, buffer[0]);

由于我正在读取固定长度,因此我相信for循环不应阻塞,但仍会阻塞。如何使循环解锁?

0 个答案:

没有答案