关于linux中select对于网络关闭不返回的处理

在mosquitto中有:

int sock;
int sockpairR;
int sockpairW;

	int sv[2];

	if(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1){
		return MOSQ_ERR_ERRNO;
	}
	if(_mosquitto_socket_nonblock(sv[0])){
		COMPAT_CLOSE(sv[0]);
		COMPAT_CLOSE(sv[1]);
		return MOSQ_ERR_ERRNO;
	}
	if(_mosquitto_socket_nonblock(sv[1])){
		COMPAT_CLOSE(sv[0]);
		COMPAT_CLOSE(sv[1]);
		return MOSQ_ERR_ERRNO;
	}
	*pairR = sv[0];
	*pairW = sv[1];

其中: sockpairR=sv[0]; sockpairW=sv[1];

当需要发送数据时调用: write(mosq->sockpairW, &sockpair_data, 1)

这样在: select -> (sock, sockpairR) 时就会返回


FD_SET(mosq->sock, &readfds);
FD_SET(mosq->sockpairR, &readfds);
if(mosq->out_packet)
FD_SET(mosq->sock, &writefds);

 pselect(maxfd+1, &readfds, &writefds, NULL, &local_timeout, NULL);

if( FD_ISSET(mosq->sockpairR, &readfds) ){
  read(sockpairR....);
  FD_SET(mosq->sock, &writefds);
}
if(FD_ISSET(mosq->sock, &writefds)){
   write...>> sock
}

其二:只能用超时时间判断了

你可能感兴趣的:(linux,select,网络,socket,linux/unix,( ̄﹁ ̄))