First time using select(), maybe a basic question?

Posted by darkletter on Stack Overflow See other posts from Stack Overflow or by darkletter
Published on 2010-06-10T15:49:59Z Indexed on 2010/06/10 15:52 UTC
Read the original article Hit count: 215

Filed under:
|
|
|

Hello people, i've been working for a few days with this server using select(). What it does, is that, i have two arrays of clients (one is "suppliers", and the other is "consumers"), and the mission of the server is to check whether the suppliers have something to send to the consumers, and in case affirmative, send it.

The second part of the server is that, when the consumers have received the suppliers' info, they send a confirmation message to the same suppliers that sent the info.

When a client connects, it gets recognized as "undefined", until it sends a message with the word "supplier" or "consumer" (in Spanish, as i'm from there), when the server puts it in the correct clients array.

Well, what the server does is not very important here. What's important is that, i'm doing both parts with two different "for" loops, and that's where i'm getting the problems. When the first user connects to the server (be it a supplier or a consumer), the server gets stuck in the first or second loop, instead of just continuing its execution. As it's the first time i'm using select(), i may be missing something. Could you guys give me any sort of help?

Thanks a lot in advance.

for(;;)
{

  rset=allset;

  nready=select(maxfd+1,&rset,NULL,NULL,NULL);


  if (FD_ISSET(sockfd, &rset)){

    clilen=sizeof(cliente);

    if((connfd=accept(sockfd,(struct sockaddr *)&cliente,&clilen))<0) printf("Error");

    IP=inet_ntoa(cliente.sin_addr);




    for(i=0;i<COLA;i++){if(indef[i]<0){indef[i]=connfd;IPind[i]=IP;break;}}



    FD_SET(connfd,&allset);     
    if(connfd > maxfd)
    maxfd=connfd;
    if(i>maxii) maxii=i;

    if(--nready<=0) continue;


  }// Fin ISSET(sockfd)



for(i=0;i<=maxii;i++){
    if((sockfd1=indef[i])<0){ continue;} //!

    if(FD_ISSET(sockfd1,&rset)){
      if((n=read(sockfd1,comp,MAXLINE))==0){close(sockfd1);FD_CLR(sockfd1,&allset);indef[i]=-1;printf("Cliente indefinido desconectado \n");}

      else{
            comp[n]='\0';



        if(strcmp(comp,"suministrador")==0){
          for(j=0;j<=limite;j++){if(sumi[j]<0){IPsum[j]=IPind[i];sumi[j]=indef[i]; indef[i]=-1;if(j>maxis) {maxis=j;}break; } }
        }

        else if(strcmp(comp,"consumidor")==0){
          for(o=0;j<=limite;j++){if(consum[o]<0){IPcons[o]=IPind[i];consum[o]=indef[i]; indef[o]=-1;if(o>maxic) {maxic=o;}break; }
            }
        }

        if(--nready <=0)break;
      }

    }





}//fin bucle for maxii


    for(i=0;i<=maxis;i++){
      if((sockfd2=sumi[i])<0){continue;}
       if(FD_ISSET(sockfd2,&rset)){

    if((n=read(sockfd2,buffer2,MAXLINE))==0){close(sockfd2);FD_CLR(sockfd2,&allset);sumi[i]=-1;printf("Suministrador desconectado \n");}

    else{
      buffer2[n]='\0';
        for(j=0;j<=maxic;j++){
          if((sockfd3=consum[j])<0){ continue;}
          else {strcpy(final,IPsum[i]);strcat(final,":");strcat(final,buffer2);write(sockfd3,final,sizeof(final));respuesta[i]=1;}

        }

        break; // ?
    }



    }
    }//fin for maxis







for(i=miniic;i<=maxic;i++){

  if((sockfd4=consum[i])<0){continue;}
  if(FD_ISSET(sockfd4,&rset)){

  if((n=read(sockfd4,buffer3,MAXLINE))==0){close(sockfd4);FD_CLR(sockfd4,&allset);consum[i]=-1;printf("Consumidor desconectado \n");}

  else{
    buffer3[n]='\0';
    IP2=strtok(buffer3,":");
    obj=strtok(NULL,":");

    for(j=0;j<100;j++){

      if((strcmp(IPsum[j],IP2)==0) && (respuesta[j]==1)) {write(sumi[j],obj,sizeof(obj)); miniic=i+1; respuesta[j]=0;
       break;                            
      }

    }


  }




  }

}

© Stack Overflow or respective owner

Related posts about c

    Related posts about sockets