home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / cfuncs / tconnect.c < prev   
C/C++ Source or Header  |  2000-07-29  |  2KB  |  97 lines

  1. /*
  2. ############################################################################
  3. #
  4. #    File:     tconnect.c
  5. #
  6. #    Subject:  Function to open TCP connection
  7. #
  8. #    Author:   Gregg M. Townsend
  9. #
  10. #    Date:     October 3, 1995
  11. #
  12. ############################################################################
  13. #
  14. #   This file is in the public domain.
  15. #
  16. ############################################################################
  17. #
  18. #  tconnect(hostname, portnum) establishes a TCP connection to the given
  19. #  host and port, returning an Icon file f.
  20. #
  21. #  Note that seek(f) must be called when switching between input and output
  22. #  on this bidirectional file.  Additionally, the DEC Alpha requires a call
  23. #  to flush(f), after the seek, when switching from input to output.
  24. #
  25. ############################################################################
  26. #
  27. #  See also:  fpoll.c
  28. #
  29. ############################################################################
  30. #
  31. #  Requires:  Unix, dynamic loading
  32. #
  33. ############################################################################
  34. */
  35.  
  36. #include <string.h>
  37. #include <stdio.h>
  38.  
  39. #include <fcntl.h>
  40. #include <netdb.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <netinet/in.h>
  44.  
  45. #include "icall.h"
  46.  
  47.  
  48. int tconnect(int argc, descriptor *argv)    /*: connect to TCP socket */
  49.    {
  50.    char *hostname, filename[1000];
  51.    unsigned char *p;
  52.    int port, fd, i, d[4];
  53.    FILE *fp;
  54.    struct hostent *h;
  55.    struct sockaddr_in sin;
  56.  
  57.    memset(&sin, 0, sizeof(sin));
  58.  
  59.    /* check arguments */
  60.    ArgString(1);
  61.    hostname = StringVal(argv[1]);
  62.  
  63.    ArgInteger(2);
  64.    port = IntegerVal(argv[2]);
  65.  
  66.    /* get host address */
  67.    if (sscanf(hostname, "%d.%d.%d.%d", &d[0], &d[1], &d[2], &d[3]) == 4) {
  68.       p = (unsigned char *) &sin.sin_addr;
  69.       for (i = 0; i < 4; i++)
  70.          p[i] = d[i];
  71.       }
  72.    else {
  73.       h = gethostbyname(hostname);
  74.       if (!h)
  75.          Fail;
  76.       memcpy(&sin.sin_addr, h->h_addr, sizeof(struct in_addr));
  77.       endhostent();
  78.       }
  79.  
  80.    /* create socket and connect */
  81.    sin.sin_family = AF_INET;
  82.    sin.sin_port = htons(port);
  83.    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  84.       Fail;
  85.    if (connect(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0)
  86.       Fail;
  87.  
  88.    /* create stdio file pointer */
  89.    fp = fdopen(fd, "r+");
  90.    if (!fp)
  91.       Fail;
  92.  
  93.    /* return Icon file */
  94.    sprintf(filename, "%s:%d", hostname, port);
  95.    RetFile(fp, Fs_Read | Fs_Write, filename);
  96.    }
  97.