title: Select() implemented wrong in program, getting timeouts from server
body: <p>A third question in the saga: <a href="https://stackoverflow.com/questions/66775899/">How to correctly implement select to correctly get data from stdin and recv()</a>. I recommend reading this and the other question it links to understand the situation.</p>
  <p>Basically, I tried my luck at implementing <code>select()</code> myself. My code:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/select.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;cstring&gt;
  #include &lt;stdlib.h&gt;
  
  using namespace std;
  
  int main (int argc, char** argv) {
      if (argv[1] == NULL) {
          cout &lt;&lt; &quot;\033[31mTARGET NOT SPECIFIED - TERMINATING...\033[0m\n&quot;;
          return -1;
      }
      if (argv[2] == NULL) {
          cout &lt;&lt; &quot;\033[31mPORT NOT SPECIFIED - TERMINATING...\033[0m\n&quot;;
          return -2;
      }
      
      string target = argv[1];
      int port = atoi(argv[2]);
  
      cout &lt;&lt; &quot;GENERATING SOCKET...\n&quot;;
      int chatter = socket(AF_INET, SOCK_STREAM, 0);
      if (chatter == -1) {
          cout &lt;&lt; &quot;\033[31mSOCKET GENERATION FAILURE - TERMINATING...\033[0m\n&quot;;
          return -3;
      }
      cout &lt;&lt; &quot;\033[32mSUCCESSFULLY GENERATED SOCKET\033[0m\n&quot;;
  
      struct sockaddr_in hint;
      hint.sin_family = AF_INET;
      hint.sin_port   = htons(port);
      inet_pton(AF_INET, target.c_str(), &amp;hint.sin_addr);
  
      struct timeval tv;
      tv.tv_usec = 0.0;
      tv.tv_sec = 5;
      int recval;
  
      cout &lt;&lt; &quot;CONNECTING TO &quot; &lt;&lt; target &lt;&lt; &quot; AT PORT &quot; &lt;&lt; port &lt;&lt; &quot;...\n&quot;;   
      int connection_status = connect(chatter, (sockaddr*)&amp;hint, sizeof(hint));
      if (connection_status == -1) {
          cout &lt;&lt; &quot;\033[31mCONNECTION FAILURE - TERMINATING...\033[0m\n&quot;;
          return -4;
      }
      cout &lt;&lt; &quot;\033[32mCONNECTED TO HOST\033[0m\n&quot;;
  
      char buf[4096] = {0};
      string msg;
      while (true) {
          fd_set rfds;
          FD_ZERO(&amp;rfds);
          FD_SET(chatter, &amp;rfds);
  
          getline(cin, msg);
          msg+&quot;\r\n&quot;;
          int sendmsg = send(chatter, msg.c_str(), msg.size()+1, 0);
          if (sendmsg == -1) {
              cout &lt;&lt; &quot;\033[31mMESSAGE SENDING FAILURE - TERMINATING...\033[0m\n&quot;;
              return -5;
          }
  
          recval = select(chatter + 1, &amp;rfds, NULL, NULL, &amp;tv);
          switch(recval) {
              case(0):
                  cout &lt;&lt; &quot;\033[31mTIMEOUT\033[0m\n&quot;;
                  break;
              case(-1):
                  cout &lt;&lt; &quot;\033[31mERROR\033[0m\n&quot;;
                  break;
              default:
                  if (recv(chatter, buf, 4096, 0) &lt; 0) {
                      cout &lt;&lt; &quot;\033[31mFAILURE TO RECEIVE MESSAGE - TERMINATING...\033[0m\n&quot;;
                      return -6;
                  } else {
                      cout &lt;&lt; recv(chatter, buf, 4096, 0) &lt;&lt; &quot;\n&quot;;
                      cout &lt;&lt; buf &lt;&lt; &quot;\n&quot;;
                  }
                  break;
          }
      }
  
      close(chatter);
  
      return 0;
  }
  </code></pre>
  <p>I keep getting <code>TIMEOUT</code> when trying the program on <code>scanme.nmap.org</code> and my HTTP server. What am I doing wrong?</p>
  <p>At this point, after fixing something a user in the first question pointed out, I know that there isn't an issue with how I'm sending data. Just an issue with the way the program handles getting data from <code>getline()</code>/<code>recv()</code>.</p>
  <p><strong>EDIT: NEW, IMPROVED, WORKING CODE THANKS TO ANSWERER</strong></p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/select.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;cstring&gt;
  #include &lt;stdlib.h&gt;
  
  using namespace std;
  
  int main (int argc, char** argv) {
      if (argv[1] == NULL) {
          cout &lt;&lt; &quot;\033[31mTARGET NOT SPECIFIED - TERMINATING...\033[0m\n&quot;;
          return -1;
      }
      if (argv[2] == NULL) {
          cout &lt;&lt; &quot;\033[31mPORT NOT SPECIFIED - TERMINATING...\033[0m\n&quot;;
          return -2;
      }
      
      string target = argv[1];
      int port = atoi(argv[2]);
  
      cout &lt;&lt; &quot;GENERATING SOCKET...\n&quot;;
      int chatter = socket(AF_INET, SOCK_STREAM, 0);
      if (chatter == -1) {
          cout &lt;&lt; &quot;\033[31mSOCKET GENERATION FAILURE - TERMINATING...\033[0m\n&quot;;
          return -3;
      }
      cout &lt;&lt; &quot;\033[32mSUCCESSFULLY GENERATED SOCKET\033[0m\n&quot;;
  
      struct sockaddr_in hint;
      hint.sin_family = AF_INET;
      hint.sin_port   = htons(port);
      inet_pton(AF_INET, target.c_str(), &amp;hint.sin_addr);
  
      int recval;
  
      cout &lt;&lt; &quot;CONNECTING TO &quot; &lt;&lt; target &lt;&lt; &quot; AT PORT &quot; &lt;&lt; port &lt;&lt; &quot;...\n&quot;;   
      int connection_status = connect(chatter, (sockaddr*)&amp;hint, sizeof(hint));
      if (connection_status == -1) {
          cout &lt;&lt; &quot;\033[31mCONNECTION FAILURE - TERMINATING...\033[0m\n&quot;;
          return -4;
      }
      cout &lt;&lt; &quot;\033[32mCONNECTED TO HOST\033[0m\n&quot;;
  
      char buf[4096] = {0};
      string msg;
      while (true) {
          struct timeval tv;
          tv.tv_usec = 0.0;
          tv.tv_sec = 5;
  
          fd_set rfds;
          FD_ZERO(&amp;rfds);
          FD_SET(chatter, &amp;rfds);
  
          getline(cin, msg);
          msg += &quot;\r\n&quot;;
          const char *pMsg = msg.c_str();
          size_t msgSize = msg.size();
          do {
              int numSent = send(chatter, pMsg, msgSize, 0);
              if (numSent == -1) {
                  cout &lt;&lt; &quot;\033[31mMESSAGE SENDING FAILURE - TERMINATING...\033[0m\n&quot;;
                  close(chatter);
                  return -5;
              }
              pMsg += numSent;
              msgSize -= numSent;
          } while (msgSize &gt; 0);
  
          recval = select(chatter + 1, &amp;rfds, NULL, NULL, &amp;tv);
          switch(recval) {
              case(0):
                  cout &lt;&lt; &quot;\033[31mTIMEOUT\033[0m\n&quot;;
                  break;
              case(-1):
                  cout &lt;&lt; &quot;\033[31mERROR\033[0m\n&quot;;
                  break;
              default:
                  int numRead = recv(chatter, buf, 4096, 0);
                  if (numRead &lt; 0) {
                      cout &lt;&lt; &quot;\033[31mFAILURE TO RECEIVE MESSAGE - TERMINATING...\033[0m\n&quot;;
                      close(chatter);
                      return -6;
                  }
                  else if (numRead == 0) {
                      cout &lt;&lt; &quot;\033[31mDISCONNECTED - TERMINATING...\033[0m\n&quot;;
                      close(chatter);
                      break;
                  } else {
                      cout &lt;&lt; numRead &lt;&lt; &quot;\n&quot;;
                      cout.write(buf, numRead);
                      cout &lt;&lt; &quot;\n&quot;;
                  }
                  break;
          }
      }
  
      close(chatter);
  
      return 0;
  }
  </code></pre>