如果没有任何响应,请等待一些特定的时间取消事件 [英] Wait for some specfic time if any response doesn't come cancel an event

查看:99
本文介绍了如果没有任何响应,请等待一些特定的时间取消事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在使用套接字编程来使用c ++连接到TCP/IP服务器.
当用户选择TCP/IP服务器的无效IP地址时. connect()API需要花费大量时间.

我希望我的应用程序< small< small< small>在特定时间内等待该API的响应.如果在那个特定时期内没有回应.我的应用程序不应尝试更多用于连接到该IP地址.

请为我提供一些实现此目标的方法.

Hi Everyone,


I am using socket programming to connect to the TCP/IP server using c++.
when user selects an invalid IP address of TCP/IP server. The connect() API takes so much time.

I want my application <small></small>to wait for the response from that API for specific time. If no response comes in that specific period. My application should not try more for connecting to that IP address.

Please suggest me some ways to achieve this.

推荐答案

很遗憾,没有timneout属性,但是,有一种简单的方法可以计时连接.遵循以下步骤:

1)创建一个名为Connected的变量,设置为false
2)您想打电话的地方:
1)将Connected设置为false
2)调用Socket.BeginConnect
3)使用ManualResetEvent.WaitOne(*超时长度*)
3)调用AsynConnect事件时,将Connected设置为true或false并设置等待的ManualResetEvent
4)如果正在等待的ManualResetEvent超时,则Connected将为false,您可以处理它;或者,如果连接失败,Connected将为false,然后您可以处理;或者,如果所有成功,Connected将为true,则您可以继续使用该连接. br/>
这是一个简单的解决方案,几乎不需要线程.

希望这会有所帮助,

Ed:)
There is unfortunately no timneout property, however, there is an easy way to time the connection. With the follwoing steps:

1) Create a variable called Connected, set to false
2) Where you want to call connect:
1) Set Connected to false
2) Call Socket.BeginConnect
3) Use an ManualResetEvent.WaitOne(*length of timeout*)
3) When the AsynConnect event is called set Connected to true or false and set the waiting ManualResetEvent
4) If the waiting ManualResetEvent timesout then Connected will be false and you can handle it or if the connection fails Connected will be false and you can then handle or, if all succeeded, Connected will be true and you can continue using that connection.

It is a simple solution with little, practically no, need for threading.

Hope this helps,

Ed :)


好吧,问题是您无法在连接方法上指定超时(不幸的是).更改PC的环境值的唯一方法是不建议这样做,因为此更改将应用​​于在该特定PC上使用该功能的每个应用程序.

您可以做的是,如果每次调用connect方法都不根据指定的超时值返回,则创建一个单独的线程,而忽略结果. (我建议使用线程池)

D,库尔特.
Well the problem is that you can not specify the timeout (unfortunately) on the connection method. The only way to do this to change the environmental values of the PC, I do not recommend this since this change will apply to every application that uses that function on that particular pc.

What you could do is create a separate thread for every call to the connect method if the thread doesn’t return according your specified timeout value ignore the result. (I suggest using a thread pool)

D, Kurt.


这篇关于如果没有任何响应,请等待一些特定的时间取消事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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