Skip to content

Commit

Permalink
Added resolve-hostnames example program.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Sep 24, 2023
1 parent ed8f7fd commit fdb367a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 60 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
60 changes: 0 additions & 60 deletions SDL_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 38 additions & 0 deletions examples/resolve-hostnames.c
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit fdb367a

Please sign in to comment.