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.
Thank you!
EDIT1:
Did the following as per the answer by unwind:
char c = '\x02';
if(memchr((*it).c_str(), c, numberOfCharactersRead) != NULL)
cout << "STX found" << endl;
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.
EDIT2:
This is the complete code for receiving data and checking for the character:
int numberOfCharactersRead = 0;
do {
char msgBuffer[1000];
memset(msgBuffer, 0, 1000);
numberOfCharactersRead = recv(clientSideSocket, msgBuffer, (1000 - 1), 0);
if (numberOfCharactersRead < 0) {
close(clientSideSocket);
}
else if (numberOfCharactersRead == 0)
close(clientSideSocket);
else {
char c = '\x02';
if(memchr(msgBuffer, c, numberOfCharactersRead) != NULL)
cout << "STX found" << endl;
memset(msgBuffer, 0, 1000);
}
} while (numberOfCharactersRead > 0);
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.
Thanks.
EDIT3:
I got the following hex value for a sample message:
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
So the hex value for the STX/ETX is not present. That mean can't use STX and ETX for checking the message formation.