What is the functionality of setSoTimeout and how it works?

后端 未结 3 1804
旧时难觅i
旧时难觅i 2020-12-12 19:08

I\'m trying to learn Socket by myself. I\'m little bit confused by following text from Oracle website. I have some questions regarding that. Thanks in advance for any clear

3条回答
  •  抹茶落季
    2020-12-12 19:25

    The JavaDoc explains it very well:

    With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

    SO_TIMEOUT is the timeout that a read() call will block. If the timeout is reached, a java.net.SocketTimeoutException will be thrown. If you want to block forever put this option to zero (the default value), then the read() call will block until at least 1 byte could be read.

提交回复
热议问题