home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / libncftp / libncftp-3.2.5-src.zip / libncftp-3.2.5 / sio / UBind.c < prev    next >
C/C++ Source or Header  |  2009-11-30  |  1KB  |  65 lines

  1. #include "syshdrs.h"
  2. #ifdef PRAGMA_HDRSTOP
  3. #    pragma hdrstop
  4. #endif
  5.  
  6. #ifdef HAVE_SYS_UN_H
  7.  
  8. int
  9. UBind(int sockfd, const char *const astr, const int nTries, const int reuseFlag)
  10. {
  11.     unsigned int i;
  12.     int on;
  13.     sockopt_size_t onsize;
  14.     struct sockaddr_un localAddr;
  15.     sockaddr_size_t ualen;
  16.     
  17.     if ((astr == NULL) || (astr[0] == '\0'))  {
  18.         errno = EINVAL;
  19.         return (-1);
  20.     }
  21.  
  22.     ualen = (sockaddr_size_t) MakeSockAddrUn(&localAddr, astr);
  23.     (void) unlink(localAddr.sun_path);
  24.  
  25.     if (reuseFlag != kReUseAddrNo) {
  26.         /* This is mostly so you can quit the server and re-run it
  27.          * again right away.  If you don't do this, the OS may complain
  28.          * that the address is still in use.
  29.          */
  30.         on = 1;
  31.         onsize = (sockopt_size_t) sizeof(on);
  32.         (void) setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
  33.             &on, onsize);
  34.     }
  35.  
  36.     for (i=1; ; i++) {
  37.         /* Try binding a few times, in case we get Address in Use
  38.          * errors.
  39.          */
  40.         if (bind(sockfd, (struct sockaddr *) &localAddr, ualen) == 0) {
  41.             break;
  42.         }
  43.         if (i == (unsigned int) nTries) {
  44.             return (-1);
  45.         }
  46.         /* Give the OS time to clean up the old socket,
  47.          * and then try again.
  48.          */
  49.         sleep(i * 3);
  50.     }
  51.  
  52.     return (0);
  53. }    /* UBind */
  54.  
  55.  
  56.  
  57.  
  58. int
  59. UListen(int sfd, int backlog)
  60. {
  61.     return (listen(sfd, (listen_backlog_t) backlog));
  62. }    /* UListen */
  63.  
  64. #endif    /* HAVE_SYS_UN_H */
  65.