3

In java, Socket has a method setSoTimeout that can be used to specify the reading timeout, but there is no method to specify the writing timeout

When I searched google, a lot of responses like "use nio and selectors", it looks that SocketChannel provides the way to specify the writing timeout, I brief the code of this class, but I still didn't find out how to do it.

Could someone show some code or some guide? Thanks

Tom
  • 5,848
  • 12
  • 44
  • 104

1 Answers1

0

One possible way to mitigate the lack of a timeout option on a write call is to simply just set the SO_SNDBUF size via Socket.setSendBuffer to something very high. This will allow subsequent calls for writing to avoid blocking unless the pipe to the remote side gets backed up. If possible, structure your protocol such that you wait for some sort of response to data already sent before sending more data. That way, you won't ever exceed the send buffer.

YMMV.

selbie
  • 100,020
  • 15
  • 103
  • 173