运行客户端时“协议不支持的地址”

时间:2013-01-23 17:57:20

标签: c sockets client-server runtime-error

这是我的问题:我正在编写一个简单的客户端,它将使用服务器的主机名和端口号作为命令行参数连接到服务器。它编译得很好但是当我运行客户端时,我从perror()收到一条消息,上面写着“错误:协议不支持地址族”。不知道为什么我收到此错误,但我知道当我调用connect()时会发生错误。由于某种原因,connect无法在我的sockaddr_in结构中找到地址系列信息。任何帮助将非常感激。

int main(int argc, char* argv[])
{
    if(argv[1] == NULL)
    {
        printf("if 1\n");
        perror("Error: argv[1]:hostname\n");
        exit(1);
    }

    struct sockaddr_in* servaddr = getServerInfo(argv[1], argv[2]);
    int socketfd = createSocket();

    if(socketfd < 0)
    {
        printf("if 2\n");
        perror("Error: socketfd\n");
        exit(1);
    }

    int commResult = communicate(socketfd, servaddr);

    if(commResult < 0)
    {
        printf("if 3\n");
        perror("Error: communication\n");
        exit(1);
    }

    return 0;
}

struct sockaddr_in* getServerInfo(char* hostname, char* port)
{
    struct sockaddr_in* servaddr = malloc((size_t)sizeof(struct sockaddr_in));
    struct hostent *hostptr;
    hostptr = gethostbyname(hostname);

    memset((void *) servaddr, 0, (size_t)sizeof(*servaddr));
    servaddr->sin_family = (short)(AF_INET);
    memcpy((void*)& servaddr->sin_addr, (void *) hostptr->h_addr, hostptr->h_length);
    servaddr->sin_port = htons((u_short)atoi(port));

    return servaddr;
}


int createSocket()
{
    int socketfd = socket(AF_INET, SOCK_STREAM, 0);
    if(socketfd < 0)
    {
        return -1;
    }

    return socketfd;
}

int communicate(int socketfd, struct sockaddr_in* servaddr)
{
    char buffer[BUFFERSIZE];

    // connect to the server at the location displayed by TCPserver.c
    if(connect(socketfd, (struct sockaddr*)&servaddr, sizeof(*servaddr)) < 0)
    {
        printf("if 4\n");
        perror("ERROR: connecting to socket\n");
        return -1;
    }

    printf("Connected to server\n");

    strcpy(buffer, "This is the message I want the client to echo on the screen to prove that we can  communicate together.\n");

    write(socketfd, buffer, strlen(buffer)+1); 

    return 0;
}

以下是服务器代码:

int main()
{
char * hostname = malloc(MAXSIZE * sizeof(char));
char buffer[BUFFERSIZE];

struct sockaddr_in* servaddr = getServerInfo(hostname);
int listensockfd = createSocket(servaddr), connsockfd;
printServerInfo(hostname, servaddr);

listen(listensockfd, MAX_NUM_LISTENER_ALLOWED);

connsockfd = accept(listensockfd, NULL, NULL);
read(connsockfd, buffer, BUFFERSIZE);
printf("Message: %s\n", buffer);

// free memory
free(servaddr);
free(hostname);

return 0;
} // End main

struct sockaddr_in* getServerInfo(char* hostname)
{
    struct sockaddr_in* servaddr = malloc((size_t)sizeof(struct sockaddr_in));
    gethostname(hostname, 32);
    struct hostent *hostptr;
    hostptr = gethostbyname(hostname);

        memset((void *) servaddr, 0, (size_t)sizeof(*servaddr));
        servaddr->sin_family = (short)(AF_INET);
        memcpy((void *)& servaddr->sin_addr, (void *) hostptr->h_addr, hostptr->h_length);
        servaddr->sin_port = htons((u_short)38000);

    return servaddr;
}

int createSocket(struct sockaddr_in* servaddr)
{
    int listensockfd = socket(AF_INET, SOCK_STREAM, 0);

    bind(listensockfd, (struct sockaddr *) &servaddr, (socklen_t)sizeof(servaddr));

    socklen_t listensocklen = sizeof(servaddr);
    getsockname(listensockfd, (struct sockaddr*)&servaddr, &listensocklen);

    return listensockfd;
}

void printServerInfo(char* hostname, struct sockaddr_in* servaddr)
{
    struct hostent *hostptr;
    hostptr = gethostbyname(hostname);

    printf("Host Name: %s\n", hostname);
    printf("Host IP: %s\n", inet_ntoa(*(struct in_addr*)*hostptr->h_addr_list));
    printf("Port Number: %d\n", htons(servaddr->sin_port));
}

1 个答案:

答案 0 :(得分:3)

&前面有一个虚假的servaddr,已经 指针。您实际上是将堆栈上指针的地址传递给connect(),而不是实际指向struct sockaddr_in的指针。

替换它:

    if(connect(socketfd, (struct sockaddr*)&servaddr, sizeof(*servaddr)) < 0)

用这个:

    if(connect(socketfd, (struct sockaddr*)servaddr, sizeof(*servaddr)) < 0)

它会起作用。

在服务器中,bind()中对getsockname()createSocket()的调用存在同样的错误。此外,您在同一个函数的两个位置使用sizeof(servaddr)(指针大小)而不是sizeof(*servaddr)

请注意,我强烈建议您使用现代getaddrinfo() API而不是过时的gethostbyname()来电。