socket 异步选择 WSAAsyncSelect 用法

本文介绍了WSAAsyncSelect在Windows环境下为异步socket提供的实现方式,并详细解析了其工作原理。包括如何通过窗口消息提醒socket接收或发送状态,accept函数创建的socket与监听socket之间的关系以及消息接收机制等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WSAAsyncSelect 实现给异步socket给了另一种实现方式,就是通过窗口消息的方式来提醒对socket接收还是发送

msdn有非常全面的解释:https://msdn.microsoft.com/en-us/library/windows/desktop/ms741540(v=vs.85).aspx

本文几解释以下我认为难懂的几点:

1.The socket created by the accept function has the same properties as the listening socket used to accept it. 
由listen socket 的accept返回的socket和listen的socket 具有同样的属性。意味着accept返回的socket不用再重新注册窗口消息。这给socket服务器提供了便利。

2.When one of the nominated network events occurs on the specified socket s, the application window hWndreceives message wMsg. The wParam parameter identifies the socket on which a network event has occurred. The low word of lParam specifies the network event that has occurred. The high word of lParam contains any error code. The error code be any error as defined in Winsock2.h.

wParam标识哪一个socket发生了事件,LOWWORD(lParam)表示当前发生的是哪个事件(是接收还是发送)  而HIWORD(lParam ) 标识错误代码

3.Although WSAAsyncSelect can be called with interest in multiple events, the application window will receive a single message for each network event。
虽然注册事件可以是同时多个事件,但是消息响应是一个一个来的。

下面附上我今天写的Socket 服务端。是由上一篇博文里的Server端修改而来。

 

附件下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值