简体   繁体   English

JAVA:何时使用Socket setSoTimeout?

[英]JAVA : When to use Socket setSoTimeout?

I am making an application where the client sends a message to the server and then waits for 5 seconds (lets assume) for the server to respond and if there is no return message, it retries again. 我正在制作一个应用程序,其中客户端将消息发送到服务器,然后等待5秒钟(假设),服务器响应,如果没有返回消息,它将再次重试。 If the server responds with the message then the client process it. 如果服务器响应该消息,则客户端将对其进行处理。 This goes on in loop and again happens after sometime. 这种情况不断循环,并在一段时间后再次发生。

For this purpose I was thinking to use setSoTimeout(time) on the Client Socket but after reading the javadoc and a lot of explanations on the internet I am confused as to whether this approach is right. 为此,我当时正在考虑在Client Socket上使用setSoTimeout(time),但是在阅读了Javadoc和互联网上的很多解释之后,我对于这种方法是否正确感到困惑。

What I read on the internet 我在网路上阅读的内容

(1) If I use setSoTimeout on the socket then it gives the timeout for the duration in which the connection needs to be established and if it is not established then it retries to establish the connection for the given time. (1)如果我在套接字上使用setSoTimeout,则它给出需要建立连接的持续时间的超时,如果未建立,则会在给定时间内重试建立连接。

(2) If I use setSoTimeout on the socket then it waits for incoming messages for the specified time interval and if no message is received then it stops waiting. (2)如果我在套接字上使用setSoTimeout,则它将在指定的时间间隔内等待传入消息,如果没有收到消息,则它将停止等待。

My questions are - 我的问题是-

(1) Which of the above are true ? (1)以上哪一项是正确的?

(2) If the second statement is true, then can I use it for my implementation ? (2)如果第二条陈述为真,那么我可以在实现中使用它吗?

(3) If the second statement is true, when does the timeout timer kickoff exactly ? (3)如果第二条陈述为真,那么超时定时器启动何时开始? Is it when I declare the socket and set the timeout period on it or is it when I send the message ? 是在声明套接字并为其设置超时期限时还是在发送消息时?

If either of the explanation don't apply to my case then what is it that I should do to wait for a fixed interval of time on the client side for the server to reply ? 如果两种解释均不适用于我的情况,那么我应该怎么做才能在客户端上等待固定的时间间隔以便服务器回复? If the reply does come I should process it and move on and redo the same process. 如果确实收到答复,我应该处理它,然后继续并重做相同的过程。 If the reply doesn't come I should move ahead and redo the whole process again. 如果没有收到答复,我应该继续进行,然后重做整个过程。

(1) If I use setSoTimeout() on the socket then it gives the timeout for the duration in which the connection needs to be established and if it is not established then it retries to establish the connection for the given time. (1)如果我在套接字上使用setSoTimeout() ,那么它将给出需要建立连接的持续时间的超时,如果未建立,则会在给定时间内重试建立连接。

This is incorrect. 这是不正确的。 setSoTimeout() does not cause re-establishment of the connection at all, let alone 'for the given time'. setSoTimeout()根本不会导致重新建立​​连接,更不用说“在给定时间内”了。

(2) If I use setSoTimeout() on the socket then it waits for incoming messages for the specified time interval and if no message is received then it stops waiting. (2)如果我在套接字上使用setSoTimeout() ,则它将在指定的时间间隔内等待传入消息,如果没有收到消息,则它将停止等待。

This is slightly more accurate, but there is no such thing as a message in TCP. 这稍微更准确,但是在TCP中没有消息。

The correct explanation is that it blocks for up to the specified timeout for at least one byte to arrive. 正确的解释是,它阻塞了指定的超时时间,至少有一个字节到达。 If nothing arrives within the timeout, a SocketTimeoutException is thrown. 如果在超时时间内什么都没有到达,则抛出SocketTimeoutException

(1) Which of the above are true? (1)以上哪一项是正确的?

Neither. 都不行

(2) If the second statement is true, then can I use it for my implementation? (2)如果第二条陈述为真,那么我可以在执行中使用它吗?

It isn't, so the second part doesn't apply, but if any statement is true you can use it as part of your implementation. 它不是,因此第二部分不适用,但是如果有任何声明为真,则可以将其用作实现的一部分。 You don't have to ask. 你不用问

(3) If the second statement is true, when does the timeout timer kickoff exactly? (3)如果第二条语句为true,则超时计时器何时开始?

When you call read() . 当您调用read()

Is it when I declare the socket and set the timeout period on it or is it when I send the message? 是在我声明套接字并为其设置超时期限时还是在发送消息时?

Neither. 都不行

If either of the explanation don't apply to my case then what is it that I should do to wait for a fixed interval of time on the client side for the server to reply? 如果两种解释均不适用于我的情况,那么我应该怎么做才能在客户端上等待固定的时间间隔以便服务器回复?

Set a read timeout. 设置读取超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM