如何将本地端点绑定到套接字? [英] How to bind the local endpoint to a socket?
问题描述
我有以下代码,我试图通过手动更改套接字的端点发送数据包到特定的客户端,我不知道如何配置套接字使用 async_write
函数发送数据包。第44-47行用于尝试解决boost异步服务器示例(已修改)中的问题。
I have the following code where I'm trying to send a packet to a specific client by changing the endpoint of the socket manually and I'm not sure how to configure the socket to use the async_write
function to send the packet. Lines 44-47 are used to try solving the problem within the boost async server example (modified).
#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;
}
推荐答案
将一些UDP的概念应用于TCP流。
It looks like you are trying to apply some concepts of UDP to a TCP stream.
TCP需要建立与三次握手的连接。
因为这个原因,你不能简单地通过修改套接字的ip远程端点地址发送数据到另一个客户端。
TCP needs to establish a connection with a 3-way handshake. For that reason, you can't send data to another client simply by modifying the ip remote endpoint address of a socket.
TCP服务器监听进入连接,接受它们,并在此过程结束时有一个有效的TCP套接字,您可以在其中写入数据。每个客户端都有自己的套接字。还要注意,TCP套接字提供了一个数据流(不是包/数据报)。
A TCP server listens for incoming connections, accepts them and at the end of this process has a valid TCP socket where you can write data. Each client has it's own socket. Also note that a TCP socket provides a data stream (not packets/datagrams).
另一方面,UDP没有连接的概念。您可以使用相同的UDP套接字发送数据报到多个端点(boost :: asio :: ip :: udp :: socket :: send_to)
On the other hand, UDP does not have the concept of a connection. You can send datagrams to multiple endpoints using the same UDP socket (boost::asio::ip::udp::socket::send_to)
这篇关于如何将本地端点绑定到套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!