From 0cc9b20c15460213e488bf5e70963b941482f628 Mon Sep 17 00:00:00 2001 From: William Harrington Date: Tue, 14 Jan 2025 16:06:02 -0600 Subject: Add source. --- sdk/net/getsocket.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 sdk/net/getsocket.c (limited to 'sdk/net/getsocket.c') diff --git a/sdk/net/getsocket.c b/sdk/net/getsocket.c new file mode 100644 index 0000000..83ca68a --- /dev/null +++ b/sdk/net/getsocket.c @@ -0,0 +1,43 @@ +#include +#include + +SOCKET getsocket(const char *host, int port, int socktype) +{ + SOCKET so; + struct hostent *hp; + struct sockaddr_in saddr; + struct in_addr *aptr; + char *sp, *p; + char *dup = strdup(host); + + so = socket(AF_INET, socktype, 0); + if(so == INVALID_SOCKET) + { + free(dup); + return so; + } + + sp = dup; + while(NULL != (p = strtok(sp, " ;:\t"))) + { + sp = NULL; + hp = gethost(p); + if(!hp) + continue; + + saddr.sin_family = hp->h_addrtype; + saddr.sin_port = htons(port); + while( (aptr = (struct in_addr *)*(hp->h_addr_list)++) != NULL) + saddr.sin_addr = *aptr; + + if(connect(so, (struct sockaddr *)&saddr, sizeof(saddr))) + continue; + + free(dup); + return so; + } + endsocket(so); + free(dup); + return INVALID_SOCKET; +} + -- cgit v1.2.3-54-g00ecf