Socket异步阻塞进行接收数据

WSAStartup(0x0202,&wsaData);

 HANDLE rtp_video_sock=socket(AF_INET,SOCK_DGRAM,0);
if( rtp_video_sock==INVALID_SOCKET)
{
closesocket(_rtspObj->rtp_video_sock);
return 0;
}
 


addr.sin_family=AF_INET; 
addr.sin_addr.s_addr=htonl(INADDR_ANY);
addr.sin_port = htons((short)_rtspObj->rtp_port+2);
if(bind(_rtspObj->rtp_video_sock,(LPSOCKADDR)&addr,sizeof(addr))==SOCKET_ERROR)
{
closesocket(_rtspObj->rtp_video_sock);
return 0;
}


//设置非堵塞通讯
DWORD ul= 1;
int  _res = 0;
_res =  ioctlsocket(_rtspObj->rtp_video_sock,FIONBIO,&ul); 


int nRecvBuf=64*1024;//设置为32K 
int leng = 4; 
_res = setsockopt(_rtspObj->rtp_video_sock,SOL_SOCKET,SO_RCVBUF,(const char*)&nRecvBuf,sizeof(int));  
   
 
while(1)
{   
struct timeval _timeout;
fd_set fds_r,fds_e; 
int i_total=0;
int i_ret;
int count = 0;
 
   /* Initialize file descriptor set */
FD_ZERO(&fds_r);
FD_SET(_rtspObj->rtp_video_sock,&fds_r);  
/* We'll wait 0.5 second if nothing happens */
_timeout.tv_sec=1;
_timeout.tv_usec=0;//500000;


i_ret=select(_rtspObj->rtp_video_sock+1,&fds_r,NULL,NULL,&_timeout) ;
if(i_ret<=0)
continue;
recvlen = recvfrom(_rtspObj->rtp_video_sock,(char *)buf,4096,0,(struct sockaddr *)&saddrclient,&addrlen1);
if(recvlen > 0)

{//数据处理

}
}

 

你可能感兴趣的:(Socket异步阻塞进行接收数据)