用winsock线程并强制recv返回 [英] threading with winsock and forcing recv to return

查看:86
本文介绍了用winsock线程并强制recv返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,起初我使用的是非阻塞套接字,但是当我尝试捕获来自多个用户的单个事件时,它很快变得混乱和不准确.如此之多,以至于我认为即使使用互斥锁,即使是多线程也是更好的选择.但是我有一个问题.在每个连接的每个线程中,应该在recv处阻塞并无限期等待,但是,那么,让我说我希望关闭服务器,并在recv中结束线程?反正有强迫recv立即返回吗?是否将套接字从另一个线程(中间recv)设置为非阻塞模式,可能导致它返回,还是必须在调用recv之前将其设置好?

okay, at first i was working with non-blocking sockets, but then when i was trying to catch individual events from multiple users, it quickly became cluttered and innaccurate. So much so, that i think even multithreading is a better alternative, just using mutexes. But i''ve got a problem. in each thread for each connection, it is supposed to block at recv and wait indefinately, but then, let''s say that i wish to shutdown the server, and end the thread mid recv? is there anyway to force recv to return immediately? does setting the socket to non-blocking mode from another thread, mid recv, maybe cause it to return or does it HAVE to be set before recv is called?

推荐答案

使用select()查看是否有数据包在调用recv()之前等待.

如果没有数据包在等待,请不要调用recv.

您可以改为调用Sleep(1).
Use select() to see if you have a packet waiting before you call recv().

If no packet is waiting, then don''t call recv.

You might call Sleep(1) instead.


这篇关于用winsock线程并强制recv返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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