title: Socket Programming using IPv6 where client and server program resides on different machines
body: <p>I am trying to create a client/server program(both programs lies on different machine and not on localhost) in C but the protocol used is IPv6 only. when i run the client,it pauses for sometime at the connect() and then fails.Why connect() is failing?</p>
  
  <p>Server Code:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;sys/socket.h&gt;
  #include&lt;netinet/in.h&gt;
  #include&lt;arpa/inet.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;netdb.h&gt;
  int main()
  {
   int sockfd,connfd,rv;
   struct addrinfo hints,*servinfo,*p;
   struct sockaddr_in6 client_addr;
  
   memset(&amp;hints,0,sizeof hints);
  
   hints.ai_family=AF_INET6;
   hints.ai_socktype=SOCK_STREAM;
   hints.ai_flags=AI_PASSIVE;
  
  
   if((rv=getaddrinfo(NULL,"8888",&amp;hints,&amp;servinfo))!=0)
   {      printf("\n Error 1 \n"); return 0; }
  
  
  for(p=servinfo;p!=NULL;p=p-&gt;ai_next)
  {
  
   if((sockfd=socket(servinfo-&gt;ai_family,servinfo-&gt;ai_socktype,0))==-1)
   {      perror("socket"); continue;}
  
   if(bind(sockfd,servinfo-&gt;ai_addr,servinfo-&gt;ai_addrlen)==-1)
   {      close(sockfd); perror("bind"); continue;}
  
    break;
  }
   if(p==NULL)
  {
   fprintf(stderr,"failed to bind");
   return 0;
  }
  
    listen(sockfd,8);
  
    printf("\n\n Waiting for connection....\n");
  
    socklen_t size=sizeof(client_addr);
  
    if((connfd=accept(sockfd,(struct sockaddr *)&amp;client_addr,&amp;size))&lt;0)
    {     printf("\n Error 4 \n"); return 0; }
    else
    {
         char ch[50];
         inet_ntop(AF_INET6,&amp;(client_addr.sin6_addr),ch,50);
         printf("\n Connected to %s \n",ch);
    }
  
    close(sockfd);
    close(connfd);
  
    return 0;
  }
  </code></pre>
  
  <p>Client Code:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;sys/socket.h&gt;
  #include&lt;netinet/in.h&gt;
  #include&lt;arpa/inet.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;netdb.h&gt;
  int main()
  {
   int i,s;
  
   struct addrinfo hints,*res,*p;
  
   memset(&amp;hints,0,sizeof (hints));
  
   hints.ai_family=AF_INET6;
   hints.ai_socktype=SOCK_STREAM;
  
   i=getaddrinfo("fe80::20c:29ff:fe60:7593%eth0","8888",&amp;hints,&amp;res);//because the system in which server code is has IPv6 address fe80::20c:29ff:fe60:7593
  
   if(i!=0)
    { printf("\n Fail 1 \n"); return 0;}
  
   for(p=res;p!=NULL;p=p-&gt;ai_next)
   { 
     if((s=socket(res-&gt;ai_family,res-&gt;ai_socktype,0))==-1)
      {perror("socket"); continue;}
  
     if(connect(s,p-&gt;ai_addr,p-&gt;ai_addrlen)==-1)
      { close(s); perror("connect"); continue;}
  
     break;
   }
  
   if(p==NULL)
   {
    fprintf(stderr,"failed to connect\n");
     return 0;
    }
  
   close(s);
   return 0;
  }
  </code></pre>