如何优化客户端和服务器之间的TCP连接

问题描述:

你好完美的程序员



i有两个服务,一个是java,另一个是c ++,它们在交换数据中充当客户端和服务器但是在1或2小时后服务c ++由于CLOSE_WAIT状态异常停止因为TCP连接在unix上增加了多线程应用程序

str_write变量是BufferedOutputStream从c ++服务获取套接字并返回它

提示:这每个服务中的所有线程共享两个部分

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

 str_write.write("send o c++ service @".getBytes(), 0, 10);
str_write.flush();
Thread.sleep(3000L);
str_write.close();
this.socket.close();
finalize();
return;



和c ++服务接收数据如


and c++ service is receive data like

  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

CLOSE_WAIT表示连接超时。以下是问题的一些不错的观点。



松开连接是常见场景,所以你的程序逻辑应该总是有一些重新连接逻辑。所以最好的做法是在打开连接时需要,做好工作,然后关闭,等待或轮询新工作。



元代码:

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:
while( flagServiceRunning ) {
 if( newJobAvailable() ) {
   if( connect() ) {
     if( processJob() ) {
       markJobAsDone();
     }
     disconnect();
   }
 }
 sleep();//some energy saving state, for a well choosen time
}