This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: help on socket connexion


Hello,

Ok, part of code...

If I run a client test (loop of 60 connection) to connect to the
server, all the fork/connection are ok...
but sometimes all the fork still "hang" in the process list even if I
stop the client


(sorry but the code is "as is")

thanx for your help




static int su=0;
static int dlog=0;
static HANDLE hlib;


sigjmp_buf context;


#define SET_BLOCKING(s)       set_blocking(s, XTRUE)
#define SET_NONBLOCKING(s)    set_blocking(s, XFALSE)


static int sys_lock(int fd)
{
#if defined(F_SETLK)
  struct flock lockparam;

  lockparam.l_type = F_WRLCK;
  lockparam.l_whence = SEEK_SET;
  lockparam.l_start = 0;
  lockparam.l_len = 0;        /* whole file */
    
  return fcntl(fd, F_SETLK, &lockparam);
#elif defined(HAVE_FLOCK)
  return flock(fd, LOCK_EX|LOCK_NB);
#elif defined(HAVE_LOCKF)
  return lockf(fd, F_TLOCK, 0);
#else
# error "No supported lock method..."
#endif
}




/**
*
*/ 
static int set_blocking(int sck, XBOOL flg)
{
  int f;
  
  f = fcntl(sck, F_GETFL, 0);
  
  if (f==-1)
    return -1;
    
  if (flg==XFALSE)
    return fcntl(sck, F_SETFL, f | O_NONBLOCK);
  else
    return fcntl(sck, F_SETFL, f & (~O_NONBLOCK));
}




/*
*
*/
static void sigpipe(int s)
{
  log_killfile();
  printf("SIGPIPE\n");
  exit(0);
}


/*
*
*/
static void sigint(int s)
{
  log_killfile();
  printf("SIGINT\n");
  exit(0);
}



/*
*
*/
static void resume()
{
  printf("SIGSEGV\n");
  SET_CMD(pTmp, "signal 11");
  siglongjmp(context, 6);
}






/*
*
*/
static void sigchld(int n)
{
  int pid;

#ifdef CYGWIN
  pid = wait3(NULL, WNOHANG,NULL);

  if (pid>0) 
    LP("sig-xsa", "child %d stops with signal %d", pid, n);

#else  
  while(1)
  {
    pid = wait3(NULL, WNOHANG,NULL);
    
    if (pid>0) 
      LP("sig-xsa", "child %d stops with signal %d", pid, n);
    else
      break;  
  }
  

  signal(SIGCHLD,sigchld);
#endif  
}








/*
*/
static int init_nw(char *address, int port, SA_IN *servaddr)
{
  struct hostent *inf;
  int f_on=1;
  int s;
  
  s = socket(AF_INET, SOCK_STREAM, 0);

  if (s<0)       
    return 0;

  /* evite time-out sur certain OS comme BSD */
  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *) &f_on, sizeof(int));
  setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &f_on, sizeof(int));

  _BZERO(servaddr, SA_IN);
  servaddr->sin_family = AF_INET;
  servaddr->sin_port = htons((unsigned short)port);

  if (address==NULL || strlen(address)==0)
    servaddr->sin_addr.s_addr = htonl(INADDR_ANY);
  else
    servaddr->sin_addr.s_addr = inet_addr(address); 

  /* pas une "dotted", recherche sa DNS */
  if (servaddr->sin_addr.s_addr==INADDR_NONE)
  {
    inf = gethostbyname(address);

    /*  impossible de trouver la dotted, marre... */
    if (inf==NULL)
      return 0;

    memcpy( &servaddr->sin_addr.s_addr, inf->h_addr, inf->h_length );
  }

  return s;
}






/**
*/
static void set_sigsev()
{
  sigset_t nset, oset;

  sigemptyset (&nset);
  sigaddset(&nset, SIGSEGV);
  sigprocmask (SIG_UNBLOCK, &nset, &oset);
  signal(SIGSEGV, resume);
}







static int xsa_running1( int cnt)
{
  struct timeval tv;
  fd_set readfds, rset;
  int rd, nb, ret;
  char ok[10];

  SET_NONBLOCKING(cnt);
 
  /* wait and see... */
  while (1)
  {
    tv.tv_sec = 12;
    tv.tv_usec = 0;
  
    FD_ZERO(&rset);
    FD_SET(cnt, &rset);
 
    //readfds = rset;
    rd = select(FD_SETSIZE, &rset, NULL, NULL, &tv);
       
    if (rd!=0)
    {
      if (FD_ISSET(cnt ,&rset))
      {
        nb = recv(cnt, ok, 5, 0);
      
        if (nb<=0)
        {
          puts("aaaaaaaaaaaaaaaaaaaaaaaa");
          return 1;
        }  
        ok[nb]=0;  
        printf(">>>%d    %s", nb, ok);
      }
      else
        return 1;  
    }
    else
    {
      puts("wait");  
      fflush(stdout);
    }  
  }
}
       







/*
*
*/
int main(int argc, char *argv[]) 
{
  int sd, r, d=0, cnt, z;
  XSA *x;
  SA_IN servaddr;
  int si=SA_CHUNK_DATA;
  fd_set readfds, rmask;
  struct timeval tv;
  socklen_t ln=sizeof(int);
  int rf, c=0, f=0;
  char *psz, path_config[512], tmp[1024];
  xstr val=NULL, val1=NULL;
  S_CFG cfg;
  int fd;
  char version[64];
  struct sockaddr_in adresse;
  int taille=sizeof adresse;  
 
  /* creation socket */
  sd = init_nw("", 5600, &servaddr);

  /* TCP Window size */
  setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *) &si, sizeof(int));
  getsockopt(sd, SOL_SOCKET, SO_SNDBUF, &si, &ln);
  setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *) &si, sizeof(int));

  if (SET_BLOCKING(sd)<0)
  {
    log_puts("xsa", "O_NONBLOCK failed"); 
    exit(500);
  }

  /* assigne adresse+port */
  if ( bind(sd, (SA *) &servaddr, sizeof(servaddr))==-1 )
  {
    log_puts("xsa", "bind error...");
    exit(501);
  }


  /* socket passe en incoming... */
  r = listen( sd, 5);


  while(1)
  {
    cnt = accept(sd, &adresse, &taille);

    if (cnt<0)
    {
      if (errno != EINTR) 
      {
        perror("accept");
        close(sd);
        exit(EXIT_FAILURE);
      }
     }
       
     rf = fork();

     if (rf==0)
     {
       close(sd); 
       xsa_running1(cnt);
       close(cnt);
       break;
     }
  }

  close(sd);
  return EXIT_SUCCESS;
}

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]