我有一个程序接受IPv4和IPv6上的TCP连接。 如果客户端通过IPv4连接,一切正常。但是,如果客户端通过IPv6堆栈进入,则TCP连接未完全建立,但netstat显示侦听套接字处于SYN_RCVD状态。这在AIX 6.1上发生
我需要做什么/配置/忘记了吗? 以下是我的代码处理套接字处理的基本部分:
/*
* If IPv4 and IPv6 are supported simultaneously
* we use the IPv6 socket because it can handle both
*/
for (AI = AddrInfo; AI != NULL; AI = AI->ai_next)
{
/* Only PF_INET and PF_INET6 are supported. */
if ((AI->ai_family != PF_INET) && (AI->ai_family != PF_INET6))
continue;
NumSocks++;
}
if (NumSocks == 0)
{
fprintf(stderr, "Function %s failed. Error: %s\n",
fktName, "unable to serve on any address.\n" );
return -1;
}
if (NumSocks > 1)
{
/* if we have more than 1 IPv4/IPv6-interface find the IPv6 interface */
for (AI = AddrInfo; AI != NULL; AI = AI->ai_next)
{
if (AI->ai_family == PF_INET6) {
break;
}
}
}
if (AI == NULL)
{
/* we only have 1 interface or no IPv6 interface */
AI = AddrInfo;
}
/* Open a socket with the correct address family for this address. */
newsock = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
if (newsock < 0)
{
fprintf(stdout, "Function %s returned with return code = %s\n",
"socket()", strerror(tcperrno()));
return -1;
}
/* Allows the socket to be bound to an address that is already in use */
val = 1;
if (setsockopt(newsock,SOL_SOCKET,SO_REUSEADDR,SPCHAR & val,(s_sock) sizeof(val)))
{
fprintf(stdout, "Function %s returned with return code = %s\n",
"setsockopt(newsock)", strerror(tcperrno()));
}
SOCK_CLOSE(newsock);
return -1;
}
/* Bind the socket to a local address and port */
if (bind(newsock, AI->ai_addr, (int) AI->ai_addrlen) < 0)
{
fprintf(stderr, "Function %s failed. Error: %s\n",
"bind(newsock)", strerror(tcperrno()));
SOCK_CLOSE(newsock);
return -1;
}
/* Listen for incomming connections. */
if (listen(newsock, SOMAXCONN) < 0)
{
fprintf(stderr, "Function %s failed. Error: %s\n",
"listen(newsock)", strerror(tcperrno()));
SOCK_CLOSE(newsock);
return -1;
}
:
/* Accept a connection. */
mysock = accept(newsock, CAST_TCP & client, &namelen);
:
根据truss,我的进程永远不会从accept()返回。