operation now in process 问题;

在非阻塞模式下。连接服务器,有时候会出现服务端提示此错误;

 

解决办法:

可以把 fcntl(tcpsock,F_SETFL,O_NONBLOCK); 放到connect之后就好了!
个人觉得在connect之前就设nonblock不好。

 

此问题的官方解释:

 

man connect:

EINPROGRESS
The socket is non-blocking and the connection cannot be com-
pleted immediately. It is possible to select(2) or poll(2) for
completion by selecting the socket for writing. After select(2)
indicates writability, use getsockopt(2) to read the SO_ERROR
option at level SOL_SOCKET to determine whether connect() com-
pleted successfully (SO_ERROR is zero) or unsuccessfully

(SO_ERROR is one of the usual error codes listed here, explain-
ing the reason for the failure).

 

int
connect_nonb(
int sockfd, const SA *saptr, socklen_t salen, int nsec)
{
   
int     flags, n, error;
    socklen_t len;
    fd_set rset, wset;
   
struct timeval tval;

    flags
= Fcntl(sockfd, F_GETFL, 0);
    Fcntl(sockfd, F_SETFL, flags
| O_NONBLOCK);

    error
= 0;
   
if ( (n = connect(sockfd, saptr, salen)) < 0)
       
if (errno != EINPROGRESS)
           
return (-1);

   
/* Do whatever we want while the connect is taking place. */

   
if (n == 0)
       
goto done;               /* connect completed immediately */

    FD_ZERO(
&rset);
    FD_SET(sockfd,
&rset);
    wset
= rset;
    tval.tv_sec
= nsec;
    tval.tv_usec
= 0;

   
if ( (n = Select(sockfd + 1, &rset, &wset, NULL,
                    nsec
? &tval : NULL)) == 0) {
        close(sockfd);         
/* timeout */
        errno
= ETIMEDOUT;
       
return (-1);
    }

   
if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
        len
= sizeof(error);
       
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
           
return (-1);     /* Solaris pending error */
    }
else
        err_quit(
"select error: sockfd not set");

  done:
    Fcntl(sockfd, F_SETFL, flags); 
/* restore file status flags */

   
if (error) {
        close(sockfd);          
/* just in case */
        errno
= error;
       
return (-1);
    }
   
return (0);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值