我如何设置的时间了socket.connect()的调用? [英] How do I set the time out of a socket.connect() call?

查看:246
本文介绍了我如何设置的时间了socket.connect()的调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序连接到主机上可能会下降。如果主机下来,我不想等待30秒左右才能超时。我使用的是阻塞式的套接字的时刻。

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.

我一直在寻找socket.poll()和socket.select(),但我宁愿只是有一个时间上的插槽设置。我不介意,如果这是一个设置我必须在系统中做的地方。另外,我似乎明白了民意调查,并选择具有面向连接的通信-is这个正确不工作?

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?

推荐答案

见的 BeginConnect 异步编程概述

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

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

更新:有一个更好的例子<一href="http://channel9.msdn.com/forums/TechOff/41602-TcpClient-or-Socket-Connect-timeout/?CommentID=212796"相对=nofollow>这里。

这篇关于我如何设置的时间了socket.connect()的调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