home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 20 / AACD20.BIN / AACD / Programming / TTCP_SDK / connect / connect.c next >
Encoding:
C/C++ Source or Header  |  1996-07-28  |  4.8 KB  |  215 lines

  1. /*
  2.  * This is a simple program that shows the basics of creating, connecting to 
  3.  * and getting data from a socket
  4.  *
  5.  */
  6.  
  7.  
  8. #include <pragmas/tsocket_pragmas.h>
  9. #include <proto/tsocket.h>
  10.  
  11. struct Library *TSocketBase;
  12.  
  13. #include <exec/libraries.h>
  14. #include <proto/dos.h>
  15. #include <proto/exec.h>
  16. #include <netdb.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/ioctl.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22.  
  23. #define BUFFSIZE 256
  24.  
  25. void ReadSocket(int sock);
  26.  
  27.  
  28. int main(int argc, char *argv[])
  29. {
  30.  
  31.     char *hostname;    
  32.     struct sockaddr_in hostaddr;
  33.     struct hostent *host;
  34.     struct servent *service;
  35.     fd_set sockmask, readmask;
  36.     int sock;
  37.  
  38.     if ( argc < 2 )
  39.     {
  40.         printf("usage: %s <host>\n", argv[0]);
  41.         return 10;
  42.     }
  43.  
  44.     hostname = argv[1];
  45.  
  46.  
  47.     if ( !(TSocketBase = OpenLibrary("tsocket.library", 0)) )
  48.     {
  49.         printf("TermiteTCP is not running.\n");
  50.         return 10;
  51.     }
  52.  
  53.     printf("\nUsing: %s\n", TSocketBase->lib_IdString);
  54.  
  55.     /*
  56.      * Create an internet stream socket
  57.      */
  58.  
  59.     if ( (sock = socket(AF_INET, SOCK_STREAM, 0)) != -1 )
  60.     {
  61.         /* 
  62.          * See if the hostname is numerical or alphanumeric
  63.          */
  64.  
  65.         if ( (hostaddr.sin_addr.s_addr = inet_addr(hostname)) == INADDR_NONE )
  66.         {
  67.             /* 
  68.              * hostname is not a dotted numerical address, look it up as a name
  69.              */
  70.  
  71.             if ( (host = gethostbyname(hostname)) != NULL )
  72.             {
  73.                 /*
  74.                  * We found it.  Copy out the numerical address.
  75.                  */
  76.  
  77.                 CopyMem(host->h_addr, &hostaddr.sin_addr, host->h_length);
  78.             }
  79.             else
  80.             {
  81.                 printf("host not found: %s\n", hostname);
  82.  
  83.                 /* Do the following to flag an error */
  84.  
  85.                 hostname[0] = NULL;
  86.             }
  87.         }
  88.  
  89.         /* if no error occurred above */
  90.  
  91.         if ( hostname[0] )
  92.         {
  93.             /*
  94.              * Look up the service (port) number for ftp
  95.              */           
  96.  
  97.             if ( (service = getservbyname("ftp", "tcp")) != NULL )
  98.             {
  99.                 hostaddr.sin_port   = service->s_port;
  100.                 hostaddr.sin_family = AF_INET;
  101.     
  102.                 /*
  103.                  * Now try to connect to it
  104.                  */
  105.  
  106.                 if ( (connect(sock, 
  107.                                 (struct sockaddr *)&hostaddr, 
  108.                                 sizeof(struct sockaddr_in))) != -1 )
  109.                 {
  110.                     printf("Connected to %s\n", hostname);
  111.  
  112.                     /*
  113.                      * Create an fdset (sockmask), consisting of just
  114.                      * one socket (sock).
  115.                      */
  116.  
  117.                     FD_ZERO(&sockmask);
  118.                     FD_SET(sock, &sockmask);
  119.  
  120.                     /*
  121.                      * Wait for a socket in our fdset to become 
  122.                      * readable.
  123.                      */
  124.  
  125.                      readmask = sockmask;
  126.  
  127.                      if ( (WaitSelect(2, &readmask, NULL, NULL, NULL, NULL)) != -1 )
  128.                      {
  129.                          /* sock is in the readable set? */
  130.     
  131.                          if ( FD_ISSET(sock, &readmask) )
  132.                              ReadSocket(sock);
  133.                      }
  134.                      else
  135.                      {
  136.                          printf("Aborted.\n");
  137.                      }
  138.  
  139.                 }
  140.                 else
  141.                     printf("Can't connect to %s\n", hostname);
  142.             }
  143.             else
  144.                 printf("unknown service: telnet\n");
  145.         }
  146.  
  147.         CloseSocket(sock);
  148.     }
  149.     else
  150.         printf("Can't create socket.\n");
  151.  
  152. #ifndef TERMITE_TCP
  153.     CloseLibrary(SocketBase);
  154. #else
  155.     CloseLibrary(TSocketBase);
  156. #endif       
  157.  
  158.     return 0;
  159. }
  160.  
  161.  
  162. void ReadSocket(int sock)
  163. {
  164.     int bytesleft, bytesrecvd;
  165.     unsigned char buff[BUFFSIZE];
  166.     static BPTR output = NULL;
  167.  
  168.     while ( 1 )
  169.     {
  170.         /*
  171.          * See how many bytes remain to be read.
  172.          */
  173.  
  174.         if ( (IoctlSocket(sock, FIONREAD, (char *)&bytesleft)) == -1 )
  175.         {
  176.             printf("ioctl error\n");
  177.             break;
  178.         }
  179.  
  180.         if ( bytesleft <= 0 )
  181.             break;
  182.  
  183.         /*
  184.          * Take precautions to avoid overflowing our buffer
  185.          */
  186.     
  187.         if ( bytesleft > BUFFSIZE )
  188.             bytesleft = BUFFSIZE;
  189.  
  190.         /*
  191.          * Read bytes from socket
  192.          */
  193.  
  194.         bytesrecvd = recv(sock, buff, bytesleft, 0);
  195.  
  196.         if ( bytesrecvd == -1 )
  197.         {
  198.             printf("recv error\n");
  199.             break;
  200.         }
  201.         
  202.         /*
  203.          * Dump any bytes received from socket
  204.          */
  205.  
  206.         if ( bytesrecvd > 0 )
  207.         {
  208.             if ( !output )
  209.                 output = Output();
  210.         
  211.             Write(output, buff, bytesrecvd);
  212.         }
  213.     }
  214. }
  215.