如何在tcp / ip协议中实现超时 [英] how implements a timeout in tcp/ip protocol

查看:165
本文介绍了如何在tcp / ip协议中实现超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello all

Hello all

我想通过以下方式询问服务器(tcp / ip connect):

I want ask a server (tcp/ip connect) with the following means:

socket()或者WSASocket()

socket() or WSASocket()

connect()或WSAConnect()

connect() or WSAConnect()

send()或WSASend()

send() or WSASend()

recv()或WSARecv()

recv() or WSARecv()

如果连接正常,它可以正常工作,但是如果出现问题(例如链接断开),响应会延迟响应。

it works if the connection is ok, but if a problem occurs (broken link for example) response is delayed in time.

如何实现超时以限制响应时间?

how can i implements a timeout to limit response time?

全部谢谢。

推荐答案

你使用取消令牌。

You use can cancellation token.

cancellation_token_source cts;auto token = cts.get_token();

您可以在要使用WinRTsocket APIS的任务中传递令牌,例如connectAsync()。您可以取消任务取消此令牌。

You can pass token in the task where you want to use WinRTsocket APIS like connectAsync() .You can cancel the task out of the task cancelling this token.

cts.cancel()

使用事件(CreateEventEx() 和WaitForSingleObjectEx( ))您可以随时取消您的令牌希望。

Using an event(CreateEventEx()  and WaitForSingleObjectEx()) you can cancel your token any time you want.

然后你的任务也将被取消。

Then your Task will also be cancelled.

我说的是Windows商店应用程序。

I am talking about windows store apps.


这篇关于如何在tcp / ip协议中实现超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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