TCP连接错误115正在进行操作原因是什么?

use*_*512 15 c networking network-programming tcp errno

我的应用程序创建了一个TCP连接,这是正常的工作.但是在一个网络服务器上有很多IP说

  • 174.XXX
  • 54.xxx喜欢这个

当调用TCP连接(非阻塞,超时为60秒)到IP 174.X.X.X 总是成功的.但TCP连接到同一服务器的ip 54.x.x.x 失败(大多数情况下)正在进行errno 115 measn操作.

你能否解释一下errno 115的可能原因是什么?

操作系统:Linux

我的TCP连接代码如下

tcp_connect(......)
{

  int iValOpt = 0;  
  int iLength= 0;

  fcnt((int)(long)SockID,F_SETFL_O_NONBLOCK);

  ret = connect (sockID,(struct sockaddr*)pstSockAdr,uiSockLen);

  if (ret < 0)
  {

        if (errno == EINPROGRESS)
        {
                stTv.tv_sec = 60;
                stTv.tv_usec = 0;
                FD_ZERO(&write_fd);
                FD_SET(sockID,&write_fd);

                iLength = sizeof(int);

                if (0 < select (sockID+1) , NULL,&write_fd,NULL,&stTv);

                {
                        if(0 > getsockopt(sockID,SOL_SOCKET,SO_ERROR,(void*)(&iValOpt),&iLength))
                        {
                                return -1
                        }

                        if (0 != iValOpt)
                        {
                                return -1;
                        }


                        return success;
                }

                else
                {
                        return -1;
                }   

        }
        else
        {
                return -1;
        }
    }

   return success;

}
Run Code Online (Sandbox Code Playgroud)

gsb*_*bil 22

根据您的信息:

  • 您正在尝试做一个connect()54.x.x.x
  • 插座是 non-blocking
  • 连接超时是 60 sec

首先,如果你看一下/usr/include/asm-generic/errno.h你会看到以下内容:

#define EINPROGRESS     115     /* Operation now in progress */
Run Code Online (Sandbox Code Playgroud)

这意味着套接字上的现有操作正在进行中.既然,你说你正在connect()打电话,让我们做一个man connect:

EINPROGRESS

The socket is nonblocking and the connection cannot be completed 
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() completed successfully
(SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual
error codes listed here, explaining the reason for the failure).

因此,最好的猜测是TCP 3次握手(您connect()54.x.x.xIP地址的调用)花费的时间比预期的要长.由于connect()操作已在进行中,因此套接字上的任何后续操作都会导致EINPROGRESS错误代码.如手册页中所述,尝试使用select()poll()检查您的套接字是否可以使用(执行read()write()调用).

您可以通过捕获和分析进出自己机器的流量来确定阻止TCP握手完成的内容54.x.x.x.帮助您解决此问题的最佳工具称为WireShark.祝好运.

TCP 3路握手


And*_*ahl 6

这似乎是connect()的行为:

如果不能立即建立连接,并且为套接字的文件描述符设置了O_NONBLOCK,则connect()将会失败并将errno设置为[EINPROGRESS],但是连接请求不得中止,并且连接应异步建立。在建立连接之前,对同一套接字的connect()的后续调用将失败,并将errno设置为[EALREADY]。