user50891
user50891

Reputation: 21

client address is null on the server side

I am trying a simple client-server program. On the client side I bind to a specific address. On the server side when I accept a connection, the client address is received as NULL. I am using getaddrinfo on the client and server side to fetch the IP address of the server.

The following is a snippet of the client code:

s = getaddrinfo(argv[1], argv[2], &hints, &result);
if (s != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
    exit(EXIT_FAILURE);
}

int sockfd =0;
struct sockaddr_in localaddr;
localaddr.sin_family = AF_INET;
localaddr.sin_addr.s_addr = inet_addr("<local_ip_address>");
localaddr.sin_port = 0;  // Any local port will do

for (rp = result; rp != NULL; rp = rp->ai_next) {
     sfd = socket(rp->ai_family, rp->ai_socktype,
                  rp->ai_protocol);

if (bind(sfd, (struct sockaddr *)&localaddr, sizeof(localaddr)) !=0)
  perror("failed to bind");

if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
{
        printf("connect success %d \n",  rp->ai_family);
        break;                  /* Success */
}

   close(sfd);
}

And the following is on the server side:

memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* Datagram socket */
hints.ai_flags = AI_PASSIVE ;    /* For wildcard IP address */
hints.ai_protocol = IPPROTO_TCP;          /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;

s = getaddrinfo(NULL, argv[1], &hints, &result);
if (s != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
    exit(EXIT_FAILURE);
}
for (rp = result; rp != NULL; rp = rp->ai_next) {

    sfd = socket(rp->ai_family, rp->ai_socktype,
            rp->ai_protocol);
    if (sfd == -1)
        continue;

   if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
   {
      printf("bind success\n");

        break;                  /* Success */
    }
 }

listen(sfd, 10);


struct sockaddr clientaddr;
socklen_t addrlen=0;
int port;
memset(&clientaddr, 0, sizeof(clientaddr));
int        connfd = accept(sfd,
                    &clientaddr,
                    &addrlen);

 s=getnameinfo((struct sockaddr *)&clientaddr, addrlen,
        clienthost, sizeof(clienthost),
        clientservice, sizeof(clientservice),
        NI_NUMERICHOST);
   if (s == 0)
    printf("Received request from host=[%s] port=[%d]\n",
           clienthost, clientservice);
    else
        fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));

When I execute the program I see an error returned by the getnameinfo as unknown family. I checked clientaddr using gdb. It shows family as 0. All the data in clientaddr is null.

Upvotes: 0

Views: 673

Answers (1)

Rajath N R
Rajath N R

Reputation: 41

You're passing addrlen zero to accept call. You need to assign addrlen to addrlen = sizeof(clientaddr)

struct sockaddr clientaddr;
socklen_t addrlen=0;
int port;
memset(&clientaddr, 0, sizeof(clientaddr));
addrlen = sizeof(clientaddr);
int        connfd = accept(sfd,
                    &clientaddr,
                    &addrlen);      

Upvotes: 2

Related Questions