Note that there are some explanatory texts on larger screens.

plurals
  1. POgetaddrinfo Removes last character
    text
    copied!<pre><code>int print_socket_info(int sock_fd, struct sockaddr_in *sin, short protocol){ char dbg[INET_ADDRSTRLEN]; char *famstr; inet_ntop(protocol, &amp;(sin-&gt;sin_addr), dbg, INET_ADDRSTRLEN); printf("============ SOCKET INFORMATION =============\n"); printf("!** socket: %d\n", sock_fd); printf("!** info-&gt;ai_addr: sockaddr_in(\n"); famstr = fam2str(sin-&gt;sin_family); printf("!** sin_family: %s\n", famstr); printf("!** sin_port: %d\n", ntohs(sin-&gt;sin_port)); printf("!** sin_addr: in_addr( s_addr : '%s' )\n", dbg); printf("!**)\n"); printf("=============================================\n"); return 1; } char *fam2str(int fam){ switch (fam){ case AF_INET: return "AF_INET"; case AF_INET6: return "AF_INET6"; case AF_UNSPEC: return "AF_UNSPEC"; default: return "(UNKNOWN)"; } return "(UNKNOWN)"; } </code></pre> <p>If I pass in hint.ai_addr (ignore the info->...that's part of a string) like so:</p> <pre><code>print_socket_info(sock, (struct sockaddr_in *)hint.ai_addr, protocol); </code></pre> <p>...then I get the following printed out...</p> <pre><code>============ SOCKET INFORMATION ============= !** socket: 3 !** info-&gt;ai_addr: sockaddr_in( !** sin_family: AF_INET6 !** sin_port: 8081 !** sin_addr: in_addr( s_addr : '::1' ) !**) ============================================= </code></pre> <p>... information is printed out correctly. Next I call the function:</p> <pre><code>res = getaddrinfo(target_host, target_port, &amp;hint, &amp;info); </code></pre> <p>I get no error so far. Now, I loop through the linked list:</p> <pre><code>struct addrinfo *rp; for (rp = info; rp != NULL; rp = rp-&gt;ai_next){ printf("==&gt; Another element.\n"); print_socket_info(sock, (struct sockaddr_in *) rp-&gt;ai_addr, protocol); } </code></pre> <p>... I get just one element printed out:</p> <pre><code>============ SOCKET INFORMATION ============= !** socket: 3 !** info-&gt;ai_addr: sockaddr_in( !** sin_family: AF_INET6 !** sin_port: 8081 !** sin_addr: in_addr( s_addr : '::' ) !**) ============================================= </code></pre> <p>...which is wreaking havoc with bind(), of course. Why was the address shortened?</p> <p>Something else that's odd: If I pass in 127.0.0.1 and use AF_INET4 then the address is maintained throughout the entire program (I only get one result and bind still fails, though).</p> <p>Any ideas? Thanks in advance.</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