linux 系统调用 getpeername c++

发布于 2024-10-07 18:02:06 字数 480 浏览 0 评论 0原文

在我的 C++ 应用程序中,我使用 getpeername 系统调用,它在 var sa 中返回 0.0.0.0。 errno 中没有错误,返回代码为 0。

代码如下:

int GetSock(int sock)
{
    struct sockaddr_storage ss;
    socklen_t salen = sizeof(ss);
    struct sockaddr *sa;
    struct addrinfo hints, *paddr, *paddrp;

    sa = (struct sockaddr *)&ss;

    if (getpeername(sock, sa, &salen) != 0) {
        error = errno;
        return -1;
    }
}

注意:我在 eclipse 中使用 GCC 编译此代码。 有什么帮助吗?

谢谢!

In my c++ application I'm using getpeername system call and it return 0.0.0.0 in the var sa.
there is no error in errno, and the return code is 0.

here is the code:

int GetSock(int sock)
{
    struct sockaddr_storage ss;
    socklen_t salen = sizeof(ss);
    struct sockaddr *sa;
    struct addrinfo hints, *paddr, *paddrp;

    sa = (struct sockaddr *)&ss;

    if (getpeername(sock, sa, &salen) != 0) {
        error = errno;
        return -1;
    }
}

note: I'm compiling this code with GCC in eclipse.
any help?

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乄_柒ぐ汐 2024-10-14 18:02:06
        error = errno;
        return -1;
    }
    /* Did you mean to return something right here? */
}
        error = errno;
        return -1;
    }
    /* Did you mean to return something right here? */
}
只为一人 2024-10-14 18:02:06

成功完成后,将返回 0。否则,应返回 -1 并设置 errno 以指示错误。

编辑:检查 sock 的值是什么。 getpeername 仅提取并存储套接字(在本例中为 sock)的对等地址,并将其存储在 sa 中。如果您的套接字未创建或您的套接字未绑定到命名套接字,则 sa 指向的内容未指定,这可能是您的情况。

Upon successful completion, 0 shall be returned. Otherwise, -1 shall be returned and errno set to indicate the error.

EDIT: Check what is the value of sock is. getpeername only extracts and stores the peer address of the socket, in this case sock, and stores it in sa. If your socket isn't created or your socket isn't bound to a named socket, what sa points to is unspecified and this may be your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文