我正在尝试设计一个客户端服务器程序,其中双方{server,client}可以与async_connect进行通信。 这是我的服务器程序代码:
std::string GetMessage()
{
std::string user("User");
user += client;
client++;
return user;
}
class TcpConnection
: public boost::enable_shared_from_this<TcpConnection>
{
private:
boost::asio::ip::tcp::socket socket1;
std::string message;
TcpConnection(boost::asio::io_service &io_service) :
socket1(io_service)
{
}
void Writehandler(const boost::system::error_code &errorCode, size_t size)
{
if (!errorCode)
{
Read();
}
std::cout << errorCode.message() << std::endl;
}
public:
typedef boost::shared_ptr<TcpConnection> pointer;
static pointer Create(boost::asio::io_service &io_service)
{
return pointer(new TcpConnection(io_service));
}
boost::asio::ip::tcp::socket &socket()
{
return socket1;
}
void Start()
{
message = GetMessage();
boost::asio::async_write(socket(), boost::asio::buffer(message),
boost::bind(&TcpConnection::Writehandler, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void Read()
{
std::array<char, 65> messages{};
auto self(shared_from_this());
boost::asio::async_read(socket(),
boost::asio::buffer(messages),
[this, self](boost::system::error_code ec, std::size_t /*length*/) {
if (!ec) {
Read();
} else {
std::cout << ec.message() << std::endl;
exit(1);
}
});
}
};
class Server
{
private:
boost::asio::ip::tcp::acceptor acceptor;
public:
Server(boost::asio::io_service &io_service, unsigned short int port)
:
acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))
{
startAccept();
}
void startAccept()
{
TcpConnection::pointer newConnection = TcpConnection::Create(acceptor.get_io_service());
acceptor.async_accept(newConnection->socket(),
boost::bind(&Server::Handler, this, newConnection, boost::asio::placeholders::error
));
}
void Handler(TcpConnection::pointer &newConnection, const boost::system::error_code &errorCode)
{
if (!errorCode)
newConnection->Start();
startAccept();
}
};
}
这是我的客户代码:
class Client
: public boost::enable_shared_from_this<Client>
{
private:
boost::asio::ip::tcp::socket socket1;
std::string port;
public:
Client(boost::asio::io_service &io_service, std::string port)
: port(std::move(port)),
socket1(io_service)
{
start();
}
void handler(const boost::system::error_code &errorCode, size_t size)
{
}
boost::asio::ip::tcp::socket &socket()
{
return socket1;
}
void start()
{
try
{
boost::asio::ip::tcp::resolver resolver(socket1.get_io_service());
boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), port);
boost::asio::ip::tcp::resolver::iterator endpoint = resolver.resolve(query);
std::cout << exception.what() << std::endl;
}
}
void read()
{
std::array<char, 64> messages{};
boost::asio::async_read(socket(),
boost::asio::buffer(messages.data(), messages.size()),
[this](boost::system::error_code ec, std::size_t /*length*/)
{
if (!ec)
{
read();
} else
{
socket().close();
}
});
}
void write()
{
std::string message{};
getline(std::cin, message, '\n');
boost::asio::async_write(socket(),
boost::asio::buffer(message),
[this](boost::system::error_code ec, std::size_t /*length*/)
{
if (!ec)
{
write();
} else
{
socket().close();
}
});
}
};
代码没有错误。但当我连接两个数据而不注入时。
答案 0 :(得分:1)
在Client::start
功能中,您实际上从未实际连接到任何远程服务器。您只能解析远程合作伙伴的地址。
boost::asio::connect
或boost::asio::async_connect
可能就是您想要的。
void start()
{
try
{
boost::asio::ip::tcp::resolver resolver(socket1.get_io_service());
boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), port);
boost::asio::ip::tcp::resolver::iterator endpoint = resolver.resolve(query);
boost::asio::connect(socket(), endpoint); //<--- Connect to your server
std::cout << exception.what() << std::endl;
}
catch (const std::exception &e)
{
//Handle
}
catch (const boost::system::system_error &e)
{
//Handle
}
}
您可能还想使用try块catch
。我不确定哪个编译器允许你这样做。
此外,建议将读缓冲区创建为类成员。