linux Tcp非阻塞 接收數據


/**********************************************************************
* 函數名稱: 
* 功能描述: 接收H264視頻數據(TCP方式)
* 輸入參數: handle        SOCKET句柄
            MaxRecvLen    數據最大的接收長度
* 輸出參數: buf         接收數據的緩存地址
            len            接收數據的長度
* 返 回 值:0為成功,1為失敗 
* 其它說明: 
***********************************************************************/
int RecvH264Data(int handle, char *buf, int *len, int MaxRecvLen)
{
    int recvbytes;
    int recvLen = MaxRecvLen;
    while(1)
    {
        recvbytes = recv( handle, buf, recvLen, 0 );//MSG_DONTWAIT
        if ( recvbytes <= 0 )
        {
            if(errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
            {
                LOGI( "Continue to!" );
                usleep(100);
                continue;
            } 
            else
            {
                LOGI( "recv error!" );
                return(1);
            }
        }

        if(recvbytes == recvLen)
        {
            LOGI( "return TRUE : %d\n",recvbytes );
            *len = recvbytes;
            break;
        }
        else
        {
            LOGI( "recvbytes : %d\n",recvbytes );
            recvLen -= recvbytes;
            buf += recvbytes;
            continue;
        }
    }
    
    return(0);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM