Różnice między wybraną wersją a wersją aktualną.
Both sides previous revision Poprzednia wersja | |||
sk2:sockets_netdbs [2024/10/18 09:12] jkonczak [getaddrinfo] |
sk2:sockets_netdbs [2025/10/13 12:14] (aktualna) jkonczak [getaddrinfo] |
||
---|---|---|---|
Linia 179: | Linia 179: | ||
} | } | ||
</code> | </code> | ||
+ | |||
+ | <html> | ||
+ | <script> | ||
+ | function genGaiCall(){ | ||
+ | var hints = ""; | ||
+ | if(document.getElementById("ai_family").value) | ||
+ | hints += "hints.ai_family = " + document.getElementById("ai_family").value + ";\n"; | ||
+ | if(document.getElementById("ai_socktype").value) | ||
+ | hints += "hints.ai_socktype = " + document.getElementById("ai_socktype").value + ";\n"; | ||
+ | if(document.getElementById("ai_protocol").value) | ||
+ | hints += "hints.ai_protocol = " + document.getElementById("ai_protocol").value + ";\n"; | ||
+ | var flags = ""; | ||
+ | if(document.getElementById("AI_PASSIVE").checked) | ||
+ | flags += "AI_PASSIVE | "; | ||
+ | if(!document.getElementById("AI_NUMERICHOST").checked) | ||
+ | flags += "AI_NUMERICHOST | "; | ||
+ | if(!document.getElementById("AI_NUMERICSERV").checked) | ||
+ | flags += "AI_NUMERICSERV | "; | ||
+ | if(flags) | ||
+ | hints += "hints.ai_flags = " + flags.slice(0, -3) + ";\n"; | ||
+ | | ||
+ | var text = ""; | ||
+ | if(hints) | ||
+ | text = "struct addrinfo hints = {};\n" + hints; | ||
+ | text += "struct addrinfo *res;\n"; | ||
+ | text += "int status;\n"; | ||
+ | text += "if (0 != (status = getaddrinfo(address, port, " + (hints?"&hints":"NULL") + ", &res))) {\n"; | ||
+ | text += " fprintf(stderr, \"Query failed: %s\\n\", gai_strerror(status));\n"; | ||
+ | text += " …\n"; | ||
+ | text += "}\n"; | ||
+ | text += "// przykładowe użycie tylko pierwszego ze zwróconych adresów:\n"; | ||
+ | text += "int sockFd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);\n"; | ||
+ | if(document.getElementById("AI_PASSIVE").checked) | ||
+ | text += "bind(sockFd, res->ai_addr, res->ai_addrlen);\n"; | ||
+ | else | ||
+ | text += "connect(sockFd, res->ai_addr, res->ai_addrlen);\n"; | ||
+ | text += "freeaddinfo(res);"; | ||
+ | document.getElementById("gaiCall").innerHTML = text; | ||
+ | } | ||
+ | </script> | ||
+ | <div style="padding: 0.5em; border: 2px lightgray solid; display: inline-block;"> | ||
+ | Interaktywny przykład jak używać <code>getaddrinfo</code>: | ||
+ | <small> | ||
+ | <div style="display:table"> | ||
+ | <div style="display:table-row"> | ||
+ | <div style="display:table-cell"> | ||
+ | <form> | ||
+ | Protokół warstwy sieci: | ||
+ | <select onChange="genGaiCall();" id="ai_family"> | ||
+ | <option value="">dowolny</option> | ||
+ | <option value="AF_INET">IPv4</option> | ||
+ | <option value="AF_INET6">IPv6</option> | ||
+ | </select> | ||
+ | <br> | ||
+ | Rodzaj gniazda: | ||
+ | <select onChange="genGaiCall();" id="ai_socktype"> | ||
+ | <option value="">dowolny</option> | ||
+ | <option value="SOCK_STREAM">strumieniowe</option> | ||
+ | <option value="SOCK_DGRAM">datagramowe</option> | ||
+ | </select> | ||
+ | <br> | ||
+ | Protokół warstwy transportowej: | ||
+ | <select onChange="genGaiCall();" id="ai_protocol"> | ||
+ | <option value="">dowolny</option> | ||
+ | <option value="IPPROTO_TCP">TCP</option> | ||
+ | <option value="IPPROTO_UDP">UDP</option> | ||
+ | </select> | ||
+ | <br> | ||
+ | Opcje: | ||
+ | <ul style="margin-top: 0em"> | ||
+ | <li><input onChange="genGaiCall();" type="checkbox" id="AI_PASSIVE"><label class="li" for="AI_PASSIVE"> chcę użyć adresu w funkcji <code>bind</code></label></li> | ||
+ | <li><input onChange="genGaiCall();" type="checkbox" checked id="AI_NUMERICHOST"><label class="li" for="AI_NUMERICHOST"> chcę użyć DNS do nazw domenowych</label></li> | ||
+ | <li><input onChange="genGaiCall();" type="checkbox" checked id="AI_NUMERICSERV"><label class="li" for="AI_NUMERICSERV"> zamiast numeru portu akceptuj też nazwę</label></li> | ||
+ | </ul> | ||
+ | </form> | ||
+ | </div> | ||
+ | <div style="display:table-cell"> | ||
+ | <pre id="gaiCall" style="margin-left:1em;margin-bottom:0"> | ||
+ | </pre> | ||
+ | <script>genGaiCall();</script> | ||
+ | </div> | ||
+ | </div> | ||
+ | </div> | ||
+ | </small> | ||
+ | </div> | ||
+ | </html> | ||
~~Zadanie.#~~ Stwórz klienta TCP, który: | ~~Zadanie.#~~ Stwórz klienta TCP, który: |