关于epoll在ET模式下接收数据的测试

关于epoll在ET模式下接收数据的测试
根据epoll的manual , epoll在ET模式下触发可读信号后必须一直读取直到出现EAGAIN ,一般在应用中会预先分布至少足够一个完整报文大小的缓冲区,接收到可读信号后可以一次性读取所有数据,但是当单个报文足够大时,很可能出现系统缓冲空间不足,从之导致无法一次性读取所有数据,虽然数据已经到达 。也就是说,一个数据可读信号触发的读取操作很可能无法读取所有数据,需要循环读取直到遇到EAGAIN 。

while ( 1 )
{
    
int  ret  =  read(fd,buf + pos, sizeof (buf) - pos) ;
    
if  ( ret  ==   0  )
    {   
//  client disconnect
    }
    
else   if  ( ret  <   0  )
    {
        
if  ( errno  ==  EAGAIN )  break  ;
        
else   if  ( errno  ==  EINTR  )  continue  ;
        
//  some error occurs
    }
    pos 
+=  ret ;
    
// check if buffer full or do request
    //**************note : sometimes the FIN and data arrive at the same time
}
//  do request

测试结果:

[
215745 ][tcp_handler.cpp: 59 ]read  fd = 8  num = 3426
[
215745 ][tcp_handler.cpp: 61 ]total read  fd = 8  num = 3426       //一次完成读取所有数据
[
215750 ][tcp_handler.cpp: 53 ]disconnect  fd = 8  
[
215751 ][server_app.cpp: 169 ] new  connection , fd = 8
[
215751 ][tcp_handler.cpp: 59 ]read  fd = 8  num = 57538
[
215751 ][tcp_handler.cpp: 59 ]read  fd = 8  num = 3426
[
215751 ][tcp_handler.cpp: 61 ]total read  fd = 8  num = 60964     //两次才完成读取所有数据
[
215756 ][tcp_handler.cpp: 53 ]disconnect  fd = 8  
[
215758 ][server_app.cpp: 169 ] new  connection , fd = 8
[
215758 ][tcp_handler.cpp: 59 ]read  fd = 8  num = 57538
[
215758 ][tcp_handler.cpp: 59 ]read  fd = 8  num = 3426
[
215758 ][tcp_handler.cpp: 61 ]total read  fd = 8  num = 60964
[
215803 ][tcp_handler.cpp: 53 ]disconnect  fd = 8  
[
215806 ][server_app.cpp: 169 ] new  connection , fd = 8
[
215806 ][tcp_handler.cpp: 59 ]read  fd = 8  num = 57538
[
215806 ][tcp_handler.cpp: 59 ]read  fd = 8  num = 3426
[
215806 ][tcp_handler.cpp: 61 ]total read  fd = 8  num = 60964
[
215811 ][tcp_handler.cpp: 53 ]disconnect  fd = 8

你可能感兴趣的:(关于epoll在ET模式下接收数据的测试)