linux下非阻塞模式网络通讯模型示例分享

2019-09-30 18:53:16刘景俊

                break;
            }

            // 为零(0)才说明连接成功
            if ( 0 != iSo_Error )
            {
                DEBUG_OUTPUT( "Call connect is failed" );

                iResult = -6;
                break;
            }
        }

        // 5.调用 connect 函数连接服务端成功,再设置套接字为阻塞模式(便于管理)
        iValue = 0;
        iResult = ioctl( iServerSocket, // 服务端收发套接字
                         FIONBIO,       // 设置或清除非阻塞I/O标志
                         &iValue );     // 零(0) - 清除,非零(0) - 设置
        if ( 0 > iResult )
        {
            DEBUG_OUTPUT_PARA( "Call ioctl to set I/O synchronization is failed, return %d",
                               iResult );

            iResult = -7;
            break;
        }

        // 6.发送协议数据
        iResult = send( iServerSocket,
                        (const char*)pucSRBuffer,
                        strlen( (const char*)pucSRBuffer ),
                        0 );
        // 发送异常则停止收发