3. Blocking and non-blocking in tcp

       
        struct timeval timeout = { 10 , 0 };
int err = 0x00 ;
err = setsockopt ( client_fd , SOL_SOCKET, SO_SNDTIMEO,( const char *)&timeout, sizeof (timeout));
if (err != 0x00 ){ perror ( "socket send conf err" ); throw ( "" );}
err = setsockopt( client_fd, SOL_SOCKET, SO_RCVTIMEO,( const char*)&timeout, sizeof(timeout));
if(err != 0x00){ perror( "socket recv conf err"); throw( "");}


在tcp中,一般情况下,只要服务器收不到数据,recv会是阻塞的.send函数也是阻塞的.

但是我们有时候想完成的功能是,服务器收不到数据,但是还是想往客户端发送数据怎么办.

tcp中提供setsockopt函数,将时间设置为10s,当tcpserver 10s没有收到数据的时候,将自动发送,而不是在等待接收.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325857782&siteId=291194637