getaddrinfo() 上的段错误

发布于 2024-11-24 19:09:31 字数 1717 浏览 13 评论 0原文

我在 getaddrinfo 调用中遇到分段错误,但无法找出原因。它发生在我的服务器和客户端上。一些代码(服务器端)位于

class TcpServer {
public:

    TcpServer(int);
    ~TcpServer();

    void launchServer();

    void communicate();

private:
    const char* port;
    int fd;
    int comm_fd;
};

tcpserver.cpp

void TcpServer::launchServer() {
    int status;

    struct addrinfo hints;
    struct addrinfo *servinfo;  //will point to the results

    //store the connecting address and size
    struct sockaddr_storage their_addr;
    socklen_t their_addr_size;

    //socket infoS
    memset(&hints, 0, sizeof hints); //make sure the struct is empty
    hints.ai_family = AF_INET;  //local address
    hints.ai_socktype = SOCK_STREAM; //tcp
    hints.ai_flags = AI_PASSIVE;     //use local-host address

    //get server info, put into servinfo
    if ((status = getaddrinfo("127.0.0.1", port, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
        exit(1);
    }

中的 main 中

TcpServer server(4950);
server.launchServer();

- 传递给构造函数的 int 被转换为端口的 const char*。< /strong>

当我运行 gdb 时,它给我一个回溯 -

#0  0xb7dca737 in getaddrinfo (name=0x8054824 "127.0.0.1", 
    service=0x1356 <Address 0x1356 out of bounds>, hints=0xbffff20c, 
    pai=0xbffff234) at ../sysdeps/posix/getaddrinfo.c:2080
#1  0x08050f79 in TcpServer::launchServer (this=0xbffff304) at tcpserver.cpp:25
#2  0x0804eae9 in main (argc=1, args=0xbffff3f4) at mainserver.cpp:47

所以“地址 0x1356 越界”让我相信端口出了问题,但我不知道可能出了什么问题。如果有人能指出错误的地方,我将不胜感激。感谢您的任何帮助。

I am getting a segmentation fault on my getaddrinfo call and cannot figure out why. It happens on both my server and client. Some code (server side) is -

class TcpServer {
public:

    TcpServer(int);
    ~TcpServer();

    void launchServer();

    void communicate();

private:
    const char* port;
    int fd;
    int comm_fd;
};

in tcpserver.cpp-

void TcpServer::launchServer() {
    int status;

    struct addrinfo hints;
    struct addrinfo *servinfo;  //will point to the results

    //store the connecting address and size
    struct sockaddr_storage their_addr;
    socklen_t their_addr_size;

    //socket infoS
    memset(&hints, 0, sizeof hints); //make sure the struct is empty
    hints.ai_family = AF_INET;  //local address
    hints.ai_socktype = SOCK_STREAM; //tcp
    hints.ai_flags = AI_PASSIVE;     //use local-host address

    //get server info, put into servinfo
    if ((status = getaddrinfo("127.0.0.1", port, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
        exit(1);
    }

in main-

TcpServer server(4950);
server.launchServer();

The int passed to the constructor is casted to a const char* for port.

When I run gdb, it gives me a backtrace of -

#0  0xb7dca737 in getaddrinfo (name=0x8054824 "127.0.0.1", 
    service=0x1356 <Address 0x1356 out of bounds>, hints=0xbffff20c, 
    pai=0xbffff234) at ../sysdeps/posix/getaddrinfo.c:2080
#1  0x08050f79 in TcpServer::launchServer (this=0xbffff304) at tcpserver.cpp:25
#2  0x0804eae9 in main (argc=1, args=0xbffff3f4) at mainserver.cpp:47

So "Address 0x1356 out of bounds" makes me believe something is wrong with port, but I do not know what could be wrong. If anyone can point out something wrong I would be grateful. Thanks for any help.

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

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

发布评论

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

评论(3

忆离笙 2024-12-01 19:09:31
getaddrinfo("127.0.0.1", port, &hints, &servinfo)
                          ^

那应该是一个char *。我猜您正在传递一个整数并强制库访问无效地址。

编辑

根据 Blagovest Buyukliev 的评论,我相信您在构造函数中做了类似的事情:this->port = (const char*) port

您需要使用某些东西(snprintf 也许?)将该整数转换为 char *。仅仅铸造是不行的

getaddrinfo("127.0.0.1", port, &hints, &servinfo)
                          ^

That should be a char *. I am guessing you are passing an integer and forcing the library to access an invalid address.

EDIT

In light of the comment of Blagovest Buyukliev I believe you are doing something like this in the constructor: this->port = (const char*) port.

You need to use something (snprintf maybe ?) to convert that integer to a char *. Simply casting won't do.

最近可好 2024-12-01 19:09:31

您是否应该通过引用而不是值传递port,即&port? 0x1356 与 4950 相同,后者是您尝试使用的端口号。

编辑:好的,我发现它应该是一个字符串而不是一个指向int的指针。我将保留我的答案,因为它显示端口值被错误地解释为地址。

Are you supposed to be passing in port by reference rather than value, i.e. &port? 0x1356 is the same value as 4950 which is the port number you're trying to use.

edit: OK, I see it's supposed to be a string rather than a pointer-to-int. I'll leave my answer in place since it shows the port value is wrongly being interpreted as an address.

不弃不离 2024-12-01 19:09:31

您正在设置一个指向端口的指针,但没有为其分配内存。 (至少我在您列出的代码中看不到这一点。)

端口应该是一个字符串(即字符数组),而不是指向字符的指针。而且也不是整数。

You are setting a pointer to port, but not allocating memory for it. (At least not that I can see in the code you listed.)

Port should be a string (i.e. char array), not a pointer to a char. And not an integer either.

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