home *** CD-ROM | disk | FTP | other *** search
/ Tutto per Internet / Internet.iso / soft95 / News / souper95 / socket.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-12  |  5.2 KB  |  237 lines

  1. /* $Id: socket.c 1.2 1995/06/25 16:39:27 cthuang Exp $
  2.  *
  3.  * This module has been modified for souper.
  4.  */
  5.  
  6. /***********************************************************************
  7.   module:       socket.c
  8.   program:      popclient
  9.   SCCS ID:      @(#)socket.c    1.5  4/1/94
  10.   programmer:   Virginia Tech Computing Center
  11.   compiler:     DEC RISC C compiler (Ultrix 4.1)
  12.   environment:  DEC Ultrix 4.3 
  13.   description:  UNIX sockets code.
  14.  ***********************************************************************/
  15.  
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <fcntl.h>
  19. #include <netinet/in.h>
  20. #include <netdb.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #ifdef __WIN32__
  26. #include <winsock.h>
  27. #else
  28. #include <unistd.h>
  29. #endif
  30.  
  31. #include "socket.h"
  32.  
  33. /* requisite, venerable SCCS ID */
  34. static char sccs_id [] = "@(#)socket.c  1.4\t3/29/94";
  35.  
  36. int
  37. Socket (const char *host, int clientPort)
  38. {
  39. #if defined(__OS2__) && !defined(__EMX__)
  40.     static char initialized = 0;
  41. #endif
  42.     int sock;
  43.  
  44.     unsigned long inaddr;
  45.     struct sockaddr_in ad;
  46.     struct hostent *hp;
  47.     
  48.     memset(&ad, 0, sizeof(ad));
  49.     ad.sin_family = AF_INET;
  50.  
  51. #if defined(__OS2__) && !defined(__EMX__)
  52.     if (!initialized) {
  53.     sock_init();
  54.     initialized = 1;
  55.     }
  56. #endif
  57.  
  58.     inaddr = inet_addr(host);
  59.     if (inaddr != INADDR_NONE)
  60.         memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
  61.     else
  62.     {
  63.         hp = gethostbyname(host);
  64.         if (hp == NULL)
  65.             return -1;
  66.         memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
  67.     }
  68.     ad.sin_port = htons(clientPort);
  69.     
  70.     sock = socket(AF_INET, SOCK_STREAM, 0);
  71. #ifdef __WIN32__
  72.     if (sock == INVALID_SOCKET) return sock;
  73. #else
  74.     if (sock < 0) return sock;
  75. #endif
  76.     if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0)
  77.         return -1;
  78.     return sock;
  79. }
  80.  
  81. void
  82. SockClose (int s)
  83. {
  84.     shutdown(s, 2);
  85. #if defined(__OS2__) && !defined(__EMX__)
  86.     soclose(s);
  87. #elif defined(__WIN32__)
  88.     closesocket(s);
  89. #else
  90.     close(s);
  91. #endif
  92. }
  93.  
  94. #define BUFSIZE 4096
  95.  
  96. int
  97. SockGets (int s, char *dest, int n)
  98. {
  99.     static char buffer1[BUFSIZE];
  100.     static char *buffer = buffer1;
  101.     static int bufsize = 0;
  102.  
  103.     char *lfptr;
  104.     int stringpos = 0, copysize, posinc, flReturnString = 0;
  105.  
  106.     for (;;)
  107.         {
  108.         /* get position of LF in the buffer */
  109.         lfptr = memchr( buffer, '\n', bufsize );
  110.  
  111.         /* if there is a LF in the buffer, then ... */
  112.         if ( lfptr )
  113.             {
  114.             copysize = lfptr - buffer + 1;
  115.             flReturnString = 1;
  116.             }
  117.         else
  118.             copysize = bufsize;
  119.  
  120.         /* make sure we won't write more than n-1 characters */
  121.         if ( copysize > (n - 1) - stringpos )
  122.             {
  123.             copysize = (n - 1) - stringpos;
  124.             flReturnString = 1;
  125.             }
  126.  
  127.         /* copy copysize characters from buffer into the string */
  128.         memcpy( dest + stringpos, buffer, copysize );
  129.         stringpos += copysize;
  130.  
  131.         /* if we got the whole string, then set buffer to the unused data
  132.            and return the string */
  133.         if ( flReturnString )
  134.             {
  135.         if (dest[stringpos-2] == '\r')
  136.         dest[stringpos-2] = '\0';
  137.         else if (dest[stringpos-1] == '\n')
  138.         dest[stringpos-1] = '\0';
  139.         else
  140.         dest[stringpos] = '\0';
  141.             bufsize -= copysize;
  142.             buffer += copysize;
  143.             return 0;
  144.             }
  145.  
  146.         /* reset buffer and receive more data into buffer */
  147.         buffer = buffer1;
  148.         if ( (bufsize = recv( s, buffer1, sizeof(buffer1), 0 )) == -1 )
  149.             {
  150.             perror( "Error on socket recv" );
  151.             return -1;
  152.             }
  153.         }
  154. #if 0
  155.     while (--len)
  156.     {
  157.         if (recv(socket, buf, 1, 0) != 1)
  158.             return -1;
  159.         if (*buf == '\n')
  160.             break;
  161.         if (*buf != '\r') /* remove all CRs */
  162.             buf++;
  163.     }
  164.     *buf = 0;
  165.     return 0;
  166. #endif
  167. }
  168.  
  169. int
  170. SockPuts (int socket, char *buf)
  171. {
  172.     int rc;
  173.     
  174.     if ((rc = SockWrite(socket, buf, strlen(buf))) < 0)
  175.         return rc;
  176.     return SockWrite(socket, "\r\n", 2);
  177. }
  178.  
  179. int
  180. SockWrite (int socket, char *buf, int len)
  181. {
  182.     int n;
  183.     
  184.     while (len) {
  185.         n = send(socket, buf, len, 0);
  186.         if (n <= 0)
  187.             return -1;
  188.         len -= n;
  189.         buf += n;
  190.     }
  191.     return 0;
  192. }
  193.  
  194. int SockRead (int socket, char *buf, int len)
  195. {
  196.     int n;
  197.     
  198.     while (len) {
  199.         n = recv(socket, buf, len, 0);
  200.         if (n <= 0)
  201.             return -1;
  202.         len -= n;
  203.         buf += n;
  204.     }
  205.     return 0;
  206. }
  207.  
  208. int
  209. SockPrintf (int socket, const char *format, ...)
  210. {
  211.     va_list ap;
  212.     char buf[2048];
  213.     
  214.     va_start(ap, format);
  215.     vsprintf(buf, format, ap);
  216.     va_end(ap);
  217.     return SockWrite(socket, buf, strlen(buf));
  218. }
  219.  
  220. #if 0
  221. int
  222. SockStatus (int socket, int seconds)
  223. {
  224.     fd_set fds;
  225.     struct timeval timeout;
  226.     
  227.     FD_ZERO(fds);
  228.     FD_SET(fds, socket);
  229.     timeout.tv_sec = seconds;
  230.     timeout.tv_usec = 0;
  231.  
  232.     if (select(socket+1, &fds, (fd_set *)0, (fd_set *)0, &timeout) <= 0)
  233.         return 0;
  234.     return 1;
  235. }
  236. #endif
  237.