Visual c++ 在我关闭应用程序之前,不会调用handle\u write 我使用Boost ASIO在Windows环境下在java和C++应用程序之间进行IPC。我现在在C++方面做一些简单的测试,现在在“LoalHoalver”服务器中向端口100写入数据。这是我的密码: int _tmain(int argc, _TCHAR* argv[]) { boost::asio::io_service ioService; TCPClient client(ioService, "localhost", "100"); ioService.run(); client.Write("Heloo!!!"); getchar(); }

Visual c++ 在我关闭应用程序之前,不会调用handle\u write 我使用Boost ASIO在Windows环境下在java和C++应用程序之间进行IPC。我现在在C++方面做一些简单的测试,现在在“LoalHoalver”服务器中向端口100写入数据。这是我的密码: int _tmain(int argc, _TCHAR* argv[]) { boost::asio::io_service ioService; TCPClient client(ioService, "localhost", "100"); ioService.run(); client.Write("Heloo!!!"); getchar(); },visual-c++,boost-asio,Visual C++,Boost Asio,和TCPClientclass: #include <iostream> #include <stdio.h> #include <ostream> #include <boost/bind.hpp> #include <boost/asio.hpp> using namespace std; using boost::asio::ip::tcp; class TCPClient { public: // Construc

TCPClient
class:

#include <iostream>
#include <stdio.h>
#include <ostream>

#include <boost/bind.hpp>
#include <boost/asio.hpp>

using namespace std;
using boost::asio::ip::tcp;

class TCPClient {
public:
    // Constructor
    TCPClient::TCPClient(boost::asio::io_service& IO_Service, const std::string& server, const std::string& port)
        : resolver_(IO_Service), socket_(IO_Service){
        connected = false;

        // Query donde defino nombre del servidor y puerto
        tcp::resolver::query query(server, port);

        resolver_.async_resolve(query,
            boost::bind(&TCPClient::handle_resolve, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::iterator));
    }

private:
    tcp::resolver   resolver_;
    tcp::socket     socket_;
    bool connected;

public:
    void Write(const std::string& data){
        size_t s = 8;

        boost::asio::async_write(   socket_,
                                    boost::asio::buffer(data, s),
                                    boost::bind(&TCPClient::handle_write,
                                    this, boost::asio::placeholders::error,
                                    boost::asio::placeholders::bytes_transferred)
                                );
        std::cout << "async_write executed" << std::endl;
    }

private:
    // Resolucion de la direccion del servidor
    void handle_resolve(const boost::system::error_code& err, tcp::resolver::iterator endpoint_iterator){
        if (!err){
            // Intentamos conectarnos al primer endpoint en la lista
            tcp::endpoint endpoint = *endpoint_iterator;
            socket_.async_connect(  endpoint,
                                    boost::bind(&TCPClient::handle_connect, this,
                                    boost::asio::placeholders::error, ++endpoint_iterator));
        }
        else{
            std::cout << "Error: " << err.message() << std::endl;
        }
    }

    // Conexion al servidor
    void handle_connect(const boost::system::error_code& err, tcp::resolver::iterator endpoint_iterator){
        if (!err){
            connected = true;
            std::cout << "Connected" << std::endl;
        }
        else if (endpoint_iterator != tcp::resolver::iterator()){
            // Conexion fallida, intentemos con el siguiente endpoint
            socket_.close();
            tcp::endpoint endpoint = *endpoint_iterator;

            socket_.async_connect(  endpoint,
                                    boost::bind(&TCPClient::handle_connect, this,
                                    boost::asio::placeholders::error, ++endpoint_iterator));
        }
        else{
            std::cout << "Error: " << err.message() << std::endl;
        }
    }

    void handle_write(const boost::system::error_code& error, std::size_t bytes_transferred){
        if (!error){
            std::cout << "Handle_write() sent bytes" << bytes_transferred << std::endl;
        }
        else {
            std::cout << "Handle_write() error:" << error << std::endl;
        }
    }


};
我重新启动服务以便发送数据,现在我看到正在调用
handle\u write
,但在关闭应用程序之前,我仍然无法看到传入的数据:

简言之,当返回时,
io\u服务已无法工作。尽管
TCPClient::write()
io\u服务
添加了更多的工作,但是
io\u服务
的事件循环永远不会再被服务,导致
TCPClient::handle\u write()
永远不会被调用。考虑一下:

  • 重置
    io\u服务
    ,使其可以通过再次调用
    io\u服务::run()
  • 通过保证每个处理程序向
    io_服务
    添加额外的工作,或构造一个对象,重新构造程序,使
    io_服务
    永远不会耗尽工作
<> >加深理解<代码> IoService::Run()<代码>,考虑阅读答案。

此外,程序调用未定义的行为,因为它无法满足的
缓冲区
参数的生存期要求:

底层内存块的所有权由调用者保留,调用者必须保证它们在调用处理程序之前保持有效


调用
client.write(“Heloo!!!”)
时,将构造一个临时
std::string
,并将其传递给
TCPClient::write()
。然后,临时
字符串
用作
异步写入()的
缓冲区
参数。由于
TCPClient::write()
不保证在调用
async_write()
完成处理程序之前它不会返回,因此临时进程的生存期违反了
async_write()
的要求,导致未定义的行为。

io_service::run()
是一个阻塞函数。尽管如此,我的端口侦听器程序似乎工作不正常。非常感谢您为我提供的有用信息:)
int _tmain(int argc, _TCHAR* argv[])
{
    boost::asio::io_service ioService;

    TCPClient client(ioService, "localhost", "4000");
    ioService.run();

    client.Write("Heloo!!!");
    ioService.reset();
    ioService.run();
    getchar();
}