Win32异步客户端传入数据处理

时间:2015-08-05 08:29:22

标签: c++ asynchronous client

我必须编写一个Win32异步客户端应用程序。应用程序将从服务器接收具有特定表单的数据。首先,这是我的代码:

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;      

例如,服务器发送如下数据:

  

id = 12 \ nversion = 10.0.5 Navtech,Inc。(周期1403)\ nlayout = 8 \ nLs0(E)= CfgRego \ nLs1(E)= CfgSelcal \ nLs2(E)= CfgCoId \ nLs3(E) = CfgDragFf \ Ls4(E)= P71A \ nLs5(E)= P71B \ n Qh57 = 0 \ nLs6(E)= P71C \ nLs7(E)= P71D \ nLs8(E)= P71E < / p>

每个“变量”用'\ n'分隔。我需要的是检索我需要的变量的值,例如,id(本例中为12),版本(10.0.5 Navtech,Inc。(周期1403))和Qh57(0)的值。所以我首先需要找出缓冲区是否包含变量的“标签”(例如“id =”)然后拆分并提取位于'='和'\ n'之间的值。

我的问题是我上面的代码工作正常,找到“id”和“variable”,它们总是服务器发送的两个第一个变量,但是当我试图检索后来发送的Qh57的值时,并且在缓冲区内的一个未确定的位置,我有时得到一个疯狂的值,如-999999,或2,这是我认为其他服务器的值发送“变量”。我不知道怎么做(字符串?)。

非常感谢一些帮助。

1 个答案:

答案 0 :(得分:0)

我想我找到了解决方案,但我想得到你的建议。它工作没有错误,但我想知道如果不幸的是,变量(例如Qh57 = 1)在2个FD_READ事件中分裂会发生什么?

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;