gpt4 book ai didi

c - errno 值未更新

转载 作者:行者123 更新时间:2023-12-02 05:24:47 24 4
gpt4 key购买 nike

如果在连续调用套接字函数期间未更新 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(" ");

最佳答案

errno 永远不会被标准库函数清除,只会被设置。此外,它可以被任何没有以其他方式记录的标准库函数虚假地设置为禁止该行为。

本质上,使用 errno 的唯一正确方法是仅在标准库函数返回指示错误的值 后立即检查它,并将其保存在另一个变量中如果您以后需要保留它。

对于 select 和大多数系统调用,指示错误的返回值为 -1,这意味着您应该只检查返回 -1 的 errno

关于c - errno 值未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10211248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com