代碼如下:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sysexits.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef __ENABLED_DEBUG_INFO_OUTPUT__
#define DEBUG_OUTPUT(format) printf( "/nFile: %s : Line: %d ->Function: %s/n"format"/n", __BASE_FILE__, __LINE__, __FUNCTION__ )
#define DEBUG_OUTPUT_PARA(format,...) printf( "/nFile: %s : Line: %d ->Function: %s/n"format"/n", __BASE_FILE__, __LINE__, __FUNCTION__, __VA_ARGS__ )
#else
#define DEBUG_OUTPUT(format)
#define DEBUG_OUTPUT_PARA(format,...)
#endif
// @brief 非阻塞等待套接字是否可讀/寫
// @param[in] sockfd 套接字描述符
// @param[in] bWhichSet true - 可讀集; false - 可寫集;
// @param[in] uiTimeOutMS 超時時長(單位:微秒);
// @pre scokfd 有效套接字描述符,即大于等于零(>=0)
// @return 此函數執行結果
// @return 0 - 可以讀/寫;
// -1 - 參數不合法;
// -2 - 檢測已超時;
// @note uiTimeOutMS 超時時長,設為零(0),則不等待超時
static inline int
wait_rw_able( int sockfd,
bool bWhichSet,
unsigned int uiTimeOutMS )
{
// 默認為檢測已超時
int iReturnValue = -2;
// 可讀描述符集
fd_set rset;
// 可寫描述符集
fd_set wset;
// select 將等待的時間
timeval tv;
do // 非循環,只是為了保證函數只有一個返回點
{
// 參數不合法
if ( 0 > sockfd )
{
iReturnValue = -1;
break;
}
// 注:每次調用 select 之前都要重設一次!
tv.tv_sec = 0;
tv.tv_usec = uiTimeOutMS;
新聞熱點
疑難解答