SDL_net: Added resolve-hostnames example program.

From fdb367a84d5598c7a208269c5b93a772e42d9186 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Mon, 11 Sep 2023 11:40:08 -0400
Subject: [PATCH] Added resolve-hostnames example program.

---
 CMakeLists.txt               |  3 ++
 SDL_net.c                    | 60 ------------------------------------
 examples/resolve-hostnames.c | 38 +++++++++++++++++++++++
 3 files changed, 41 insertions(+), 60 deletions(-)
 create mode 100644 examples/resolve-hostnames.c

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 743fab9..1ae12bd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -29,3 +29,6 @@ target_link_libraries(voipchat PRIVATE SDL3_net::SDL3_net SDL3::SDL3)
 
 add_executable(simple-http-get examples/simple-http-get.c)
 target_link_libraries(simple-http-get PRIVATE SDL3_net::SDL3_net SDL3::SDL3)
+
+add_executable(resolve-hostnames examples/resolve-hostnames.c)
+target_link_libraries(resolve-hostnames PRIVATE SDL3_net::SDL3_net SDL3::SDL3)
diff --git a/SDL_net.c b/SDL_net.c
index 4aac5c4..b4d4cae 100644
--- a/SDL_net.c
+++ b/SDL_net.c
@@ -1465,63 +1465,3 @@ int SDLNet_WaitUntilInputAvailable(void **vsockets, int numsockets, int timeoutm
 
     return retval;
 }
-
-
-#if 0  // some test code.
-#include <stdio.h>
-
-int main(int argc, char **argv)
-{
-    if (SDLNet_Init() < 0) {
-        SDL_Log("SDLNet_Init() failed: %s", SDL_GetError());
-        return 1;
-    }
-
-#if 1
-    SDLNet_StreamSocket *stream;
-    if (argc > 1) {
-        SDLNet_Server *server = SDLNet_CreateServer(NULL, 7997);
-        SDLNet_WaitUntilConnected(server);
-        SDLNet_AcceptClient(server, &stream);
-    } else {
-        SDLNet_Address *addr = SDLNet_ResolveHostname("localhost");
-        SDLNet_WaitUntilResolved(addr, -1);
-        stream = SDLNet_CreateClient(addr, 7997);
-        SDLNet_WaitUntilConnected(stream, -1);
-    }
-
-    printf("\n\nConnected!\n\n");
-
-    fcntl(fileno(stdin), F_SETFL, fcntl(fileno(stdin), F_GETFL, 0) | O_NONBLOCK);
-
-    while (1) {
-        char buf[128];
-        buf[0] = '\0';
-        fgets(buf, sizeof (buf), stdin);
-        if (buf[0]) {
-            SDLNet_WriteToStreamSocket(stream, buf, SDL_strlen(buf));
-        }
-        const int br = SDLNet_ReadStreamSocket(stream, buf, sizeof (buf));
-        if (br > 0) {
-            fwrite(buf, 1, br, stdout);
-        }
-    }
-#endif
-
-#if 0
-    //SDLNet_SimulateAddressResolutionLoss(3000, 30);
-
-    SDLNet_Address **addrs = (SDLNet_Address **) SDL_calloc(argc, sizeof (SDLNet_Address *));
-    for (int i = 1; i < argc; i++) {
-        addrs[i] = SDLNet_ResolveHostname(argv[i]);
-    }
-
-    for (int i = 1; i < argc; i++) {
-        SDLNet_WaitUntilResolved(addrs[i], -1);
-    }
-#endif
-
-    SDLNet_Quit();
-    return 0;
-}
-#endif
diff --git a/examples/resolve-hostnames.c b/examples/resolve-hostnames.c
new file mode 100644
index 0000000..f12b708
--- /dev/null
+++ b/examples/resolve-hostnames.c
@@ -0,0 +1,38 @@
+/*
+ * This is just for demonstration purposes! This doesn't
+ * do anything as complicated as, say, the `dig` utility.
+ *
+ * All this to say: don't use this for anything serious!
+ */
+
+#include <stdio.h>
+#include <SDL3/SDL.h>
+#include <SDL3/SDL_main.h>
+#include "SDL_net.h"
+
+int main(int argc, char **argv)
+{
+    if (SDLNet_Init() < 0) {
+        SDL_Log("SDLNet_Init() failed: %s", SDL_GetError());
+        return 1;
+    }
+
+    //SDLNet_SimulateAddressResolutionLoss(3000, 30);
+
+    SDLNet_Address **addrs = (SDLNet_Address **) SDL_calloc(argc, sizeof (SDLNet_Address *));
+    for (int i = 1; i < argc; i++) {
+        addrs[i] = SDLNet_ResolveHostname(argv[i]);
+    }
+
+    for (int i = 1; i < argc; i++) {
+        const char *str;
+        SDLNet_WaitUntilResolved(addrs[i], -1);
+        str = SDLNet_GetAddressString(addrs[i]);
+        SDL_Log("%s: %s", argv[i], str ? str : "[FAILED TO RESOLVE]");
+        SDLNet_UnrefAddress(addrs[i]);
+    }
+
+    SDLNet_Quit();
+
+    return 0;
+}