Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want your server to listen on IPV4 and IPV6 addresses you <strike>need to</strike> could set up two socket to listen on.</p> <p><code>getaddrinfo()</code> might return information for more then one internet address for given host and/or service.</p> <p>The member <code>ai_family</code> of the <code>hints</code> structure passed specifies which address family the caller is interest in. If <code>AF_UNSPEC</code> is specified IPV4 and IPV6 addresses might be returned.</p> <p>To find out if there are such addresses available you might like to mod you code like so:</p> <pre><code>int open_listenfd(int port, int * pfdSocketIpV4, int * pfdSocketIpV6) { ... struct addrinfo hints = {0}; hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE|AI_ADDRCONFIG; hints.ai_next = NULL; struct addrinfo * res = NULL; int err=getaddrinfo(hostname, portname, &amp;hints, &amp;res); free(pName); if (err) { return -1; } struct addrinfo * pAddrInfoIpV4 = NULL; struct addrinfo * pAddrInfoIpV6 = NULL; { struct addrinfo * pAddrInfo = res; /* Loop over all address infos found until a IPV4 and a IPV6 address is found. */ while (pAddrInfo) { if (!pAddrInfoIpV4 &amp;&amp; AF_INET == pAddrInfo-&gt;ai_family) { pAddrInfoIpV4 = pAddrInfo; /* Take first IPV4 address available */ } else if (!pAddrInfoIpV6 &amp;&amp; AF_INET6 == pAddrInfo-&gt;ai_family) { pAddrInfoIpV6 = pAddrInfo; /* Take first IPV6 address available */ } else { break; /* Already got an IPV4 and IPV6 address, so skip the rest */ } pAddrInfo= pAddrInfo-&gt;ai_next; /* Get next address info, if any */ } } if (pAddrInfoIpV4) { ... /* create, bind and make IPV4 socket listen */ int fdSocketIpV4 = socket(pAddrInfoIpV4-&gt;ai_family,... *pfdSocketIpV4 = fdSocketIpV4; } if (pAddrInfoIpV6) { /* create, bind and make IPV6 socket listen */ int fdSocketIpV6 = socket(pAddrInfoIpV6-&gt;ai_family,... *pfdSocketIpV6 = fdSocketIpV6; } freeaddrinfo(res); ... </code></pre> <p>Then call it like so:</p> <pre><code>... int fdSocketIpV4 = -1; int fdSocketIpV6 = -1; if (0 &gt; open_listenfd(port, &amp;fdSocketIpV4, &amp;fdSocketIpV6)) { printf("Error executing 'open_listenfd()'\n"); } else { ... /* go for accepting connectings on 'fdSocketIpV4' and 'fdSocketIpV6' */ </code></pre> <hr> <p><strong>Update:</strong></p> <p>As commented by <em>Per Johansson</em> an alternative approach would be to set up a dual stack socket, supporting both, Ipv4 and Ipv6, as mentioned by this answer: <a href="https://stackoverflow.com/q/1618240/694576">How to support both IPv4 and IPv6 connections</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload