开发者

Getting ai_socktype not supported

I had this working previously, but I didn't understand how, so I'm attempting to rewrite a bit of code.

Everything seems to be going fine, except when I try to use getaddrinfo(), I'm getting back a result of -7, which translates into the error "ai_socktype not supported".

I'm willing to bet it's simply me still getting a handle on pointers. If someone can explain how/where I'm going wrong, I would appreciate it.

The code:

#define PORT "4400"
typedef struct {
    int port;
    fd_set *connections;
    int connections_count;
    int listener;
    struct addrinfo *address;
    struct addrinfo *socket_hints;
} Server;

void initialize_server(Server *passed_server, char *port) {
    struct addrinfo *temp;
    int status; // Get addrinfo status

    // Set up the server hints
    passed_server->socket_hints = malloc(sizeof passed_server->socket_hints);
    passed_server->socket_hints->ai_family = AF_UNSPEC;
    passed_server->socket_hints->ai_socktype = SOCK_STREAM;
    passed_server->socket_hints->ai_flags = AI_PASSIVE;

    if((status = getaddrinfo(NULL, port, passed_server->socket_hints, &temp)) != 0) {
        fprintf(stderr, "Error with getaddrinfo: %s\n", gai_strerror(status));
    }
    printf("Result: %d\n", status);
}

int main(int argc, char** argv) {
    // Set up socket stuff
开发者_运维百科    Server *server = malloc(sizeof *server); // Set up the server
    initialize_server(server, PORT);

    return (EXIT_SUCCESS);
}


After going back through the code, I realized I needed to change:

passed_server->socket_hints = malloc(sizeof passed_server->socket_hints);

to

passed_server->socket_hints = malloc(sizeof *passed_server->socket_hints);

I believe it's because in the first one I'm getting the size of a memory location (type int). In the second one, I'm returning the sizeof the struct to which socket_hints is pointing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