2 * socket.c -- socket library functions
4 * These were designed and coded by Carl Harris <ceharris@mal.com>
5 * and are essentially unchanged from the ancestral popclient.
7 * The file pointer arguments are currently misleading -- there
8 * is only one shared internal buffer for all sockets.
10 * For license terms, see the file COPYING in this directory.
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
23 #include <sys/ioctl.h>
24 #if defined(STDC_HEADERS)
28 #if defined(HAVE_UNISTD_H)
31 #if defined(HAVE_STDARG_H)
39 /* Size of buffer for internal buffering read function
40 don't increase beyond the maximum atomic read/write size for
41 your sockets, or you'll take a potentially huge performance hit */
43 #define INTERNAL_BUFSIZE 2048
45 FILE *Socket(host, clientPort)
51 struct sockaddr_in ad;
54 memset(&ad, 0, sizeof(ad));
55 ad.sin_family = AF_INET;
57 inaddr = inet_addr(host);
58 if (inaddr != INADDR_NONE)
59 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
62 hp = gethostbyname(host);
65 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
67 ad.sin_port = htons(clientPort);
69 sock = socket(AF_INET, SOCK_STREAM, 0);
72 if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
74 return fdopen(sock, "r+");
77 int SockWrite(buf,len,sockfp)
86 n = write(fileno(sockfp), buf, len);
96 int SockGets(buf, len, sockfp)
105 if (read(fileno(sockfp), buf, 1) != 1)
111 if (*buf != '\r') /* remove all CRs */
118 #if defined(HAVE_STDARG_H)
119 int SockPrintf(FILE *sockfp, char* format, ...)
122 int SockPrintf(sockfp,format,va_alist)
131 #if defined(HAVE_STDARG_H)
132 va_start(ap, format) ;
136 vsprintf(buf, format, ap);
138 return SockWrite(buf, strlen(buf), sockfp);
142 /* socket.c ends here */