title: Locating STX and ETX in a String in C++
body: <p>Is there a way to detect STX (Start of Text) and ETX (End of Text)  character sequences in a message received by recv() in c++? I'm trying fix any partial reads that may happen in TCP so that I can reform the complete message.</p>
  
  <p>Thank you!</p>
  
  <p>EDIT1:</p>
  
  <p>Did the following as per the answer by unwind:</p>
  
  <pre><code>char c = '\x02';
  if(memchr((*it).c_str(), c, numberOfCharactersRead) != NULL) 
      cout &lt;&lt; "STX found" &lt;&lt; endl;
  </code></pre>
  
  <p>Still I didn't manage to detect the character. Any issue with this implementation? 'it' is an iterator for a vector of the type string. </p>
  
  <p>EDIT2:</p>
  
  <p>This is the complete code for receiving data and checking for the character:</p>
  
  <pre><code>int numberOfCharactersRead = 0;
  
      do {
          char msgBuffer[1000];
          memset(msgBuffer, 0, 1000);
          numberOfCharactersRead = recv(clientSideSocket, msgBuffer, (1000 - 1), 0);
          if (numberOfCharactersRead &lt; 0) {
              close(clientSideSocket);
          }
          else if (numberOfCharactersRead == 0)
              close(clientSideSocket);
          else {
              char c = '\x02';
              if(memchr(msgBuffer, c, numberOfCharactersRead) != NULL) 
                  cout &lt;&lt; "STX found" &lt;&lt; endl;
              memset(msgBuffer, 0, 1000);
          }
  
      } while (numberOfCharactersRead &gt; 0); 
  </code></pre>
  
  <p>So, I'm simply checking for the STX, not yet concatenating the buffered data. However, the check for STX is still failing. Kindly let me know any issue in this approach.</p>
  
  <p>Thanks.</p>
  
  <p>EDIT3:</p>
  
  <p>I got the following hex value for a sample message:</p>
  
  <pre><code>3C 31 34 32 3E 4F 63 74 20 32 35 20 31 31 3A 33 39 3A 31 32 20 6C 6F 63 61 6C 68 6F 73 74 20 5B 20 32 30 31 32 2D 4F 63 74 2D 32 35 20 31 31 3A 33 39 3A 31 32 2C 36 31 33 20 5D 20 20 20 20 49 4E 46 4F 20 7B 20 4D 61 69 6E 2E 6A 61 76 61 20 7D 20 2D 20 74 65 73 74 20 69 6E 66 6F 72 6D 61 74 69 6F 6E 20 6D 65 73 73 61 67 65 20
  </code></pre>
  
  <p>So the hex value for the STX/ETX is not present. That mean can't use STX and ETX for checking the message formation.</p>