Is an OutputStream in Java blocking? (Sockets)

后端 未结 3 975
清歌不尽
清歌不尽 2021-02-19 03:35

I am currently writing naive network code for a project and a mate hinted me at the possibility that when I send a package of information from the server to all clients in an it

相关标签:
3条回答
  • 2021-02-19 03:58

    Of course, when you write to a socket, this write is buffered. Socket object have a setSendBufferSize() method for setting this buffer size. If your write can be cached in this buffer, then of course, you may iterate immediately on the following socket. Otherwise this buffer need to be flushed to the client immediately. So, during flushing you are going to be blocked. If you want to avoid being blocked while flushing the buffer, you have to use a SocketChannel in non blocking I/O. Anyway, the best option for writing to many socket concurrently, is to manage each socket with a different thread, so that all writes may be executed at the same time.

    0 讨论(0)
  • 2021-02-19 03:58

    An OutputStream is blocking. It probably has some buffering, but that doesn't help you much if the server is never consuming bytes (any fixed buffer will eventually fill up). So your friend is right, you need to write in a separate thread, or use something more advanced, like nio.

    On the reading side, you can use available() to avoid blocking. No matching call exists on the write side. I wish there was.

    0 讨论(0)
  • 2021-02-19 04:02

    Your friend is right but it has more to do with how tcpip protocol works. Largely simplifying packets sent to the client need to be confirmed. If the client is not responding (fails to read incoming data, computer is under heavy load, etc.) the server won't receive acknowledgements and will stop sending the data. This mechanism built into TCP/IP prevents one end of the communication from sending large amounts of data without making sure the other end received them. In turns this avoid the requirement to resend big amounts of data.

    In Java this manifests as blocking write to OutputStream. The underlying TCP/IP stack/operating system does not allow sending more data until the client is ready to receive it.

    You can easily test this! I implemented simple server that accepts connection but fails to read incoming data:

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                final ServerSocket serverSocket = new ServerSocket(4444);
                final Socket clientSocket = serverSocket.accept();
                final InputStream inputStream = clientSocket.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }).start();
    

    And a simple client that just sends as much data as it can in 4K batches:

    final Socket client = new Socket("localhost", 4444);
    final OutputStream outputStream = client.getOutputStream();
    int packet = 0;
    while(true) {
        System.out.println(++packet);
        outputStream.write(new byte[1024 * 4]);
    }
    

    The client loop hangs on my computer after 95 iterations (your mileage may vary). However if I read from inputStream in server thread - the loop goes on and on.

    0 讨论(0)
提交回复
热议问题