socket soSetTimeout clarification

ตาม Document


public void setSoTimeout(int timeout)
throws SocketException
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. 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 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.

Parameters: timeout - the specified timeout, in milliseconds. Throws: SocketException - if there is an error in the underlying protocol, such as a TCP error.

มันจะจับเวลาที่ InputStream ตอน Read ค่าจาก Client ถ้า Client ไม่ติดต่อส่งค่ามาในระยะเวลา timeout
socket จะ Throws: SocketException แต่ไม่ได้ตัด connection ให้เราไป handled ใน SocketException เอง ว่าจะทำอะไรต่อ


String readme;
 readme = bs.readline;
// TODO do stuff with readme
catch (SocketTimeoutException e)
// did not receive the line. readme is undefined, but the socket can still be used
 socket.close(); // disconnect, for example

อ้างอิง >

Related posts:

This entry was posted in java. Bookmark the permalink.