2

我正在尝试为 tcpip 的异步 I/O 设置升压测试。

我发送 tcp 信息的功能:

    int TcpClient::sendMsgToServer(string msg) {
    if (isConnected == true) {
        Logger::debug("Asynch send request for msg: " + msg, __LINE__,
                __FILE__);
        sendSequence++;
        msg.append("\r\n");
        int length = msg.length() + 2; //+2 is for \r\n
        char* buffer = (char*) msg.c_str();
        Logger::debug(buffer, __LINE__, __FILE__);
        m_Socket.async_send(boost::asio::buffer(buffer, length),
                boost::bind(&fingigcc::TcpClient::sendMsgToServerErrorHandler,
                        this, boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred));
        std::string sentMsg = "Sent Msg: " + msg;
        Logger::debug(sentMsg, __LINE__, __FILE__);


        return 0;
    } else {
        return -1;
    }

}

    void TcpClient::sendMsgToServerErrorHandler(
            const boost::system::error_code& error, std::size_t bytes_transferred) {
        Logger::debug("Sending complete: ", __LINE__, __FILE__);
        sendResponseSequence++;
        int i = error.value();
        std::stringstream ss;
        ss.clear();
        ss << i;
        Logger::debug("RESULTCODE: " + ss.str(), __LINE__, __FILE__);
        if (i == 0) {
            Logger::debug("RESULT: " + error.message(), __LINE__, __FILE__);

        } else {
            Logger::crit(error.message(), __LINE__, __FILE__);
            m_Socket.close();
            isConnected = false;
            connectionStateChanged(isConnected);
        }

    }

现在,如果我只是在主线程中运行它(不运行增强测试),这个函数就可以正常工作

我的升压测试功能如下所示:

BOOST_AUTO_TEST_CASE( CommunicationCore_sendTcpIpMsg_test ) {

    CommunicationCoreFixture f;
    int compare = f.c->initializeTcpIpConnection(serverAddress, serverPort); // i initialize the connection here. runs fine with any issue
    sleep(2);
    compare = f.c->sendMsgToServer("IDENTIFY console-2");
    BOOST_MESSAGE("Sending returned value : " << compare);
    BOOST_CHECK(compare == 0);
}

它失败并出现以下错误:

    Entering test case "CommunicationCore_sendTcpIpMsg_test"
unknown location(0): fatal error in "CommunicationCore_sendTcpIpMsg_test": memory access violation at address: 0x01003255: no mapping at fault address
*****************Test is aborted

在测试此类异步功能时我应该注意什么?

我的构建信息是:

Platform: linux
Compiler: GNU C++ version 4.7.2
STL     : GNU libstdc++ version 20120920
Boost   : 1.49.0

编辑:

我也试过用以下方式修改它..但仍然得到同样的错误:

int TcpClient::sendMsgToServer(string msg) {
    if (isConnected == true) {
        Logger::debug("Asynch send request for msg: " + msg, __LINE__,
                __FILE__);
        sendSequence++;
        msg.append("\r\n");
    char *buffer = new char[msg.size() + 1];
    buffer[msg.size()] = 0;
    memcpy(buffer, msg.c_str(), msg.size());
    Logger::debug(buffer, __LINE__, __FILE__);

    m_Socket.async_send(boost::asio::buffer(buffer, (msg.size() + 1)),
            boost::bind(&fingigcc::TcpClient::sendMsgToServerErrorHandler,
                    this, boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));
        std::string sentMsg = "Sent Msg: " + msg;
        Logger::debug(sentMsg, __LINE__, __FILE__);


        return 0;
    } else {
        return -1;
    }

}
4

1 回答 1

3

m_Socket.async_send(boost::asio::buffer(buffer, length)-- 但是对象是本地的,超出范围后msgc_str()结果无效!msg因此,您实际上将临时缓冲区传递给异步操作。缓冲区不会超过此操作,因此段错误是不可避免的。

于 2013-02-02T22:00:34.800 回答