如何设置tcp连接的超时

编程入门 行业动态 更新时间:2024-10-28 03:33:42
本文介绍了如何设置tcp连接的超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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连接的超时

本文发布于:2023-10-16 06:14:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何设置   tcp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!