59 Linux socket的select编程

Posted Chasing_Chasing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了59 Linux socket的select编程相关的知识,希望对你有一定的参考价值。

59.1 引言

        作为代码搬运工,正常来说写过一次代码之后,就不想重新的重复造轮子,所以特此记录,Linux下的socket的select编程相关配置及例程。

59.2 例程

        Linux socket例程。



int ht_socketOpen(void)

	int		ni				= 0;
	int		ret				= 0;
	int		sock_fd			= 0;
	int 	optval			= 0;
	int		sock_nbio		= 1;
	int 	keep_alive 		= 1;
	int 	isIPAddrFlag	= 0;
	int rcvBufSize 			= (4*1024);
	int rcvBufSizelen 		= sizeof(rcvBufSize);

	struct addrinfo		*res = NULL;
	struct addrinfo		hints;
	struct sockaddr_in 	local_ipv4;
	struct sockaddr_in 	server_ipv4;
	struct ql_data_call_info info = 0;

	if(gsock_fd >0)
	
		Console_Error("socket is already opened\\r\\n");
		return gsock_fd;
	
	
	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family 	= AF_INET;
	hints.ai_socktype 	= SOCK_STREAM;

	//创建tcp套接字
	sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(sock_fd < 0)
	
		Console_Error("open socket failed\\r\\n");
		goto iExit;
	
	
	//保存打开的socket
	gsock_fd = sock_fd;

	//设置接受缓冲区大小
	ret = setsockopt(sock_fd, SOL_SOCKET, SO_RCVBUF, &rcvBufSize, sizeof(rcvBufSize));
	if(ret < 0)
	
		Console_Error("setsockopt SO_RCVBUF failed\\r\\n");
		goto iExit;
	
	//获取系统修改后的大小
	ret = getsockopt(sock_fd, SOL_SOCKET, SO_RCVBUF, &rcvBufSize, (socklen_t*)&rcvBufSizelen);
	if(ret < 0)
	
		Console_Error("getsockopt SO_RCVBUF failed\\r\\n");
		goto iExit;
	


	//设置接收阻塞时间
	optval	= 1000;
	ret = setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &optval, sizeof(optval));
	if (ret < 0) 
	
		Console_Error("setsockopt SO_RCVTIMEO failed\\r\\n");
		goto iExit;
	

	optval = 1;
	ret = setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(int));
	if (ret < 0) 
	
		Console_Error("setsockopt TCP_NODELAY failed\\r\\n");
		goto iExit;
	

	//设置链接为keepalive类型
	ret = setsockopt(sock_fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keep_alive, sizeof(keep_alive));
	if(ret < 0)
	
		Console_Error("setsockopt SO_KEEPALIVE failed\\r\\n");
		goto iExit;
	

	optval = 180;
	if (setsockopt(sock_fd, IPPROTO_TCP, TCP_KEEPIDLE, &optval, sizeof(optval)) < 0) 
		Console_Error("setsockopt TCP_KEEPIDLE failed\\r\\n");
		goto iExit;
	

	/* Send next probes after the specified interval. Note that we set the
	 * delay as interval / 3, as we send three probes before detecting
	 * an error (see the next setsockopt call). */
	optval = 60;
	if (setsockopt(sock_fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval, sizeof(optval)) < 0) 
		Console_Error("setsockopt TCP_KEEPINTVL failed\\r\\n");
		goto iExit;
	

	/* Consider the socket in error state after three we send three ACK
	 * probes without getting a reply. */
	optval = 3;
	if (setsockopt(sock_fd, IPPROTO_TCP, TCP_KEEPCNT, &optval, sizeof(optval)) < 0) 
		Console_Error("setsockopt TCP_KEEPCNT failed\\r\\n");
		goto iExit;
	

    memcpy(serverIP, "192.168.0.163", strlen("192.168.0.163"));
    targetPort  = 12584;

	//获取本地ip相关信息
	ql_get_data_call_info(HT_PROFILE_IDX, HT_IP_PROTOCOL, &info);
	local_ipv4.sin_family = AF_INET;
	local_ipv4.sin_port = htons(ql_soc_generate_port());
	local_ipv4.sin_addr = info.v4.addr.ip;

	//绑定端口和ip
	ret = bind(sock_fd,(struct sockaddr *)&local_ipv4,sizeof(struct sockaddr));
	if(ret<0)
	
		Console_Error("bind cid[%d] failed(ip %s)\\n", HT_PROFILE_IDX,inet_ntoa(local_ipv4.sin_addr));
		goto iExit;
	

	//复制信息以
	server_ipv4.sin_family 		= AF_INET;
	server_ipv4.sin_addr.s_addr	= inet_addr(serverIP);
	server_ipv4.sin_port 		= htons(targetPort);
	
	//连接服务器
	ret = connect(sock_fd, (struct sockaddr *)&server_ipv4, sizeof(server_ipv4));
	if(ret < 0)
	
		inet_ntop(AF_INET,&server_ipv4.sin_addr, serverIP, 16);
		Console_Error("connect %s:%d failed\\r\\n",serverIP,server_ipv4.sin_port);
		goto iExit;
	

	Console_Debug("connect %s success!\\r\\n",serverIP);

	//释放资源
	if(res!=NULL)
		freeaddrinfo(res);
	
	return 0;
