关于POLL检测对端关闭连接----《Linux高性能服务器编程》读书笔记

本文介绍了一种利用POLL机制检测对端关闭连接的方法,并通过聊天室服务器与客户端的示例代码进行了验证。该方法包括两种检测手段:触发POLLIN事件结合recv返回0和触发POLLRDHUP事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POLL检测对端关闭连接有两种方法:

1)触发POLLIN事件,recv调用返回0

2)触发POLLRDHUP事件

其中POLLRDHUP 需要2.6.17以上内核,而且代码最开始处定义_GNU_SOURCE,实际上测试不定义也行。测试代码如下:

聊天室服务器代码

//#define _GNU_SOURCE 1
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <poll.h>
#include <fcntl.h>
#include <assert.h>
#include <vector>
#include <map>

#define USER_LIMIT	5			//最大用户数量
#define BUF_SIZE	64
#define FD_LIMIT	65536		//文件描述符限制

#define PRINT_DEBUG 1			//打印调试日志

//客户端连接数据
typedef struct client_data
{
	int			 fd_;
	sockaddr_in	 addr_;
	char*	wr_buf_;
	char buf_[BUF_SIZE];
}client_data_t;

typedef std::map< int, client_data_t > MAP_USER_DATA;

typedef std::vector< pollfd >	VEC_POLLFD;


int remove_userdata( MAP_USER_DATA& mapuserdata, int fd )
{
	int ret = -1;
	MAP_USER_DATA::iterator it = mapuserdata.find( fd );
	if( it != mapuserdata.end() )
	{
		ret = 0;
		mapuserdata.erase( it );
	}
	
	return ret;
}


int setnobloking( int fd )
{
	int old_opt = fcntl( fd, F_GETFL );
	int new_opt = old_opt | O_NONBLOCK;
	fcntl( fd, F_SETFL, new_opt );
	return old_opt;
}

