user1995624
user1995624

Reputation: 31

"address not supported by protocol" when running a client

Here's my issue: I am writing a simple client that will connect to a server using the server's hostname and port number as commandline arguments. It compiles fine but when I run the client, I get a message from perror() that says "Error: address family not supported by protocol." Not sure why I am getting this error but I know that the error occurs when I call connect(). For some reason, connect is unable to find the address family information in my sockaddr_in struct. Any help would be much appreciated.

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;
}

Here is the server code as well:

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));
}

Upvotes: 1

Views: 968

Answers (1)

Daniel Roethlisberger
Daniel Roethlisberger

Reputation: 7065

You have a spurious & in front of servaddr, which already is a pointer. You are actually passing the address of the pointer on the stack to connect(), instead of the actual pointer to the struct sockaddr_in.

Replace this:

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

with this:

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

and it will work.

In the server, there is the same mistake in the calls to bind() and getsockname() in createSocket(). Furthermore, you use sizeof(servaddr) (which is the size of a pointer) instead of sizeof(*servaddr) in two places in the same function.

Note that I'd strongly recommend to use the modern getaddrinfo() API instead of the obsolete gethostbyname() call.

Upvotes: 3

Related Questions