iExit:
	if(sock_fd>0)
	
		gsock_fd = 0;
		close(sock_fd);
	

	//释放资源
	if(res!=NULL)
		freeaddrinfo(res);

	return -1;

select例程。


int ht_socketSend(uint8_t *pBuffer, uint16_t nlength, int timeoutMs)

	int ret 	= 0;
	int sock_fd = -1;
	struct timeval t;
	fd_set write_fds;

	sock_fd = gsock_fd;	
	
	if(sock_fd <= 0)
	
		Console_Error("socket %d error\\r\\n",sock_fd);
		return -1;
	
	
	t.tv_sec	= 1;
	t.tv_usec	= timeoutMs*1000;

	FD_ZERO(&write_fds);
	FD_SET(sock_fd, &write_fds);

	ret = select(sock_fd + 1, NULL, &write_fds, NULL, &t);
	if(ret < 0)
	
		Console_Error("select failed!,ret:%d\\r\\n",ret);
		return -1;
	
	
	if(FD_ISSET(sock_fd, &write_fds))
	
		ret = send(sock_fd, pBuffer, nlength, 0);
		if(ret > 0)
		
			return ret;
		
		else
		
			return -1;
		
	

	return -1;


int ht_socketRcv(uint8_t *pBuffer, uint16_t nlength, int timeoutMs)
	
	int ret 		= 0;
	int sock_fd 	= -1;
	int readSize 	= 0;
	struct timeval t;
	fd_set read_fds;

	sock_fd = gsock_fd;	

	if(sock_fd <=0)
	
		Console_Error("socket %d error\\r\\n",sock_fd);
		return -1;
	

	do
	
		t.tv_sec	= 0;
		t.tv_usec	= 1000; //ms阻塞
		FD_ZERO(&read_fds);
		FD_SET(sock_fd, &read_fds);

		ret = select(sock_fd + 1, &read_fds, NULL, NULL, &t);
		if(ret < 0)
		
			Console_Error("select failed!,ret:%d\\r\\n",ret);
			return -1;
		
		
		if(FD_ISSET(sock_fd, &read_fds))
		
			ret = recv(sock_fd, (uint8_t*)(pBuffer+readSize), nlength , 0);
			if(ret > 0)
			
				readSize += ret;
				//数据读完了
				if(readSize == nlength)
				
					break;
				
			
			else if(ret == 0)
			
				Console_Error("rcv occur error\\r\\n");
				break;
			
		

		if(timeoutMs >=1 )
			timeoutMs--;

	while(timeoutMs>0);
	
	return readSize;

以上是关于59 Linux socket的select编程的主要内容,如果未能解决你的问题,请参考以下文章

linux socket编程、server端有几个socket绑定不同端口、想用select函数实

Socket编程实践 --Select-I/O复用

linux基础编程:IO模型:阻塞/非阻塞/IO复用 同步/异步 Select/Epoll/AIO(转载)

socket编程的select poll和epoll这两种机制,本质区别在哪里?

linux网路编程--网络超时检测

socket编程:多路复用之select模型