今日开始探寻socket编程中select的妙用

LTO编译器优化介绍以及开启方法 LTO优化的详细介绍 阅读详情

第一步:全文转发MSDN介绍

select
The Windows Sockets select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O.

int select (
  int nfds,                          
  fd_set FAR * readfds,              
  fd_set FAR * writefds,             
  fd_set FAR * exceptfds,            
  const struct timeval FAR * timeout 
);
 
Parameters
nfds
[in] This parameter is ignored; it is included only for compatibility with Berkeley sockets.
readfds
[in/out] An optional pointer to a set of sockets to be checked for readability.
writefds
[in/out] An optional pointer to a set of sockets to be checked for writability
exceptfds
[in/out] An optional pointer to a set of sockets to be checked for errors.
timeout
[in] The maximum time for select to wait, or NULL for blocking operation.
Remarks
The select function is used to determine the status of one or more sockets. For each socket, the caller can request information on read, write or error status. The set of sockets for which a given status is requested is indicated by an FD_SET structure. The sockets contained within the FD_SET structures must be associated with a single service provider. For the purpose of this restriction, sockets are considered to be from the same service provider if the WSAPROTOCOL_INFO structures describing their protocols have the same providerId value. Upon return, the structures are updated to reflect the subset of these sockets that meet the specified condition. The select function returns the number of sockets meeting the conditions. A set of macros is provided for manipulating an FD_SET structure. These macros are compatible with those used in the Berkeley software, but the underlying representation is completely different.

The parameter readfds identifies the sockets that are to be checked for readability. If the socket is currently in the listen state, it will be marked as readable if an incoming connection request has been received such that an accept is guaranteed to complete without blocking. For other sockets, readability means that queued data is available for reading such that a call to recv, WSARecv, WSARecvFrom, or recvfrom is guaranteed not to block.

For connection-oriented sockets, readability can also indicate that a request to close the socket has been received from the peer. If the virtual circuit was closed gracefully, and all data was received, then a recv will return immediately with zero bytes read. If the virtual circuit was reset, then a recv will complete immediately with an error code such as WSAECONNRESET. The presence of out-of-band data will be checked if the socket option SO_OOBINLINE has been enabled (see setsockopt).

The parameter writefds identifies the sockets that are to be checked for writability. If a socket is processing a connect call (nonblocking), a socket is writable if the connection establishment successfully completes. If the socket is not processing a connect call, writability means a send, sendto, or WSASendto are guaranteed to succeed. However, they can block on a blocking socket if the len parameter exceeds the amount of outgoing system buffer space available. It is not specified how long these guarantees can be assumed to be valid, particularly in a multithreaded environment.

The parameter exceptfds identifies the sockets that are to be checked for the presence of out-of-band data (see section DECnet Out-Of-band data for a discussion of this topic) or any exceptional error conditions.

Important Out-of-band data will only be reported in this way if the option SO_OOBINLINE is FALSE. If a socket is processing a connect call (nonblocking), failure of the connect attempt is indicated in exceptfds (application must then call getsockopt SO_ERROR to determine the error value to describe why the failure occurred). This document does not define which other errors will be included.

Any two of the parameters, readfds, writefds, or exceptfds, can be given as NULL. At least one must be non-NULL, and any non-NULL descriptor set must contain at least one handle to a socket.

Summary: A socket will be identified in a particular set when select returns if:

readfds:

If listen has been called and a connection is pending, accept will succeed
Data is available for reading (includes OOB data if SO_OOBINLINE is enabled)
Connection has been closed/reset/terminated
writefds:

If processing a connect call (nonblocking), connection has succeeded
Data can be sent
exceptfds:

If processing a connect call (nonblocking), connection attempt failed
OOB data is available for reading (only if SO_OOBINLINE is disabled)
Four macros are defined in the header file WINSOCK2.H for manipulating and checking the descriptor sets. The variable FD_SETSIZE determines the maximum number of descriptors in a set. (The default value of FD_SETSIZE is 64, which can be modified by defining FD_SETSIZE to another value before including WINSOCK2.H.) Internally, socket handles in an FD_SET structure are not represented as bit flags as in Berkeley Unix. Their data representation is opaque. Use of these macros will maintain software portability between different socket environments. The macros to manipulate and check FD_SET contents are:

