2 * socket.c -- socket library functions
4 * For license terms, see the file COPYING in this directory.
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
16 #if defined(STDC_HEADERS)
19 #if defined(HAVE_UNISTD_H)
22 #if defined(HAVE_STDARG_H)
30 #ifdef INADDR_BROADCAST
31 #define INADDR_NONE INADDR_BROADCAST
33 #define INADDR_NONE -1
38 * Size of buffer for internal buffering read function
39 * don't increase beyond the maximum atomic read/write size for
40 * your sockets, or you'll take a potentially huge performance hit
42 #define INTERNAL_BUFSIZE 2048
44 FILE *sockopen(char *host, int clientPort)
48 struct sockaddr_in ad;
52 memset(&ad, 0, sizeof(ad));
53 ad.sin_family = AF_INET;
55 inaddr = inet_addr(host);
56 if (inaddr != INADDR_NONE)
57 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
60 hp = gethostbyname(host);
63 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
65 ad.sin_port = htons(clientPort);
67 sock = socket(AF_INET, SOCK_STREAM, 0);
70 if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
75 fp = fdopen(sock, "r+");
77 /* the point of all this mishigoss ... dynamic per-stream buffering */
78 setvbuf(fp, NULL, _IOLBF, INTERNAL_BUFSIZE);
85 * Use the chargen service to test buffering directly.
89 FILE *fp = sockopen("localhost", 19);
92 while (fgets(buf, sizeof(buf)-1, fp))
97 /* socket.c ends here */