diff options
author | William Harrington <kb0iic@berzerkula.org> | 2025-01-14 16:06:02 -0600 |
---|---|---|
committer | William Harrington <kb0iic@berzerkula.org> | 2025-01-14 16:06:02 -0600 |
commit | 0cc9b20c15460213e488bf5e70963b941482f628 (patch) | |
tree | bb0143245583ec846630f39bfa2258dba640ccd7 /sdk/net/tcpstream.c | |
parent | 0e084ade5069756d487b5c948c48b777e37c00c9 (diff) |
Add source.
Diffstat (limited to 'sdk/net/tcpstream.c')
-rw-r--r-- | sdk/net/tcpstream.c | 88 |
1 files changed, 88 insertions, 0 deletions
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 <net/stream.h> +#include <std/string.h> + +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; +} + |