如果在连续调用套接字函数期间未更新 errno 值,问题可能会发生什么?
socket (AF_INET, -1, 0);
socket (AF_INET, SOCK_STREAM, -1);
第一个应该有 errno = EINVAL 第二个应该有 errno = EPROTONOSUPPORT
从@JonathanLeffler 下面提供的代码中,cygwin 的输出如下:
*$ ./socket.exe
Error from socket(AF_INET, -1, 0): 124 (Socket type not supported)
-- errno = 124 (Socket type not supported)
Error from socket(AF_INET, SOCK_STREAM, -1): 123 (Protocol not supported)
-- errno = 123 (Protocol not supported)
socket(AF_INET, SOCK_STREAM, 0) succeeded (fd = 3)
-- errno = 123 (Protocol not supported)*
在创建套接字之前,编辑了下面的上述代码以创建套接字 fd0
int fd0 = socket(AF_INET, SOCK_STREAM, 0);
if (fd0 < 0)
printf("Error from socket(AF_INET, SOCK_STREAM, 0): %d (%s)\n", errno, strerror(errno));
else
{
printf("socket(AF_INET, SOCK_STREAM, 0) succeeded (fd = %d)\n", fd0);
close(fd0);
}
printf("-- errno = %d (%s)\n", errno, strerror(errno));
int fd1 = socket(AF_INET, -1, 0);
.....
结果如下:
*$ ./socket.exe
socket(AF_INET, SOCK_STREAM, 0) succeeded (fd = 3)
-- errno = 0 (No error)
Error from socket(AF_INET, -1, 0): 124 (Socket type not supported)
-- errno = 124 (Socket type not supported)
Error from socket(AF_INET, SOCK_STREAM, -1): 123 (Protocol not supported)
-- errno = 123 (Protocol not supported)
socket(AF_INET, SOCK_STREAM, 0) succeeded (fd = 3)
-- errno = 123 (Protocol not supported)*
预计第一个和最后一个套接字创建应该具有相同的 errno 值。
但是如何解释这个输出呢?第 5 和第 6 个套接字创建具有相同的 errno 值,但错误原因不同。
$ ./test_select.exe
[1] ierr = 124, iSocket = -1 socket(AF_INET, -1, 0);
: Socket type not supported
[2] ierr = 124, iSocket = -1 socket(AF_INET, -1, 0);
: Socket type not supported
[3] ierr = 124, iSocket = 3 socket(AF_INET, SOCK_STREAM, 0);
: Socket type not supported
[4] ierr = 124, iSocket = -1 socket (AF_INET, -1, 0);
: Socket type not supported
[5] ierr = 123, iSocket = -1 socket (AF_INET, SOCK_STREAM, -1);
: Protocol not supported
[6] ierr = 123, iSocket = -1 socket (AF_INET, -1, 0)
: Protocol not supported
[7] ierr = 124, iSocket = -1 socket(AF_INET, SOCK_STREAM, -1)
: Protocol not supported
实际代码如下:
sockfd = socket(AF_INET, -1, 0);
err = errno;
printf("[1] err = %d, sockfd = %d socket(AF_INET, -1, 0);\n", err, sockfd);
perror(" ");
sockfd = socket(AF_INET, -1, 0);
err = errno;
printf("[2] err = %d, sockfd = %d socket(AF_INET, -1, 0);\n", err, sockfd);
perror(" ");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
err = errno;
printf("[3] err = %d, sockfd = %d socket(AF_INET, SOCK_STREAM, 0);\n", err, sockfd);
perror(" ");
close(sockfd);
sockfd = socket(AF_INET, -1, 0);
err = errno;
printf("[4] err = %d, sockfd = %d socket (AF_INET, -1, 0);\n", err, sockfd);
perror(" ");
sockfd = socket(AF_INET, SOCK_STREAM, -1);
err = errno;
printf("[5] err = %d, sockfd = %d socket (AF_INET, SOCK_STREAM, -1);\n", err, sockfd);
perror(" ");
sockfd = socket(AF_INET, -1, 0);
err = errno;
printf("[6] err = %d, sockfd = %d socket (AF_INET, -1, 0)\n", err, sockfd);
perror(" ");
err = errno;
sockfd = socket(AF_INET, SOCK_STREAM, -1);
printf("[7] err = %d, sockfd = %d socket(AF_INET, SOCK_STREAM, -1)\n", err, sockfd);
perror(" ");