sending packets to a client given the remote_endpoint object and the socket?

别来无恙 提交于 2019-12-25 04:14:06

问题


Is there a way to send a packet to a client knowing the remote_endpoint object where endpoint.address and endpoint.port are used?

EDIT: I added the async_write free function but I still don't know how to specify the ip-address or the port for the socket_ to send data_ to (the client). The changes are lines 44-49 to the boost async server example.

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

using boost::asio::ip::tcp;

class session
{
public:
  session(boost::asio::io_service& io_service)
    : socket_(io_service)
  {
  }

  tcp::socket& socket()
  {
    return socket_;
  }

  void start()
  {
      std::cout<<"start(): "<<std::endl;
          socket_.async_read_some(boost::asio::buffer(data_, max_length),
          boost::bind(&session::handle_read, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }
void read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred);


private:
  void handle_read(const boost::system::error_code& error,
      size_t bytes_transferred)
  {
      std::cout<<"handle read: bytes_transferred"<<bytes_transferred<<std::endl;
    if (!error)
    {
          data_ = {'0','1','2','3','4','5','6','7','8','9'};
          /*read_handler(error, bytes_transferred);
          boost::asio::async_write(socket_,boost::asio::buffer(data_, bytes_transferred),
                boost::bind(&session::handle_write, this,
                            boost::asio::placeholders::error));*/
          boost::system::error_code ec;
          boost::asio::ip::tcp::endpoint endpoint = socket_.remote_endpoint(ec);
          std::cout<<"ip address: "<<endpoint.address()<<std::endl;
          std::cout<<"port: "<<endpoint.port()<<std::endl;
          //how to get the socket to send data to specific port, ip-address e.g. changing the ip-address and port by editing it?
          async_write(socket_, boost::asio::buffer(data_, max_length), boost::bind(&session::writeHandler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
    }
    else
    {
      delete this;
    }
  }
  void writeHandler(const boost::system::error_code& errorCode, size_t bytesTransferred)
  {
        std::cout << "DEBUG> Transfered " << bytesTransferred << " bytes to " << socket_.remote_endpoint().address().to_string() << std::endl;
  }
  void handle_write(const boost::system::error_code& error)
  {
      std::cout<<"handle write: "<<std::endl;

    if (!error)
    {
            std::cout<<"before: "<<data_<<std::endl;

            socket_.async_read_some(boost::asio::buffer(data_, max_length),
            boost::bind(&session::handle_read, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));

            std::cout<<"after: "<<data_<<std::endl;
    }
    else
    {
      delete this;
    }
  }

  tcp::socket socket_;
  enum { max_length = 1024 };
  char data_[max_length];
};
void session::read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
      std::cout<<bytes_transferred<<std::endl;
}

class server
{
public:
  server(boost::asio::io_service& io_service, short port)
    : io_service_(io_service),
      acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
  {
    start_accept();
  }


private:
  void start_accept()
  {
    session* new_session = new session(io_service_);
    acceptor_.async_accept(new_session->socket(),
        boost::bind(&server::handle_accept, this, new_session,
          boost::asio::placeholders::error));
  }

  void handle_accept(session* new_session,
      const boost::system::error_code& error)
  {
    if (!error)
    {
      new_session->start();
    }
    else
    {
      delete new_session;
    }

    start_accept();
  }

  boost::asio::io_service& io_service_;
  tcp::acceptor acceptor_;
};

int main()
{
  try
  {
    boost::asio::io_service io_service;
    server s(io_service, 4000);

    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

回答1:


Assuming TCP sockets since that is what you linked to in the documentation, no that is not possible. You need a socket object to send data, not just an endpoint. Use the async_write() free function, one of its many overloads, or any of the socket class methods to send data.



来源:https://stackoverflow.com/questions/16006894/sending-packets-to-a-client-given-the-remote-endpoint-object-and-the-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!