#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 非阻塞等待套接字是否可讀/寫(xiě)
// @param[in] sockfd 套接字描述符
// @param[in] bWhichSet true - 可讀集; false - 可寫(xiě)集;
// @param[in] uiTimeOutMS 超時(shí)時(shí)長(zhǎng)(單位:微秒);
// @pre scokfd 有效套接字描述符,即大于等于零(>=0)
// @return 此函數(shù)執(zhí)行結(jié)果
// @return 0 - 可以讀/寫(xiě);
// -1 - 參數(shù)不合法;
// -2 - 檢測(cè)已超時(shí);
// @note uiTimeOutMS 超時(shí)時(shí)長(zhǎng),設(shè)為零(0),則不等待超時(shí)
static inline int
wait_rw_able( int sockfd,
bool bWhichSet,
unsigned int uiTimeOutMS )
{
// 默認(rèn)為檢測(cè)已超時(shí)
int iReturnValue = -2;
// 可讀描述符集
fd_set rset;
// 可寫(xiě)描述符集
fd_set wset;
// select 將等待的時(shí)間
timeval tv;
do // 非循環(huán),只是為了保證函數(shù)只有一個(gè)返回點(diǎn)
{
// 參數(shù)不合法
if ( 0 > sockfd )
{
iReturnValue = -1;
break;
}
// 注:每次調(diào)用 select 之前都要重設(shè)一次!
tv.tv_sec = 0;
tv.tv_usec = uiTimeOutMS;
// 檢測(cè)是否可讀
if ( true == bWhichSet )
{
// 清除其所有位
FD_ZERO( rset );
// 設(shè)置關(guān)心的描述符
FD_SET( sockfd, rset );
// 大于零(0) - 有套接字可讀,零(0) - 沒(méi)有,負(fù)數(shù) - 出錯(cuò)
if ( 0 select( sockfd + 1, // 從描述符零(0)開(kāi)始搜索,故此要對(duì)套接字描述符加壹(1)
rset, // 可讀描述符集
NULL, // 可寫(xiě)描述符集
NULL, // 異常描述符集
tv ) ) // 等待時(shí)間
{
// 可讀描述符是我們的套接字
if ( FD_ISSET( sockfd, rset ) )
{
iReturnValue = 0;
break;
}
}
}
// 檢測(cè)是否可寫(xiě)
else
{
// 清除其所有位
FD_ZERO( wset );
// 設(shè)置關(guān)心的描述符
FD_SET( sockfd, wset );
// 大于零(0) - 有套接字可讀,零(0) - 沒(méi)有,負(fù)數(shù) - 出錯(cuò)
if ( 0 select( sockfd + 1, // 從描述符零(0)開(kāi)始搜索,故此要對(duì)套接字描述符加壹(1)
NULL, // 可讀描述符集
wset, // 可寫(xiě)描述符集
NULL, // 異常描述符集
tv ) ) // 等待時(shí)間
{
// 可讀描述符是我們的套接字
if ( FD_ISSET( sockfd,
wset ) )
{
iReturnValue = 0;
break;
}
}
}
}while( 0 );
return iReturnValue;
}
// @brief 發(fā)送且接收通訊協(xié)議
// @param[int][out] pucSRBuffer 發(fā)送且接收協(xié)議字符緩沖區(qū)指針
// @param[int] usBufferLen 發(fā)送且接收協(xié)議字符緩沖區(qū)大小
// @pre pucSRBuffer 有效的協(xié)議字符緩沖區(qū)指針,且字符串長(zhǎng)度大于零(0)
// @return 此函數(shù)執(zhí)行結(jié)果
// @retval 0 成功
// @retval -1 參數(shù)不合法
// @retval -2 創(chuàng)建連接服務(wù)端的套接字失敗
// @retval -3 設(shè)置連接服務(wù)端的套接字為非阻塞模式失敗
// @retval -4 套按字非阻塞模式下也不可寫(xiě)
// @retval -5 調(diào)用 getsockopt 函數(shù)失敗
// @retval -6 調(diào)用 connect 函數(shù)失敗
// @retval -7 設(shè)置連接服務(wù)端的套接字為阻塞模式失敗
// @retval -8 發(fā)送協(xié)議數(shù)據(jù)失敗
// @retval -9 等待服務(wù)端返回?cái)?shù)據(jù)超時(shí)
// @retval -10 調(diào)用 recv 函數(shù)出錯(cuò)
// @retval -11 pucSRBuffer 指向的緩沖區(qū)空間不足
int
send_receive_data( unsigned char* const pucSRBuffer,
const unsigned short usBufferLen )
{
// 本函數(shù)執(zhí)行結(jié)果返回值
int iResult = 0; // 默認(rèn)為零(0) 表示成功
// 連接服務(wù)端的 TCP 套接字
int iServerSocket = -1;
// 服務(wù)端IP與端口
sockaddr_in sServerAddr;
// I/O 狀態(tài)標(biāo)識(shí)值
int iValue = 1;
// 獲取套接字錯(cuò)誤狀態(tài)碼
int iSo_Error = 0;
socklen_t So_Error_len = sizeof( iSo_Error );
// 接收到的通訊協(xié)議數(shù)據(jù)長(zhǎng)度
unsigned short usRealReceivedData = 0;
do // 非循環(huán),只是為了減少分支縮進(jìn)和保證進(jìn)出口唯一
{
// 1.檢查參數(shù)是否合法
if ( ( NULL == pucSRBuffer ) ||
( 0 >= usBufferLen ) ||
( 0 == pucSRBuffer[0] ) )
{
DEBUG_OUTPUT( "Invalid parameter" );
iResult = -1;
break;
}
// 2.創(chuàng)建連接服務(wù)端的套接字
iServerSocket = socket( AF_INET, // IPv4 協(xié)議
SOCK_STREAM, // TCP 套接字協(xié)議類(lèi)型
0 ); // 默認(rèn)協(xié)議,通常設(shè)置為零(0)
if ( 0 > iServerSocket )
{
DEBUG_OUTPUT( "Create socket is failed" );
iResult = -2;
break;
}
// 3.為了調(diào)用 connect 函數(shù)不阻塞,設(shè)置連接服務(wù)端的套接字為非阻塞模式
iValue = 1; //
iResult = ioctl( iServerSocket, // 服務(wù)端收發(fā)套接字
FIONBIO, // 設(shè)置或清除非阻塞I/O標(biāo)志
iValue ); // 零(0) - 清除,非零(0) - 設(shè)置
if ( 0 > iResult )
{
DEBUG_OUTPUT_PARA( "Call ioctl to set I/O asynchronization is failed, return %d",
iResult );
iResult = -3;
break;
}
sServerAddr.sin_family = AF_INET;
inet_pton( AF_INET,
m_caServerIP,
sServerAddr.sin_addr );
sServerAddr.sin_port = htons( m_usServerPort );
// 4.連接服務(wù)端
iResult = connect( iServerSocket,
(sockaddr*)sServerAddr,
sizeof( sServerAddr ) );
// 調(diào)用 connect 函數(shù),正常情況下,因?yàn)?TCP 三次握手需要一些時(shí)間,
// 而非阻塞調(diào)用只要不能立即完成就會(huì)返回錯(cuò)誤,所以這里會(huì)返回 EINPROGRESS ,
// 表示在建立連接但還沒(méi)有完成。
if ( 0 != iResult ) // 成功則返回零(0)
{
// 內(nèi)核中對(duì) connect 有超時(shí)限制是 75 秒,為了加快反應(yīng)速度此處設(shè)為750毫秒。
// 注:無(wú)論連接與否,都會(huì)返回可寫(xiě),除非有錯(cuò)誤發(fā)生,這里僅是縮短等待連接的時(shí)間而已。
iResult = wait_rw_able( iServerSocket,
false, // 是否可寫(xiě)
750000 ); // 750毫秒
if ( 0 != iResult )
{
DEBUG_OUTPUT( "Can't write in asynchronization" );
iResult = -4;
break;
}
if ( 0 > getsockopt( iServerSocket,
SOL_SOCKET,
SO_ERROR,
iSo_Error,
So_Error_len ) )
{
DEBUG_OUTPUT( "Call getsockopt is failed" );
iResult = -5;
break;
}
// 為零(0)才說(shuō)明連接成功
if ( 0 != iSo_Error )
{
DEBUG_OUTPUT( "Call connect is failed" );
iResult = -6;
break;
}
}
// 5.調(diào)用 connect 函數(shù)連接服務(wù)端成功,再設(shè)置套接字為阻塞模式(便于管理)
iValue = 0;
iResult = ioctl( iServerSocket, // 服務(wù)端收發(fā)套接字
FIONBIO, // 設(shè)置或清除非阻塞I/O標(biāo)志
iValue ); // 零(0) - 清除,非零(0) - 設(shè)置
if ( 0 > iResult )
{
DEBUG_OUTPUT_PARA( "Call ioctl to set I/O synchronization is failed, return %d",
iResult );
iResult = -7;
break;
}
// 6.發(fā)送協(xié)議數(shù)據(jù)
iResult = send( iServerSocket,
(const char*)pucSRBuffer,
strlen( (const char*)pucSRBuffer ),
0 );
// 發(fā)送異常則停止收發(fā)
if ( iResult != (int)strlen( (const char*)pucSRBuffer ) )
{
DEBUG_OUTPUT( "Call send is failed" );
iResult = -8;
break;
}
// 7.判斷是否可讀 - 即服務(wù)端是否返回?cái)?shù)據(jù)
iResult = wait_rw_able( iServerSocket, // 服務(wù)端收發(fā)套接字
true, // 是否可讀
750000 ); // 750毫秒
if ( 0 != iResult )
{
DEBUG_OUTPUT( "Waitting for recevie data has time out" );
iResult = -9;
break;
}
// 清零(0),方便調(diào)用者計(jì)算收到的通訊協(xié)議數(shù)據(jù)長(zhǎng)度
memset( pucSRBuffer, 0, usBufferLen );
do
{
// 8.從客戶(hù)端接收數(shù)據(jù)
iResult = recv( iServerSocket, // 服務(wù)端收發(fā)套接字
pucSRBuffer + usRealReceivedData, // 存放數(shù)據(jù)的緩沖區(qū)地址
usBufferLen - usRealReceivedData - 1, // 每次讀出的字節(jié)
0 ); // 默認(rèn)為零(0),無(wú)特殊要求
// 返回負(fù)數(shù)為出錯(cuò)了,直接跳出不再等待嘗試接收新數(shù)據(jù)
if ( 0 > iResult )
{
DEBUG_OUTPUT_PARA( "Call recv is failed, return %d", iResult );
iResult = -10;
break;
}
// 接收數(shù)據(jù)時(shí)網(wǎng)絡(luò)中斷就會(huì)返回零(0)
if ( 0 == iResult )
{
break;
}
usRealReceivedData += iResult;
// 傳出參數(shù)所指緩沖區(qū)空間不足矣放下全部應(yīng)簽數(shù)據(jù)
if ( usBufferLen = usRealReceivedData )
{
DEBUG_OUTPUT( "pucSRBuffer is not superfluous space" );
iResult = -11;
break;
}
}while( 0 == wait_rw_able( iServerSocket,
true, // 是否可讀
750000 ) ); // 750毫秒
// 收數(shù)據(jù)時(shí)出錯(cuò)了,則直接跳出返回
if ( 0 > iResult )
{
break;
}
// 執(zhí)行至此發(fā)收通訊數(shù)據(jù)完畢
iResult = 0;
break;
}while( 0 );
// 套接字創(chuàng)建成功,則要釋放資源
if ( -1 != iServerSocket )
{
close( iServerSocket );
}
return iResult;
}
標(biāo)簽:淮南 河北 黔南 隴南 河池 通遼 常州 黔南
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《linux下非阻塞模式網(wǎng)絡(luò)通訊模型示例分享》,本文關(guān)鍵詞 linux,下非,阻塞,模式,網(wǎng)絡(luò)通訊,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。