Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java socket / output stream writes : do they block?

If I am only WRITING to a socket on an output stream, will it ever block? Only reads can block, right? Someone told me writes can block but I only see a timeout feature for the read method of a socket - Socket.setSoTimeout().

It doesn't make sense to me that a write could block.

like image 751
jbu Avatar asked Aug 27 '09 04:08

jbu


People also ask

Does writing to a socket block?

If O_NONBLOCK is set, write() does not block the process. If some data can be written without blocking the process, write() writes what it can and returns the number of bytes written. Otherwise, it sets errno to EAGAIN and returns -1.

Are Java sockets blocking?

Java has TCP and UDP sockets. The methods such as connect(), accept(), read(), and write() defined in the ServerSocket and Socket class are used for blocking socket programming. For example, when a client invokes the read() method to read data from the server, the thread gets blocked until the data is available.

Is input stream of socket is connected to the output stream of remote socket?

The input stream connects to the output stream of the remote socket. It returns the output stream of the socket.

Which of the constructors creates an unconnected socket?

Constructor Summary Creates an unconnected socket, with the system-default type of SocketImpl. Creates a stream socket and connects it to the specified port number at the specified IP address. Deprecated.


1 Answers

A write on a Socket can block too, especially if it is a TCP Socket. The OS will only buffer a certain amount of untransmitted (or transmitted but unacknowledged) data. If you write stuff faster than the remote app is able to read it, the socket will eventually back up and your write calls will block.

Responding to these followup questions:

So is there a mechanism to set a timeout for this? I'm not sure what behavior it'd have...maybe throw away data if buffers are full? Or possibly delete older data in the buffer?

There is no mechanism to set a write timeout on a java.net.Socket. There is a Socket.setSoTimeout() method, but it affects accept() and read() calls ... and not write() calls. Apparently, you can get write timeouts if you use NIO, non-blocking mode, and a Selector, but this is not as useful as you might imagine.

A properly implemented TCP stack does not discard buffered data unless the connection is closed. However, when you get a write timeout, it is uncertain whether the data that is currently in the OS-level buffers has been received by the other end ... or not. The other problem is that you don't know how much of the data from your last write was actually transferred to OS-level TCP stack buffers. Absent some application level protocol for resyncing the stream*, the only safe thing to do after a timeout on write is to shut down the connection.

By contrast, if you use a UDP socket, write() calls won't block for any significant length of time. But the downside is that if there are network problems or the remote application is not keeping up, messages will be dropped on the floor with no notification to either end. In addition, you may find that messages are sometimes delivered to the remote application out of order. It will be up to you (the developer) to deal with these issues.

* It is theoretically possible to do this, but for most applications it makes no sense to implement an additional resyncing mechanism on top of an already reliable (to a point) TCP/IP stream. And if it did make sense, you would also need to deal with the possibility that the connection closed ... so it would be simpler to assume it closed.

like image 88
Stephen C Avatar answered Oct 05 '22 17:10

Stephen C