home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / me34src.zip / me3 / comserver / client.c next >
C/C++ Source or Header  |  1995-01-14  |  2KB  |  89 lines

  1. /* client.c :  Routines for the client end of the client/server
  2.  *    communications.
  3.  */
  4.  
  5. /* Craig Durland    Public Domain
  6.  *   I got the algorithms from MITs X11 source.
  7.  *   Distributed "as is", without warranties of any kind, but comments,
  8.  *     suggestions and bug reports are welcome.
  9.  */
  10.  
  11. static char what[] = "@(#)Compute Server Client Support 2/2/92 v1.1 8/93";
  12.  
  13. /* ******************************************************************** */
  14. /* ************************** Client Socket *************************** */
  15. /* ******************************************************************** */
  16.  
  17. #ifdef __STDC__
  18.  
  19. #ifdef __hpux            /* for ANSI C on HP-UX */
  20. #define _HPUX_SOURCE
  21. #endif  /* __hpux */
  22.  
  23. #ifdef __apollo            /* for ANSI C on Apollo BSD */
  24. #define _BSD_SOURCE
  25. #endif    /* __apollo */
  26.  
  27. #ifdef _AIX            /* for ANSI C on IBM AIX */
  28. #define _ALL_SOURCE
  29. #endif  /* _AIX */
  30.  
  31. #endif    /*  __STDC__ */
  32.  
  33. #include <stdio.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <sys/un.h>
  37. #include <errno.h>
  38.  
  39.  
  40. int CSopen_client_socket(socket_name, retries)
  41.   char *socket_name; int retries;
  42. {
  43.   struct sockaddr_un unaddr;        /* UNIX socket data block */
  44.   struct sockaddr *addr;        /* generic socket pointer */
  45.   int addrlen;                /* length of addr */
  46.   int fd;                /* socket file descriptor */
  47.  
  48.   unaddr.sun_family = AF_UNIX;
  49.   strcpy (unaddr.sun_path, socket_name);
  50.  
  51.   addr = (struct sockaddr *)&unaddr;
  52. #ifdef SUN_LEN    /* BSD44SOCKETS, SUN_LEN defined in sys/un.h on AIX */
  53.   addrlen = SUN_LEN(&unaddr);
  54.   unaddr.sun_len = strlen(unaddr.sun_path);
  55. #else
  56. #ifdef SCM_RIGHTS    /* 4.3bsd reno and later (e.g. 386bsd) */
  57.   addrlen = sizeof(unaddr.sun_len) + sizeof(unaddr.sun_family) +
  58.         strlen(unaddr.sun_path) + 1;
  59. #else
  60.   addrlen = strlen(unaddr.sun_path) + sizeof(unaddr.sun_family);
  61. #endif /* SCM_RIGHTS */
  62. #endif /* SUN_LEN */
  63.  
  64.   /*
  65.    * Open the network connection.
  66.    */
  67.   do
  68.   {
  69.     if ((fd = socket((int) addr->sa_family, SOCK_STREAM, 0)) < 0)
  70.     return -1;
  71.  
  72.     if (0 == connect(fd, addr, addrlen)) break;
  73.     else        /* error */
  74.     {
  75.       int olderrno = errno;
  76.  
  77.       (void) close(fd);
  78.       if (olderrno != ENOENT || retries <= 0)
  79.       {
  80.     errno = olderrno;
  81.     return -1;
  82.       }
  83.       sleep(1);
  84.     }
  85.   } while (retries-- > 0);
  86.  
  87.   return fd;
  88. }
  89.