int main( int argc, char* argv[] )
{
	if( argc <= 2 )
	{
		printf( "usage: %s ip port\n", argv[0] );
		return 1;
	}
	
	const char* ip = argv[1];
	int port = atoi( argv[2] );
	int ret = 0;
	
	struct sockaddr_in addr;
	bzero( &addr, sizeof( addr ) );
	addr.sin_family = AF_INET;
	inet_pton( AF_INET, ip, &addr.sin_addr );
	addr.sin_port = htons( port );
	
	int listenfd = socket( PF_INET, SOCK_STREAM, 0 );
	assert(  listenfd != -1 );
	
	
	ret = bind( listenfd, ( struct sockaddr* )&addr, sizeof( addr ) );
	assert( ret != -1 );
	
	ret = listen( listenfd, 5 );
	assert( ret != -1 );
	printf( "listen[%s:%d] success fd[%d]\n", ip, port, listenfd );
	
	MAP_USER_DATA map_userdata;
	
	VEC_POLLFD vec_fds;
	pollfd pollfd_svr;
	pollfd_svr.fd = listenfd;
	pollfd_svr.events = POLLIN /*| POLLERR*/;
	pollfd_svr.revents = 0;
	vec_fds.push_back( pollfd_svr );
	
	int user_count = 0;
	VEC_POLLFD vec_newcli_fds;
	while( 1 )
	{
		ret = poll( &( *vec_fds.begin() ), vec_fds.size(), -1 );
		if( ret < 0 )
		{
			printf( "poll failed err[%d]\n", errno );
			break;
		}
		
		
		VEC_POLLFD::iterator itvec = vec_fds.begin();
		for( ; itvec != vec_fds.end() ; )
		{
#if PRINT_DEBUG
			printf( "fd[%d] revents[0x%x]\n", itvec->fd, itvec->revents );
#endif
			if( ( itvec->fd == listenfd ) && ( itvec->revents & POLLIN ) )
			{
				struct sockaddr_in cli_addr;
				socklen_t cli_addrlen = sizeof(  cli_addr );
				int clifd = accept( listenfd, ( struct sockaddr* )&cli_addr, &cli_addrlen );
				if(  clifd < 0 )
				{
					printf( "accept failed err[%d]\n", errno );
					itvec++;
					continue;
				}
				if( user_count > USER_LIMIT )
				{
					//TODO?
				}
				
				printf( "new client[%s:%d] connect fd[%d] user count[%d]\n", 
				inet_ntoa( cli_addr.sin_addr ), ntohs( cli_addr.sin_port ), clifd, ++user_count );
				
				client_data_t clidata = {0};
				clidata.fd_ = clifd;
				clidata.addr_ = cli_addr;
				map_userdata[clifd] = clidata;
				
				setnobloking( clifd );
	
				pollfd pollfd_cli;
				pollfd_cli.fd = clifd;
				pollfd_cli.events = POLLIN | POLLRDHUP | POLLERR;
				pollfd_cli.revents = 0;
				vec_newcli_fds.push_back( pollfd_cli );
			}//new client connection coming
			else if( itvec->revents & POLLERR )
			{
				//TODO:
				//printf( "fd[%d] err\n", itvec->fd );
				//continue;
			}
			else if( itvec->revents & POLLRDHUP )
			{
				//client close connection
				//FIXME:useing recv() = 0 to judge peer close connection
				printf( "client close connection fd[%d] POLLRDHUP\n", itvec->fd );
				user_count--;
				close( itvec->fd );
				remove_userdata( map_userdata, itvec->fd );
				itvec = vec_fds.erase(itvec);
				continue;
			}
			else if( itvec->revents & POLLIN )
			{
				MAP_USER_DATA::iterator it = map_userdata.find( itvec->fd );
				if( it == map_userdata.end() )
				{
					printf( "can not find user data by fd[%d]!\n", itvec->fd );
					itvec++;
					continue;
				}
				client_data_t& userdata = it->second;
				memset( userdata.buf_, 0, sizeof( userdata.buf_ ) );
				ret = recv( userdata.fd_, userdata.buf_, sizeof( userdata.buf_ ) - 1 , 0 );
				if( ret < 0 )
				{
						if( errno != EAGAIN && errno != EWOULDBLOCK )
						{
							//read error
							printf( "client close connection fd[%d] err[%d] vec_fds size[%d]\n", itvec->fd, errno, vec_fds.size() );
							user_count--;
							close( itvec->fd );
							remove_userdata( map_userdata, itvec->fd );
							itvec = vec_fds.erase(itvec);
							continue;
						}
				}
				else if( ret == 0 )
				{
					//peer close connection
					printf( "client close connection fd[%d]\n", itvec->fd );
					user_count--;
					close( itvec->fd );
					remove_userdata( map_userdata, itvec->fd );
					itvec = vec_fds.erase(itvec);
					continue;			
				}
				else
				{
					printf( "client[%d] --> msg[%s]\n", userdata.fd_, userdata.buf_ );
					//notify other client socket to ready to send data
					MAP_USER_DATA::iterator itmap_temp;
					VEC_POLLFD::iterator itvec_tmp = vec_fds.begin();
					for( ; itvec_tmp != vec_fds.end(); itvec_tmp++ )
					{
						if( itvec_tmp->fd == listenfd )
						{
							continue;
						}
						itvec_tmp->events |= ~POLLIN;
						itvec_tmp->events |= POLLOUT;
						itmap_temp = map_userdata.find( itvec_tmp->fd );
						if( itmap_temp == map_userdata.end() )
						{
							printf( "not find fd[%d]!", itvec_tmp->fd );
							continue;
						}
						itmap_temp->second.wr_buf_ = userdata.buf_;
					}
					
				}
			}
			else if( itvec->revents & POLLOUT )
			{
				MAP_USER_DATA::iterator itmap_tmp = map_userdata.find( itvec->fd );
				if( itmap_tmp == map_userdata.end() )
				{
					itvec++;
					continue;
				}
				client_data_t& userdata = itmap_tmp->second;
				if( !userdata.wr_buf_ )
				{
					itvec++;
					continue;
				}
				
				ret = send( userdata.fd_, userdata.wr_buf_, strlen( userdata.wr_buf_ ), 0 );
				printf( "--> client[%d] msg[%s]\n", userdata.fd_, userdata.wr_buf_ );
				userdata.wr_buf_ = NULL;
				itvec->events |= ~POLLOUT;
				itvec->events |= POLLIN;
			}
			itvec++;
		}
		//add new client fds
		if( vec_newcli_fds.size() > 0 )
		{
			vec_fds.insert( vec_fds.end(), vec_newcli_fds.begin(), vec_newcli_fds.end() );
			vec_newcli_fds.clear();
		}
	}//while
	
	close( listenfd );
	
	return 0;
}




