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/tcpstream.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 sdk/net/tcpstream.c (limited to 'sdk/net/tcpstream.c') diff --git a/sdk/net/tcpstream.c b/sdk/net/tcpstream.c new file mode 100644 index 0000000..b39645d --- /dev/null +++ b/sdk/net/tcpstream.c @@ -0,0 +1,88 @@ +/* + * Portable TCP session stream functions. + * $Id$ + * Copyright (c) 1997 by Tycho Softworks. + * For conditions of distribution and reuse see product license. + */ + +#include +#include + +STREAM opentcp(const char *host, int port) +{ + SOCKET so = getsocket(host, port, SOCK_STREAM); + FILE *fp; + + if(so == INVALID_SOCKET) + return NULL; + + fp = fdopen(so, "r+"); + if(!fp) + { + endsocket(so); + return NULL; + } + buftcp(fp, 0); + return fp; +} + +char *gettcp(char *buf, size_t count, STREAM fp) +{ + memset(buf, 0, count); + if(!fgets(buf, count, fp)) + return NULL; + + return buf; +} + +int puttcp(char *buf, STREAM fp) +{ + int err; + err = fputs(buf, fp); + fflush(fp); + return err; +} + +void closetcp(STREAM fp) +{ + SOCKET so = fileno(fp); + + fflush(fp); + shutdown(so, 2); + fclose(fp); +} + +int buftcp(STREAM fp, int size) +{ + fflush(fp); + + if(!size) + return setvbuf(fp, NULL, _IOLBF, 1024); + else + return setvbuf(fp, NULL, _IOFBF, size); +} + +STREAM accepttcp(SOCKET so) +{ + struct sockaddr addr; + int len; + int si; + FILE *fp; + + len = sizeof(addr); + si = accept(so, &addr, &len); + if(si < 0) + return NULL; + + fp = fdopen(si, "r+"); + if(!fp) + { + shutdown(si, 2); + close(si); + return NULL; + } + + buftcp(fp, 0); + return fp; +} + -- cgit v1.2.3-54-g00ecf