home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Sockets / teststdio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-30  |  3.0 KB  |  184 lines  |  [TEXT/MPS ]

  1. #include <Events.h>
  2. #include <memory.h>
  3. #include <types.h>
  4. #include <OSUtils.h> /* for SysBeep */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <sys/types.h>
  9. #include <sys/time.h>
  10. #include <sys/errno.h>
  11. #include <sys/socket.h>
  12. #include <sys/ioctl.h>
  13. #include <netinet/in.h>
  14.  
  15. #include "tcpglue.h"
  16. #include "socket.internal.h"
  17.  
  18. static struct timeval pollPlease = {0,0};
  19. main()
  20. {
  21.     int status;
  22.     int s,s1,s2;
  23.     struct sockaddr_in me, her, name;
  24.     int namelen;
  25.     int herlen = sizeof(her);
  26.     int bytes;
  27.     char line[1000];
  28.     FILE *inFile, *outFile;
  29.  
  30.     s = s_socket(AF_INET, SOCK_STREAM, 0);
  31.     if (s < 0)
  32.     {
  33.         perror("socket");    
  34.         exit(1);
  35.     }
  36.     bzero((char *)&me, sizeof(me));
  37.     me.sin_family = AF_INET;
  38.     me.sin_port = htons(25);
  39.     if (s_bind(s, (caddr_t)&me, sizeof(me), 0) < 0)
  40.     {
  41.         perror("bind");    
  42.         exit(1);
  43.     }
  44.     
  45. #if 1
  46.     if (s_ioctl(s,FIONBIO,0) < 0)
  47.     {
  48.         perror("ioctl(FIONBIO)");    
  49.         exit(1);
  50.     }
  51. #endif
  52.  
  53.     her.sin_family = AF_INET;
  54. #if 0
  55.     her.sin_addr.s_addr = 0x0d000ce8;
  56. #else
  57.     her.sin_addr.s_addr = 0x8064660a;
  58. #endif
  59.     her.sin_port = htons(25);
  60.     if (s_connect(s,(caddr_t)&her,sizeof(her)) < 0)
  61.     {
  62.         if (errno != EINPROGRESS)
  63.         {
  64.             perror("connect");    
  65.             exit(1);
  66.         }
  67.     }
  68.  
  69.     if ((inFile = (FILE *)s_fdopen(s,"r")) == NULL)
  70.     {
  71.         perror("fdopen input");
  72.         exit(1);
  73.     }
  74.     if ((outFile = (FILE *)s_fdopen(s,"w")) == NULL)
  75.     {
  76.         perror("fdopen output");
  77.         exit(1);
  78.     }
  79.     (void) getcrlf(line,sizeof(line)-2,inFile);
  80.     errno = 0;
  81.     while (s_fprintf(outFile,"HELO milligan.utcs.utoronto.ca",line) == EOF)
  82.     {
  83.         if (errno != EALREADY)
  84.         {
  85.             perror("fprintf");
  86.             exit(1);
  87.         }
  88.         fprintf(stderr,"fprintf would block\n");
  89.         errno = 0;
  90.     }
  91.     errno = 0;
  92.     while (s_fflush(outFile) == EOF)
  93.     {
  94.         if (errno != EALREADY)
  95.         {
  96.             perror("fflush");
  97.             exit(1);
  98.         }
  99.         fprintf(stderr,"fflush would block\n");
  100.         errno = 0;
  101.     }
  102.     errno = 0;
  103.     while (s_fprintf(outFile,"\015\012",line) == EOF)
  104.     {
  105.         if (errno != EALREADY)
  106.         {
  107.             perror("fprintf");
  108.             exit(1);
  109.         }
  110.         fprintf(stderr,"fprintf would block\n");
  111.         errno = 0;
  112.     }
  113.     errno = 0;
  114.     while (s_fflush(outFile) == EOF)
  115.     {
  116.         if (errno != EALREADY)
  117.         {
  118.             perror("fflush");
  119.             exit(1);
  120.         }
  121.         fprintf(stderr,"fflush would block\n");
  122.         errno = 0;
  123.     }
  124.     (void) getcrlf(line,sizeof(line)-2,inFile);
  125. }
  126.  
  127. getcrlf(buf,n,inFile)  char *buf;  int n; FILE *inFile;
  128. {
  129.     char c;
  130.     register char *cs;
  131.     int seenCR;
  132.     
  133.     cs = buf;
  134.     seenCR = 0;
  135.     --n;
  136.     while(n > 0)
  137.     {
  138.         errno = 0;
  139.         c = s_fgetc(inFile);
  140.         if (c == EOF)
  141.         {
  142.             /* no input available or still connecting or writing - don't block */
  143.             if (errno == EWOULDBLOCK || errno == EALREADY)
  144.             {
  145.                 tcpCheckNotify();
  146.                 perror("getc: would block");
  147.                 continue;
  148.             }
  149.             else if (errno == 0)
  150.             {
  151.                 fprintf(stderr,"getc: end of file");
  152.                 exit(1);
  153.             }
  154.             else
  155.             {
  156.                 perror("getc");
  157.                 exit(1);
  158.             }
  159.         }
  160.         *cs++ = c;
  161.         --n;
  162.         if (seenCR && c == '\012')
  163.         {
  164.             seenCR = 0;
  165.             cs -= 2;
  166.             *cs++ = '\n';
  167.             break;
  168.         }
  169.         seenCR = (c == '\015');
  170.     }
  171.     if (seenCR /* n-1 bytes read and last was CR */)
  172.     {
  173.         ungetc(inFile);
  174.         cs--;
  175.     }
  176.     *cs++ = '\0';
  177.     fprintf(stderr,"'%s'\n",buf);
  178.     return;
  179. }
  180.  
  181. perror(s) char *s;
  182. {
  183.     (void) fprintf(stderr,"%s: error %d\n",s,errno);
  184. }