FD_CLR(s, *set)
Removes the descriptor s from set.
FD_ISSET(s, *set)
Nonzero if s is a member of the set. Otherwise, zero.
FD_SET(s, *set)
Adds descriptor s to set.
FD_ZERO(*set)
Initializes the set to the NULL set.
The parameter timeout controls how long the select can take to complete. If timeout is a null pointer, select will block indefinitely until at least one descriptor meets the specified criteria. Otherwise, timeout points to a TIMEVAL structure that specifies the maximum time that select should wait before returning. When select returns, the contents of the TIMEVAL structure are not altered. If TIMEVAL is initialized to {0, 0}, select will return immediately; this is used to "poll" the state of the selected sockets. If select returns immediately, then the select call is considered nonblocking and the standard assumptions for nonblocking calls apply. For example, the blocking hook will not be called, and Windows Sockets will not yield.

Note The select function has no effect on the persistence of socket events registered with WSAAsyncSelect or WSAEventSelect.

Return Values
The select function returns the total number of socket handles that are ready and contained in the FD_SET structures, zero if the time limit expired, or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, WSAGetLastError can be used to retrieve a specific error code.

Error Codes
WSANOTINITIALISED A successful WSAStartup must occur before using this function.
WSAEFAULT The Windows Sockets implementation was unable to allocate needed resources for its internal operations, or the readfds, writefds, exceptfds, or timeval parameters are not part of the user address space.
WSAENETDOWN The network subsystem has failed.
WSAEINVAL The timeout value is not valid, or all three descriptor parameters were NULL.
WSAEINTR A blocking Windows Socket 1.1 call was canceled through WSACancelBlockingCall.
WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAENOTSOCK One of the descriptor sets contains an entry that is not a socket.

QuickInfo
  Windows NT: Yes
  Windows: Yes
  Windows CE: Use version 1.0 and later.
  Header: Declared in winsock2.h.
  Import Library: Link with ws2_32.lib.

揭秘cocos-mcp:如何用AI助手提升CocosCreator游戏开发效率 本文深入介绍了cocos-mcp工具,它作为连接AI助手与CocosCreator编辑器的桥梁,遵循MCP协议,能显著提升游戏开发效率。通过自然语言指令,开发者可让AI直接查询场景结构、批量修改属性或生成上下文代码,将重复操作变为智能调度,实现从“手动操作者”到“指令指挥官”的工作流变革。 阅读详情

相关推荐

SAP PPDS启发式排产实战:从零配置到自定义规则开发(附ABAP代码示例)

本文详细介绍了SAP PPDS启发式排产的实战应用,从基础配置到自定义规则开发的全流程。通过ABAP代码示例,展示了如何实现多目标优化、实时响应和约束可视化,帮助企业提升订单交付率、减少设备闲置时间。文章还包含开发环境准备、标准模板解析、自定义开发步骤及性能优化技巧,适合制造业数字化转型的实践参考。

seed的博客 183

关于socket select的理解

socket通信中select函数的使用和解释 博文转载于https://www.cnblogs.com/gangzilife/p/9766292.html,我觉得对于select的说明比较明白。 Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只 是习惯写诸如 connect、accept、recv或recvfrom这样的阻塞程序(所谓阻塞方式block,顾名思义,就 是进程或是线程执行到这些函数时必须等待某个事件的发生,如果事件没

weixin_37859743的博客 1218

GD32F427移植LiteOS-M实战:如何快速复用官方Demo进行二次开发

本文详细介绍了如何在GD32F427开发板上移植LiteOS-M操作系统,通过复用OpenHarmony官方Demo快速构建RTOS工程。文章涵盖环境搭建、芯片配置迁移、中断向量表处理和外设驱动适配等关键步骤,并提供了调试技巧和二次开发进阶建议,帮助开发者高效完成移植工作。

weixin_33736048的博客 415

socket的select的用法

select 是一个用于在多个套接字上进行非阻塞 I/O 操作的函数。它可以同时监视多个套接字,并在有数据可读、可写或有错误发生时返回。该函数的语法如下: int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); nfds 是最大的文件描述符值加 1。...

