Win32的异步客户端传入的数据处理 [英] Win32 async client incoming data processing

查看:85
本文介绍了Win32的异步客户端传入的数据处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须写一个Win32异步客户端应用程序。应用将从其中有一个特定的形式的服务器接收数据。首先,这里是我的一片code的:

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 \\ n版本= 10.0.5 NavTech的公司(循环1403)\
layout=8\
Ls0(E)=CfgRego\
Ls1(E)=CfgSelcal\
Ls2(E)=CfgCoId\
Ls3(E)=CfgDragFf\\Ls4(E)=P71A\
Ls5(E)=P71B\
Qh57=0\
Ls6(E)=P71C\
Ls7(E)=P71D\
Ls8(E)=P71E

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

每个变量是由一个'\\ n'分隔。我需要的是中检索我需要的变量的值,例如,id的值(12在本例中),版本(10.0.5 NavTech的公司(周期1403))和Qh57(0)。所以,我首先需要找到如果缓冲区包含变量的标签(例如ID =)再拆,提取它位于'='和'\\ n'之间的数值。

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'.

我的问题是,我的code以上做工精细找到id和变量,它总是由服务器发送前两个变量,但是当我尝试中检索Qh57,这就是价值稍后发送,并在缓冲区内的不确定的位置上,我有时会收到一个疯狂的超值,喜欢-999999,或2,这是我觉得其他的服务器的价值派变量。我不知道该怎样和这事的想法(字符串?)。

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?).

有些帮助将是非常美联社preciated。

Some help would be very appreciated.

推荐答案

我想我找到了解决办法,但我希望有您的意见。据工作没有错误,但我不知道,不幸的是,一个变量(Qh57 = 1为例)到达2 FD_READ事件分割后会发生什么?

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;

这篇关于Win32的异步客户端传入的数据处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