1

这是我的问题:我正在编写一个简单的客户端,它将使用服务器的主机名和端口号作为命令行参数连接到服务器。它编译得很好,但是当我运行客户端时,我从 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));
}
4

1 回答 1

3

&前面有一个 spurious 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()调用。

于 2013-01-23T18:38:33.563 回答