weixin_35755823的博客 630

Win11家庭版如何让Win+R运行CMD默认管理员权限?3种方法实测有效

本文针对Win11家庭版用户,实测了三种让Win+R运行CMD默认获取管理员权限的有效方法。通过分析UAC机制与家庭版功能限制,详细介绍了注册表编辑、快捷方式创建与任务计划程序三种方案的原理、步骤与风险对比,帮助用户根据安全与效率需求选择最佳方案,实现一键高效管理。

sat99的博客 1270

高性能网站设计之缓存更新的套路

看到好些人在写更新缓存数据代码时,先删除缓存,然后再更新数据库,而后续的操作会把数据再装载的缓存中。然而,这个是逻辑是错误的。试想,两个并发操作,一个是更新操作,另一个是查询操作,更新操作删除缓存后,查询操作没有命中缓存,先把老数据读出来后放到缓存中,然后更新操作更新了数据库。于是,在缓存中的数据还是老的数据,导致缓存中的数据是脏的,而且还一直这样脏下去了。 我不知道为什么这么多人用的都是这

代码技巧 1万+

select

elect在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如connect、accept、recv或recvfrom这样的阻塞程序(所谓阻塞方式block,顾名思义,就是进程或是线程执行到这些函数时必须等待某个事件

fysy0000的专栏 840

R语言新手也能搞定!WGCNA分析全流程代码详解(附常见报错解决方案)

本文为R语言新手提供了WGCNA分析的完整代码流程与实战指南。从环境配置、数据准备、软阈值选择、网络构建到模块-表型关联分析,逐步详解核心步骤。重点针对安装依赖、内存不足等常见报错提供了具体解决方案,帮助初学者避开陷阱,顺利完成加权基因共表达网络分析。

terraform7cloud的博客 539

socket can 编程

Socket can 文档原名称:can.txt,位于 linux-3.2.0-m3352/Documentation/networking/can.txt。 CAN 控制器局域网络协议族之 Readme 文件(aka Socket CAN)。另外在该文档的后边附加一个CAN收发测试程序,用于与下位机通信。 目录 1 概述 / 什么是 Socket CAN? 2 目的 / 为什么要使用 socket API? 3 socket can 概念 3.1 接收列表 3.2 数据发送的本地回环模式(loopback

Mr.W的博客 9071

PX4无人机-Gazebo仿真实现动态目标追踪的视觉控制方案

本文详细介绍了PX4无人机在Gazebo仿真环境中实现动态目标追踪的视觉控制方案。从PX4与Gazebo仿真环境搭建、动态目标追踪核心技术栈、仿真环境深度定制技巧到追踪算法的优化实践,提供了全面的技术指导和常见问题排查方法,帮助开发者高效实现无人机视觉追踪功能。

weixin_29196315的博客 450

YOLOv8改进策略【Backbone/主干网络】| 2023 U-Net V2 替换骨干网络,加强细节特征的提取和融合

一、本文介绍 本文记录的是基于U-Net V2的YOLOv8目标检测改进方法研究。本文利用U-Net V2替换YOLOv8的骨干网络,UNet V2通过其独特的语义和细节融合模块(SDI),能够为骨干网络提供更丰富的特征表示。并且其中的注意力模块可以使网络聚焦于图像中与任务相关的区域,增强对关键区域特征的提取,进而提高模型精度。本文配置了原论文中pvt_v2_b0、pvt_v2_b1、pvt_v2_b2、pvt_v2_b3、pvt_v2_b4和pvt_v2_b5六种模型,以满足不同的需求。 文章目录一、本文

Limiiiing的博客 1721

SOCKET编程中,select()函数的作用

    select()在SOCKET编程中还是比较重要的,可是对于初学SOCKET的人来说都不太爱用select()写程序,他们只是习惯写诸如 conncet()、accept()、recv()或recvfrom这样的阻塞程序(所谓阻塞方式block,顾名思义,就是进程或是线程执行到这些函数时必须等待某个事件发生,如果事件没有发生,进程或线程就被阻塞,函数不能立即返回)。可是使用select()

韦编二绝 2260

3 ATK2(AUTOSAR OS)代码移植-目标硬件及编译环境简介

该部分介绍ATK2代码移植的目标硬件以及编译环境

xiaoduanduansad的博客 718

select在socket编程妙用

用过 WinSock API 网友们知道:WinSock 编程中有一很方便的地方便是其 息驱动机制,不管是底层 API 的 WSAAsyncSelect() 还是 MFC 的异步Socket类: CAsyncSocket,都提供了诸如 FD_ACCEPT、FD_READ、FD_CLOSE 之类的消息 供编程人员捕捉并处理。FD_ACCEPT 通知进程有客户方Socket请求连接, FD_READ通

beyondjjw的专栏 2154

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

【win网络编程】socket中的recv阻塞和select的用法

转载请注明出处:作者 kikilizhm 在编写ftp客户端程序时,在联通后使用recv函数进行接收欢迎信息时,需要申请内存进行接收数据保存,一次读取成功,但是由于一个随机的ftp服务端在说,欢迎信息的大小是不知道的,所以在尝试使用死循环,在阅读recv的说明时讲到返回值即是接收到的字节数,那么返回0的时候就代表结束了,实践发现recv是个阻塞函数,在连接不断开的情况下,会一直处于阻塞状

kikilizhm的专栏 1万+

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

select 读数据

本文出自:   作者:   (2001-09-13   09:00:00)       发信站:   华南网木棉站   (Tue   Aug     4   15:43:41   1998),   转信             【   原文由   cpu   所发表   】                 用过   WinSock   API   网友们知道:WinSock   编程中有一很方便的

dadalan的专栏 1719

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

对Socket CAN的理解(2)——【Socket的原理及使用】

转载请注明出处:http://blog.youkuaiyun.com/Righthek 谢谢!            为了能够对Socket CAN的深入理解,我们需要了解Socket的机制。          Socket的中文翻译为“插座”,在计算机世界里称为套接字。Socket最初是作为网络上不同主机之间进程的通信接口,后来应用越来越广,在同一主机上的不同进程之间通信也可以用Socket。简单来说,

Wayne Woo的专栏 2万+

SocketCAN

From Wikipedia, the free encyclopedia   (Redirected from Socketcan) Jump to: navigation, search Free software portal SocketCAN is a set of open source CAN drivers an

flymachine的专栏 4831

socket select()用法,哪些socket api会阻塞

一、winsock中 #include  //#include 原型 int select( int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, const struct timeval* timeout ); nfds:本参数忽略,仅起到兼容作用。    readfds:(可选)指针,指

wangzhong1979的专栏 2910

linux socketCAN通信,linux can 总线socket接口测试使用

最近调试一个sja1000的can驱动,发现到了2.6.36,linux把can总线封装成了网络接口。内核文档里给出了这么修改的原因。1. Overview / What is Socket CAN--------------------------------The socketcan package is an implementation of CAN protocols (Controll...

weixin_30647853的博客 801

Windows socket I/O模型 之 select(3)

好久没有研究select了,不过我近几天我又有时间研究了。 mu

oldmtn的专栏 974

linux之socketcan

Socket CAN 对CAN protocol做了封装,对CAN设备的读写变成了类似tcp/ip的读写网络(socket操作)。 目标环境:linux2.6.37, i.mx27开发板带mcp2515 首先注册spi device和mcp2515 device: 代码 1 static const int plat27_pins[] __initconst= { 2 ...

weixin_33951761的博客 400

2013-7-15 socket study_1

#define FD_ZERO(set) (((fd_set FAR *)(set))->fd_count=0) /*  * The new type to be used in all  * instances which refer to sockets.  */ typedef u_int           SOCKET; /*  * Select uses arrays o

thinker 1074

在SOCKET编程中,select()函数的作用

转自https://zhidao.baidu.com/question/6530466.html?qbl=relate_question_3&word=select%D4%DAsocket Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如connect、accept、recv或recvfrom这样的阻塞程序(所谓

qingkongyeyue的博客 1159
上一篇: Sleep魅力的应用
下一篇: 中国经济的未来出路在于农村经济(2009-10-24 12:51:35)
PerfectToday
博客等级 码龄18年 46粉丝 129原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值