如何在客户端套接字连接上设置超时? [英] How to set timeout on client socket connection?
问题描述
我正在尝试在Java中的客户端套接字上设置连接的超时.我已将默认的连接超时设置为2000,即:
I am trying to set the timeout of a connection on the client socket in java. I have set a default connect timeout to 2000, i.e:
this.socket.connect(this.socketAdd, timeOut);
我正在尝试在Web应用程序上.当用户发出请求时,我正在将值传递给套接字服务器,但是如果我在5秒钟内未收到任何响应,则套接字应断开连接.但就我而言,整个请求将再次提交.谁能告诉我我要去哪里错了?
This I am trying on a web application. When a user makes a request, I am passing values to socket server, but if I don't receive any response in 5 secs the socket should disconnect. But in my case the whole request is getting submitted once again. Can any one please tell me where am I going wrong?
如果5秒钟内没有任何响应,我想断开套接字连接.我该如何设置?任何示例代码都会有所帮助.
I want to cut the socket connection, if I don't get any response in 5 secs. How can I set it? Any sample code would help.
推荐答案
您甚至可以尝试:
Socket client=new Socket();
client.connect(new InetSocketAddress(hostip,port_num),connection_time_out);
这篇关于如何在客户端套接字连接上设置超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!