简体   繁体   English

Java套接字超时:管道损坏

[英]Java socket timing out: Broken pipe

I'm writing a simple server in Java, and I'm able to retrieve incoming data from the client on the server side, but not on the client side due to a 2000ms timeout. 我正在用Java语言编写一个简单的服务器,并且能够从服务器端的客户端检索传入的数据,但由于2000ms超时,因此无法从客户端检索。 Anyone know why this times out? 有人知道为什么会超时吗?

This is the server's code: 这是服务器的代码:

private static void listen() throws IOException {
    while(true) {
        Socket clientSocket = serverSocket.accept();
        StringBuilder bufferedStringInput = new StringBuilder(); 
        CharBuffer cbuf = CharBuffer.allocate(4096);
        try {
            InputStream is = clientSocket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF8"));
            int noCharsLeft = 0;
            while ((noCharsLeft = br.read(cbuf)) != -1) { 
                char[] arr = new char[noCharsLeft];
                cbuf.rewind();
                cbuf.get(arr);
                bufferedStringInput.append(arr);
                cbuf.clear();
            }           
            System.out.println(bufferedStringInput.toString());
        } catch (IOException e) {
            System.out.println("Error received client data: " + e.getMessage());
        }

        String message = "Hello client";
        try {
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
            out.print(message);
        } catch (IOException e) {
            System.out.println("Error getting output stream from client: " + e.getMessage());
        }
        clientSocket.close();       
    } 
}

You're reading the input until end of stream, which only happens when the peer closes the connection, then you're trying to write to it, so of course you get a broken pipe. 您正在读取输入,直到流结束,这仅在对等方关闭连接时发生,然后您尝试对其进行写入,因此,您当然会遇到断线的情况。 Doesn't make sense. 没道理 You should just read the input until you have one entire request, whatever that means in your protocol. 您应该只读取输入内容,直到收到一个完整的请求,无论协议中的含义如何。

There are other problems lurking here: 这里还有其他问题:

  • If the client code uses readLine() , you're not sending a line terminator: use println(), not print(), and close the PrintWriter , not just the client socket. 如果客户端代码使用readLine() ,则不发送行终止符:使用println(),而不是print(),并关闭PrintWriter ,而不仅仅是客户端套接字。

  • cbuf.rewind()/get()/clear() should be cbuf.flip()/get()/compact(). cbuf.rewind()/get()/clear()应该是cbuf.flip()/get()/compact().

  • But it would make more sense to read directly into a char[] cbuf = new char[8192]; 但是直接读入char[] cbuf = new char[8192];会更有意义char[] cbuf = new char[8192]; array, then bufferedStringInput.append(cbuf, 0, noCharsLeft), and forget about the CharBuffer altogether. 数组,然后是bufferedStringInput.append(cbuf, 0, noCharsLeft),并且完全bufferedStringInput.append(cbuf, 0, noCharsLeft), CharBuffer Too much data copying at present. 当前数据复制过多。

  • noCharsLeft is a poor name for that variable. noCharsLeft是该变量的较差名称。 It is a read count. 这是一个读取计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM