[C ++ winsock2.0]如何中止winsock的阻塞调用? [英] [c++winsock2.0]How to abort winsock blocking call?

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

问题描述

我使用Winsock 2在C ++中,并想知道如何让我的服务器停止从客户端连接读。读线程都会被阻止在的recv(),我不知道如何来终止它。要做到这一点的方法之一是使用非阻塞套接字使用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的,你有许多替代的这里,不刚选择()选择()只有当你的应用程序必须在许多平台上运行的一个很好的选择,你无法使用上级(但互不兼容)替代品可在每一个现代的平台。

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.

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

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