如何中止 winsock 阻塞调用? [英] How to abort winsock blocking call?

查看:26
本文介绍了如何中止 winsock 阻塞调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C++ 中使用 Winsock 2,想知道如何让我的服务器停止从客户端连接读取.读取线程在 recv() 中被阻塞,我不知道如何中止它.一种方法是使用带有 select() 的非阻塞套接字,但该线程在检查新的 select() 之前必须等待.

I use Winsock 2 in C++, and wonder how to make my server stop reading from the client connection. The reading thread gets blocked in recv() and I have no idea how to abort it. One way to do this is use non-blocking sockets with select(), but this thread has to wait before checking the new select().

停止读取套接字的正确方法是什么?

What is the proper way to stop reading the socket?

推荐答案

如果你的程序除了使用套接字 I/O 之外还有其他事情要做,你首先不应该阻塞.

If your program has other things to do besides working with socket I/O, you should not block in the first place.

您声称您的线程必须等待,但这只是您程序当前设计的反映.你应该重新设计它,让它与某种形式的非阻塞套接字一起工作.每个网络问题都可以使用非阻塞套接字来解决.

You claim your thread must wait, but that's just a reflection of your program's current design. You should redesign it so it works with some form of non-blocking sockets. Every networking problem can be addressed using non-blocking sockets.

由于您使用的是 Winsock,因此您有许多替代方案,不只是 select().select() 仅当您的应用程序必须在多个平台上运行并且您无法使用每个现代平台上可用的卓越(但相互不兼容)替代方案时才是一个不错的选择.

Since you're using Winsock, you have many alternatives here, not just select(). select() is a good choice only if your application must run on many platforms and you are unable to use the superior (but mutually incompatible) alternatives available on every modern platform.

这篇关于如何中止 winsock 阻塞调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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