聊天室客户端代码

#define _GNU_SOURCE 1
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <poll.h>
#include <fcntl.h>
#include <assert.h>

#define BUF_SIZE 64


int main( int argc, char* argv[] )
{
	if( argc <=2 )
	{
		printf("usage: %s ip port\n", argv[0]);
		return 1;
	}
	
	const char* ip = argv[1];
	int port = atoi( argv[2] );
	
	struct sockaddr_in svr_addr;
	bzero( &svr_addr, sizeof( svr_addr ) );
	svr_addr.sin_family = AF_INET;
	inet_pton( AF_INET, ip, &svr_addr.sin_addr );
	svr_addr.sin_port = htons( port );
	
	int sockfd = socket( PF_INET, SOCK_STREAM, 0 );
	assert( sockfd >= 0 );
	if( connect( sockfd, (struct sockaddr*)&svr_addr, sizeof(svr_addr) ) < 0 )
	{
		printf( "connect [%s:%d] failed err[%d]\n", ip, port, errno );
		close( sockfd );
		return 1;
	}
	printf( "connect [%s:%d] success\n", ip, port );
	//注册文件描述符0(标准输入)和文件描述符sockfd上的可读事件
	pollfd fds[2];
	fds[0].fd = 0;
	fds[0].events = POLLIN;
	fds[0].revents = 0;
	fds[1].fd = sockfd;
	fds[1].events = POLLIN | POLLRDHUP;
	fds[1].revents = 0;
	
	char rd_buf[BUF_SIZE];
	char stdin_buf[BUF_SIZE];
	int pipefd[2];
	int ret = pipe( pipefd );
	assert( ret != -1 );
	
	while( 1 )
	{
		ret = poll( fds, 2, -1 );
		if( ret < 0 )
		{
			printf( "poll failed err[%d]\n", errno );
			break;
		}
		
		if(  fds[1].revents & POLLRDHUP )
		{
			printf( "server close the connect!\n" );
			break;
		}
		
		if( fds[1].revents & POLLIN )
		{
			memset( rd_buf, 0, sizeof( rd_buf ) );
			ret = recv( fds[1].fd, rd_buf, BUF_SIZE - 1, 0 );
			printf( "svr --> %s\n", rd_buf );
			
		}
		
		if( fds[0].revents & POLLIN )
		{
			//使用splice将用户输入数据直接写到sockfd上(零拷贝)
			//FIXME:不支持splice,不知道是否是虚拟机系统问题
			/*ret = splice( 0, NULL, pipefd[1], NULL, 100,
					SPLICE_F_MORE | SPLICE_F_MOVE );
					printf( "splice 1 err[%d]\n", errno );
			ret = splice( pipefd[0], NULL, sockfd, NULL, 100,
					SPLICE_F_MORE | SPLICE_F_MOVE );
			*/
			
			memset( stdin_buf, 0, sizeof( stdin_buf ) );
			ret = read( fds[0].fd, stdin_buf, BUF_SIZE - 1 );
			if( ret <= 0 )
			{
				printf( "read stdin failed err[%d]", errno );
				continue;
			}
			char* rdata = strtok( stdin_buf, "\n" );
			if( strcmp( rdata, "q" ) == 0 || strcmp( rdata, "Q" ) == 0 )
			{
				printf( "user exit chatroom\n" );
				break;
			}
			else if( strcmp( rdata, "d" ) == 0 || strcmp( rdata, "D" ) == 0 )
			{
				printf( "i will core dump!\n" );
				char* p;
				*p = 0;
				break;
			}
			ret = send( sockfd, rdata, strlen( rdata ), 0 );
			printf( "send msg[ %s ] --> svr\n", rdata );
		}
		
	}//while
	
	
	close( pipefd[0] );
	close(  pipefd[1] );
	close( sockfd );
	return 0;
}

2种方式检测对端关闭连接对比:

 

POLLRDHUP

recv() = 0

客户端主动close

YES

YES

Ctrl+c 关闭客户端

YES

YES

客户端异常退出(段错误)

YES

YES

实际上向POLL注册了POLLRDHUP,POLLIN事件,
pollfd_cli.events = POLLIN | POLLRDHUP ;

对端关闭时,这2个事件都能触发,日志打印:

fd[4] revents[0x2001]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值