Hello all
Hello all
我使用C ++连接套接字
I have use C++ to connect a socket
如果连接OK时间为0秒,
If connect OK time will is 0 second,
如果连接失败的时间总是20秒。
If connect failed time alway 20 second.
如果连接失败,如何设置时间是5秒。
How to to set time is 5 second if connect failed.
我有代码C#是检查时间,但我不知道用C ++编写代码。
I have code C# is check time, but I don't know code it in C++.
public static int PingHost(string _HostURI, int _PortNumber) { using (TcpClient tcp = new TcpClient()) { IAsyncResult ar = tcp.BeginConnect(_HostURI, _PortNumber, null, null); System.Threading.WaitHandle wh = ar.AsyncWaitHandle; try { if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5), true)) { tcp.Close(); return -1; throw new TimeoutException(); } if (tcp.Connected) tcp.EndConnect(ar); else return -1; } finally { wh.Close(); } return 0; } }
请帮我解决。
非常感谢,
TínhNg。
推荐答案
您好,
首先你应该阅读这篇文章: http://www.codeguru / forum / showthread.php?t = 312668
first you should read this article: www.codeguru/forum/showthread.php?t=312668
这是一种定义超时的方法: forums.codeguru/showthread.php?48476-Time-out-in-MFC-Sockets(看最后的答案)。
Here is a way to define a timeout: forums.codeguru/showthread.php?48476-Time-out-in-MFC-Sockets (look at the last answer).
HTH,Guido
HTH, Guido
更多推荐
如何设置tcp连接的超时
发布评论