C ++ <policy-file-request> </policy-file-request>

时间:2013-06-13 12:16:15

标签: c++ sockets flash

希望这将是我最后一个与套接字相关的C ++问题。 我有一个.SWF文件,它发送一个策略文件请求。

我检查我的传入数据char 0是否为&lt;,如下所示:

if (raw[0] == '<')

然后我发送我的政策:

            send(this->s, Env::Policy().c_str(), sizeof(Env::Policy()), 0);

            std::cout << "Sent " << Env::Policy().c_str() << std::endl;
            running = false;
            closesocket(this->s);
            break;

中断;将停止while(this-&gt; running)循环。 我的政策字符串:

std::string Env::Policy()
{
    char c = 0;

    return "<?xml version=\"1.0\"?>\r\n<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">\r\n<cross-domain-policy>\r\n<allow-access-from domain=\"*\" to-ports=\"1-31111\" />\r\n</cross-domain-policy>" + c;
}

但是每次发送它都没有任何反应。套接字不会收到新连接(如在C#套接字服务器中)。但是当我重新连接.swf时,它会接受一个新的连接。 发生了什么事?

我的完整时间(此 - >运行)循环:

while (running)
{
    char c[256];

    int bits = recv(s, c, sizeof(c), 0);

    if (bits > 0)
    {
        std::string data = c;
        std::string raw = data.substr(0, bits);

        std::cout << "First char: " << raw[0] << std::endl;

        if (raw[0] == '<')
        {
            send(this->s, Env::Policy().c_str(), sizeof(Env::Policy()), 0);

            std::cout << "Sent " << Env::Policy().c_str() << std::endl;
            running = false;
            closesocket(this->s);
            break;
        }

        int header = Env::B64Decode(raw.substr(3, 2));

        switch (header)
        {
        case 202:
            this->msg = new ServerMessage("DA");
            this->msg->AddInt32(6);
            this->msg->AddInt32(0);
            this->msg->AddInt32(1);
            this->msg->AddInt32(1);
            this->msg->AddInt32(1);
            this->msg->AddInt32(3);
            this->msg->AddInt32(0);
            this->msg->AddInt32(2);
            this->msg->AddInt32(1);
            this->msg->AddInt32(4);
            this->msg->AddInt32(1);
            this->msg->AddString("dd-MM-yyyy");
            this->msg->AddChar(2);
            this->sendData(this->msg->toString());

            this->msg = new ServerMessage("@H");
            this->msg->AddString("[100,105,110,115,120,125,130,135,140,145,150,155,160,165,170,175,176,177,178,180,185,190,195,200,205,206,207,210,215,220,225,230,235,240,245,250,255,260,265,266,267,270,275,280,281,285,290,295,300,305,500,505,510,515,520,525,530,535,540,545,550,555,565,570,575,580,585,590,595,596,600,605,610,615,620,625,626,627,630,635,640,645,650,655,660,665,667,669,670,675,680,685,690,695,696,700,705,710,715,720,725,730,735,740]");
            this->msg->AddChar(2);
            this->sendData(this->msg->toString());
            break;
        default:
            std::cout << "Unregistered header " << header << std::endl;
            break;
        }
    }
    else
    {
        break;
    }
}

1 个答案:

答案 0 :(得分:1)

 std::string data = c;

只有在字符串肯定是0终止

时才有用
 std::string raw = data.substr(0, bits);

你可以做得那么简单

 const std::string raw(c, c+bits);

在您的保单功能中,没有任何理由可以使用charc,但如果它的值> 0,则可能会导致问题。

最重要的是,发送sizeof(Env::Policy())字节完全没有意义,你应该发送整个字符串!

const auto& policy = Env::Policy();
send(this->s, policy, policy.size() + 1, 0);

可能没有+1,具体取决于你是否想要0。