title: python socket send message but only one time can retrieve
body: <p>I have one python socket client example which I want to connect to an socket server and send command to get result. When I used following client python
  code, only first command will be successful returned, from the 2nd command, return data length always is 0.</p>
  
  <p>Here is my call process: I will first import following python class, and declare it as <code>cu = command_util(ip,port)</code>, use <code>cu.execute_command("cmd_name")</code> to execute. In my application, I will run more than 50 times <code>execute_command()</code>.</p>
  
  <p>Previously, I created new socket class and connect first, then use <code>send()</code> command, but when so many commands will be executed, some of the results will be lost. That's the reason I need change to following code which may prevent so many commands.</p>
  
  <p>current design python sample:</p>
  
  <pre><code>import sys
  from socket import *
  import time
  import re
  
  
  class CommandUtil:
      def __init__(self, _host_ip='127.0.0.1', _host_port=2888, _buf_size=4096):
          self._host_ip = _host_ip
          self._host_port = _host_port
          self._buf_size = _buf_size
          _addr = (_host_ip, int(_host_port))
          self._client = socket(AF_INET, SOCK_STREAM)
          self._client.connect(_addr)
  
      def __del__(self):
          print "enter into __del__"
          self._client.close()
  
      def execute_command(self, _cmd_str):
          self._client.send(_cmd_str)
          _result = ""
          data = self._client.recv(self._buf_size)
          # debug
          print ("data length=%s " % len(data))
          data_again = ''
          if len(data) &lt; 5 :
              data_again = self._client.recv(self._buf_size)
              print ("data length again=%s " % len(data_again))
          data += data_again
          _result += data[4:]
          if not data:
              print("anything wrong for set %s ?", _cmd_str)
          while len(data) - self._buf_size &gt;= 0:
              # still need to receive data
              data = self._client.recv(self._buf_size)
              _result += data
          return _result
  </code></pre>
  
  <p>Previously sample code which connect each time for each command:</p>
  
  <pre><code>def execute_command():
      _addr = (_host, int(_port))
      _client = socket(AF_INET, SOCK_STREAM)
      _client.connect(_addr)
      _client.send(_cmd_str)
      data = _client.recv(_buf_size)
      # print ("data length=%s " % len(data))
      data_again = ''
      if len(data) &lt; 5:
          data_again = _client.recv(_buf_size)
          # print ("data length again=%s " % len(data_again))
      data += data_again
      _result += data[4:]
      if not data:
          print("anything wrong for set %s", _cmd_str)
      while len(data) - _buf_size &gt;= 0:
          # still need to receive data
          data = _client.recv(_buf_size)
          _result += data
      _client.close()
      # time.sleep(1)
      return _result
  </code></pre>
  
  <p>here is the server code in c++:</p>
  
  <pre><code>struct sockaddr_in ServerAddr;
  bzero(&amp;ServerAddr,sizeof(ServerAddr));
  ServerAddr.sin_family = AF_INET;
  ServerAddr.sin_addr.s_addr = htons(INADDR_ANY);
  ServerAddr.sin_port = htons(SERVER_PORT);
  
  int ServerSocket = socket(PF_INET,SOCK_STREAM,0);
  int opt =1;
  setsockopt(ServerSocket,SOL_SOCKET,SO_REUSEADDR,&amp;opt,sizeof(opt));
  bind(ServerSocket,(struct sockaddr*)&amp;ServerAddr,sizeof(ServerAddr)
  listen(ServerSocket, MAX_LISTEN_QUEUE)
  
  char ReqBuff[BUFFER_SIZE];
  char RspBuff[BUFFER_SIZE];
  while (true) {  #&lt;&lt;&lt;---- Server holder init here !
      struct sockaddr_in ClientAddr;
      socklen_t len = sizeof(ClientAddr);
  
      int SessionSocket = accept(ServerSocket,(struct sockaddr*)&amp;ClientAddr,&amp;len);
  
      bzero(ReqBuff, BUFFER_SIZE);
      int length = recv(SessionSocket,ReqBuff,BUFFER_SIZE,0);
      if (length &gt; 0) {
          ReqBuff[length] = '\0';
          std::string Rsp = execcommd()
          if (Rsp.length() == 0) {
              Rsp = "\n";
          }
          bzero(RspBuff, BUFFER_SIZE);
          *((uint32_t *)RspBuff) = Rsp.length();
          send(SessionSocket,RspBuff,sizeof(uint32_t),0)
  
          uint32_t Pos = 0;
          do {
              uint32_t Len = ((length - Pos) &gt; BUFFER_SIZE) ? BUFFER_SIZE : (length - Pos);
              bzero(RspBuff, BUFFER_SIZE);
              memcpy(RspBuff, Rsp.c_str()+Pos, Len);
              if(send(SessionSocket,RspBuff,Len,0)&lt;0) {
                  std::cout &lt;&lt; "Send Response Content Fail" &lt;&lt; std::endl;
                  break;
              }
              Pos += BUFFER_SIZE;
          } while (length &gt; Pos);
      }
      close(SessionSocket);#&lt;&lt;&lt;---- You close whole server not client socket
  }
  close(ServerSocket);
  </code></pre>