Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello perfect programmer

i have 2 services one in java and other in c++ both act as client and server in exchange data but after 1 or 2 hours the service in c++ stop abnormally due to CLOSE_WAIT status OF TCP Connection increase on unix it multi thread application
str_write variable is BufferedOutputStream get from socket from c++ service and return to it
hint : this two section are shared between all threads in every service
Java
 str_write.write("send o c++ service @".getBytes(), 0, 10);
str_write.flush();
Thread.sleep(3000L);
str_write.close();
this.socket.close();
finalize();
return;

and c++ service is receive data like
C++
  int l;
char delem = "@";
     timeval t_out;

         fd_set set;
   
     
       t_out.tv_sec = 6;
      
      t_out.tv_usec = 0;
      FD_ZERO(&set);   
      FD_SET(Socket,&set);
  
  l = 8;    
        l = select(Socket +1, &set, NULL, NULL, &t_out);
      
         setsockopt(Socket,SOL_SOCKET,TCP_NODELAY|SO_RCVTIMEO,(char*)&t_out,sizeof(struct timeval));

       if(l==0)
         {
          printf("\n time out kk %i \n",timeout);
          return -10;
         }
       
         else if(len<0)
          {
         printf("\n error in select 2 TCPClient \n");
       
           return -5;
          }
         
     char a[1] = {0};
    
       memset(data,0,sizeof(data));
      // strcpy(data," ");   
     ssize_t l1;
     int i = 0;
     int cycle = 0;
     while(true)
     {
     
          l1 = recv(Socket,a,1,0);
          
          if(l1==0)
          {
          printf("\n closed ");
          i = 0;
            break;
          }
          if(l1 == -1)
          {
            cycle++;
            if(r==5 &&cycle<6)
             {
                sleep(1);
                printf("wait");
             
                    continue;
             }
             
            break;
          }
          data =  stpcpy(data,a);
          i +=l1;
         
          if(delm_len>1 &&strstr(data,delem)!=NULL)
          {
            printf("\n hit strstr \n  ");
            break;
          }
            else if(delm_len==1 && delem[0]==a[0])
            { 
            r = 1;
            printf("\n hit \n");
                break;
            }
            if(i>=len && r==1)
            break;
            
      }//while(i< len) ;    
       printf("\n data recv %i  r = %i ",i,r);      


What I have tried:

every thing try reduce fail not prevent
Posted
Updated 16-Nov-16 6:53am
v2

1 solution

CLOSE_WAIT is an indication that the connection has timed out. Here are some nice point to this issue.

Loosing the connection is a common scenario, so your program logic should always have some reconnection logic build in. So best practice is to open a connection when needed, do the work and than close and wait or poll for a new job.

meta-code:
C++
while( flagServiceRunning ) {
 if( newJobAvailable() ) {
   if( connect() ) {
     if( processJob() ) {
       markJobAsDone();
     }
     disconnect();
   }
 }
 sleep();//some energy saving state, for a well choosen time
}
 
Share this answer
 
Comments
Mahmoud_Gamal 16-Nov-16 13:34pm    
are you mean set timeout for every thread process

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900