home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / vrac / souper15.zip / SOURCE / SOCKET.C < prev    next >
C/C++ Source or Header  |  1996-05-18  |  5KB  |  238 lines

  1. /* $Id: socket.c 1.3 1996/05/18 21:06:26 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, short clientPort)
  38. {
  39. #if defined(__OS2__) && !defined(__EMX__)
  40.     static char initialized = 0;
  41. #endif
  42.     int sock;
  43.     unsigned long inaddr;
  44.     struct sockaddr_in ad;
  45.     struct hostent *hp;
  46.  
  47. #if defined(__OS2__) && !defined(__EMX__)
  48.     if (!initialized) {
  49.     sock_init();
  50.     initialized = 1;
  51.     }
  52. #endif
  53.     
  54.     memset(&ad, 0, sizeof(ad));
  55.     ad.sin_family = AF_INET;
  56.  
  57.     inaddr = inet_addr(host);
  58.     if (inaddr != INADDR_NONE)
  59.         memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
  60.     else
  61.     {
  62.         hp = gethostbyname(host);
  63.         if (hp == NULL)
  64.             return -1;
  65.         memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
  66.     }
  67.     ad.sin_port = htons(clientPort);
  68.     
  69.     sock = socket(AF_INET, SOCK_STREAM, 0);
  70. #ifdef __WIN32__
  71.     if (sock == INVALID_SOCKET) return sock;
  72. #else
  73.     if (sock < 0) return sock;
  74. #endif
  75.     if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0)
  76.         return -1;
  77.     return sock;
  78. }
  79.  
  80. void
  81. SockClose (int s)
  82. {
  83.     shutdown(s, 2);
  84. #if defined(__OS2__) && !defined(__EMX__)
  85.     soclose(s);
  86. #elif defined(__WIN32__)
  87.     closesocket(s);
  88. #else
  89.     close(s);
  90. #endif
  91. }
  92.  
  93. #define BUFSIZE 4096
  94.  
  95. int
  96. SockGets (int s, char *dest, int n)
  97. {
  98.     static char buffer1[BUFSIZE];
  99.     static char *buffer = buffer1;
  100.     static int bufsize = 0;
  101.  
  102.     char *lfptr;
  103.     int stringpos = 0, copysize, posinc, flReturnString = 0;
  104.  
  105.     for (;;)
  106.         {
  107.         /* get position of LF in the buffer */
  108.         lfptr = memchr( buffer, '\n', bufsize );
  109.  
  110.         /* if there is a LF in the buffer, then ... */
  111.         if ( lfptr )
  112.             {
  113.             copysize = lfptr - buffer + 1;
  114.             flReturnString = 1;
  115.             }
  116.         else
  117.             copysize = bufsize;
  118.  
  119.         /* make sure we won't write more than n-1 characters */
  120.         if ( copysize > (n - 1) - stringpos )
  121.             {
  122.             copysize = (n - 1) - stringpos;
  123.             flReturnString = 1;
  124.             }
  125.  
  126.         /* copy copysize characters from buffer into the string */
  127.         memcpy( dest + stringpos, buffer, copysize );
  128.         stringpos += copysize;
  129.  
  130.         /* if we got the whole string, then set buffer to the unused data
  131.            and return the string */
  132.         if ( flReturnString )
  133.             {
  134.         if (dest[stringpos-2] == '\r')
  135.         dest[stringpos-2] = '\0';
  136.         else if (dest[stringpos-1] == '\n')
  137.         dest[stringpos-1] = '\0';
  138.         else
  139.         dest[stringpos] = '\0';
  140.             bufsize -= copysize;
  141.             buffer += copysize;
  142.             return 0;
  143.             }
  144.  
  145.         /* reset buffer and receive more data into buffer */
  146.         buffer = buffer1;
  147.         if ( (bufsize = recv( s, buffer1, sizeof(buffer1), 0 )) == -1 )
  148.             {
  149.             perror( "Error on socket recv" );
  150.             return -1;
  151.             }
  152.         }
  153. #if 0
  154.     while (--len)
  155.     {
  156.         if (recv(socket, buf, 1, 0) != 1)
  157.             return -1;
  158.         if (*buf == '\n')
  159.             break;
  160.         if (*buf != '\r') /* remove all CRs */
  161.             buf++;
  162.     }
  163.     *buf = 0;
  164.     return 0;
  165. #endif
  166. }
  167.  
  168. int
  169. SockPuts (int socket, char *buf)
  170. {
  171.     int rc;
  172.     
  173.     if ((rc = SockWrite(socket, buf, strlen(buf))) < 0)
  174.         return rc;
  175.     return SockWrite(socket, "\r\n", 2);
  176. }
  177.  
  178. int
  179. SockWrite (int socket, void *src, int len)
  180. {
  181.     int n;
  182.     char *buf = (char *)src;
  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, void *dest, int len)
  195. {
  196.     int n;
  197.     char *buf = (char *)dest;
  198.     
  199.     while (len) {
  200.         n = recv(socket, buf, len, 0);
  201.         if (n <= 0)
  202.             return -1;
  203.         len -= n;
  204.         buf += n;
  205.     }
  206.     return 0;
  207. }
  208.  
  209. int
  210. SockPrintf (int socket, const char *format, ...)
  211. {
  212.     va_list ap;
  213.     char buf[2048];
  214.     
  215.     va_start(ap, format);
  216.     vsprintf(buf, format, ap);
  217.     va_end(ap);
  218.     return SockWrite(socket, buf, strlen(buf));
  219. }
  220.  
  221. #if 0
  222. int
  223. SockStatus (int socket, int seconds)
  224. {
  225.     fd_set fds;
  226.     struct timeval timeout;
  227.     
  228.     FD_ZERO(fds);
  229.     FD_SET(fds, socket);
  230.     timeout.tv_sec = seconds;
  231.     timeout.tv_usec = 0;
  232.  
  233.     if (select(socket+1, &fds, (fd_set *)0, (fd_set *)0, &timeout) <= 0)
  234.         return 0;
  235.     return 1;
  236. }
  237. #endif
  238.