问题
I have to write a Win32 async client application. The application will receive data from the server which have a specific form. First, here is my piece of code :
case FD_READ:
{
char *pBuffer = nullptr;
char *next_token = nullptr;
recv(Socket, readBuffer, sizeof(readBuffer), 0); // char readBuffer[5000]
pBuffer = readBuffer;
if(strstr(pBuffer, "id=") != NULL) // Looking for variable starting by "id="
{
char *label = strtok_s(pBuffer, "=", &next_token);
char *pId = strtok_s(NULL, "\n", &next_token); // Take the value (after "=")
pBuffer = strtok_s(NULL, "", &next_token);
strcpy_s(id, pId); // Copy the value to a variable for later use
}
if( strstr(pBuffer, "version") != NULL)
{
char *label = strtok_s(pBuffer, "=", &next_token);
char *pVersion = strtok_s(NULL, "\n", &next_token);
pBuffer = strtok_s(NULL, "", &next_token);
strcpy_s(version, pVersion);
}
if( strstr(pBuffer, "Qh57=") != NULL)
{
char *label = strtok_s(pBuffer, "=", &next_token);
char *pFoFd = strtok_s(NULL, "\n", &next_token);
pBuffer = strtok_s(NULL, "", &next_token);
strcpy_s(foFd, pFoFd);
}
// So on for many variables
}
break;
For example, the server sends data like this:
id=12\nversion=10.0.5 Navtech, Inc. (cycle 1403)\nlayout=8\nLs0(E)=CfgRego\nLs1(E)=CfgSelcal\nLs2(E)=CfgCoId\nLs3(E)=CfgDragFf\Ls4(E)=P71A\nLs5(E)=P71B\nQh57=0\nLs6(E)=P71C\nLs7(E)=P71D\nLs8(E)=P71E
Each "variable" is separated by a '\n'. What I need is to retreive the value of the variables I need, for example, the value of id (12 in this example), version (10.0.5 Navtech, Inc. (cycle 1403)) and Qh57 (0). So I first need to find if the buffer contains the "label" of the variable (for example "id=") then split and extract the value which is located between the '=' and the '\n'.
My problem is that my code above is working fine to find "id" and "variable" which are always the two first variables sent by the server, but when I try to retreive the value of Qh57, which is sent later, and at an undetermined position within the buffer, I sometimes get a crazy value, like -999999, or 2, which is I think the value of other server sent "variables". I have no idea of how to do with this (string?).
Some help would be very appreciated.
回答1:
I think I found a solution, but I'd like to have your advise. It is working without error, but I wonder what will happen if, unfortunately, a variable (Qh57=1 for example) arrives splitted in 2 FD_READ events ?
case FD_READ:
{
string sReadBuffer;
string var;
char *pVar = nullptr;
char *next_token = nullptr;
int bytes_recv = recv(Socket, readBuffer, sizeof(readBuffer), 0);
sReadBuffer = readBuffer; // Copy the buffer to string
istringstream iss(sReadBuffer); // Put into a stream
while (getline(iss, var)) // Default delimiter '\n'
{
pVar = _strdup(var.c_str()); // Cast string to char *
if(strstr(pVar, "id=") != NULL)
{
char *label = strtok_s(pVar, "=", &next_token);
char *pId = strtok_s(NULL, "\n", &next_token);
strcpy_s(id, pId);
}
if( strstr(pVar, "version") != NULL)
{
char *label = strtok_s(pVar, "=", &next_token);
char *pVersion = strtok_s(NULL, "\n", &next_token);
strcpy_s(version, pVersion);
}
if( strstr(pVar, "Qh57=") != NULL)
{
char *label = strtok_s(pVar, "=", &next_token);
char *pFoFd = strtok_s(NULL, "\n", &next_token);
strcpy_s(foFd, pFoFd);
appendTextToEdit(hDebug, "Qh57=");
appendTextToEdit(hDebug, foFd);
appendTextToEdit(hDebug, "\n");
}
}
}
break;
来源:https://stackoverflow.com/questions/31827266/win32-async-client-incoming-data-processing