title: Binding Sockets to IPv6 Addresses
body: <p>I am trying to write a web server that listens on both IPv4 and IPv6 addresses.  However, the code that I originally wrote did not work.  Then I found out that the IPv6 structures work for both IPv4 and IPv6.  So now I use the IPv6 structures however, only the IPv4 addresses work.  This post, <a href="https://stackoverflow.com/questions/2455762/why-cant-i-bind-ipv6-socket-to-a-linklocal-address">why can&#39;t i bind ipv6 socket to a linklocal address</a>, which said to add <code>server.sin6_scope_id = 5;</code> so I did that but it still does not accept IPv6 telnet connections.  Any help would be greatly appreciated because I am thoroughly stumped.<br>
  Thanks!</p>
  
  <p>My code is below:</p>
  
  <pre><code>void initialize_server(int port, int connections, char* address)
  {
          struct sockaddr_in6 socket_struct;
          /*Creates the socket*/
          if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
          {
                  syslog(LOG_ERR, "%s\n", strerror(errno));
                  exit(EXIT_FAILURE);
          }/*Ends the socket creation*/
  
          /*Populates the socket address structure*/
                  socket_struct.sin6_family = AF_INET6;
  
          if(address == NULL)
                  socket_struct.sin6_addr=in6addr_any;
          else
          {
                  inet_pton(AF_INET6, "fe80::216:3eff:fec3:3c22", (void *)&amp;socket_struct.sin6_addr.s6_addr);
          }
          socket_struct.sin6_port =htons(port);
          socket_struct.sin6_scope_id = 0;
          if (bind(sock_fd, (struct sockaddr*) &amp;socket_struct, sizeof(socket_struct)) &lt; 0)
          {
                  syslog(LOG_ERR, "%s\n", strerror(errno));
                  exit(EXIT_FAILURE);
          }//Ends the binding.
  
          if (listen(sock_fd, connections) &lt;0)
          {
                  syslog(LOG_ERR, "%s\n", strerror(errno));
                  exit(EXIT_FAILURE);
          }//Ends the listening function
  
  }//ends the initialize server function.
  </code></pre>