How do I set the time out of a socket.connect() call? [duplicate]

流过昼夜 提交于 2019-12-06 12:00:38

问题


I have an app that connects to a host that might be down. If the host is down I don't want to wait for the 30 or so seconds it takes to time out. I'm using blocking sockets at the moment.

I've been looking at socket.poll() and socket.select() but I'd rather just have a time setting on the socket. I don't mind if it's a setting I have to do somewhere in the system. Also, I seemed to understand that poll and select don't work with connection oriented communication -is this correct?

If this is absolutely impossible, what is a nice way to get the results I want using poll, select or some other technique?


回答1:


See BeginConnect and Asynchronous Programming Overview

IAsyncResult asr = socket.BeginConnect( ip, port, null, null );

bool res = asr.AsyncWaitHandle.WaitOne( 10000, true );  // 10 sec timeout

Update: There is a better example here.



来源:https://stackoverflow.com/questions/456891/how-do-i-set-the-time-out-of-a-socket-connect-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!