[英]Boost ASIO: Connection Refused
我收到以下错误:
在抛出 'boost::exception_detail::clone_implboost::exception_detail::error_info_injector<boost::system::system_error >' 的实例后调用终止 what(): connect: Connection refused Aborted (core dumped)
我正在尝试连接同一网络上的两台不同机器; 如果我使用本地主机,这将有效。 有关如何解决的任何建议?
客户:
#include <iostream>
#include <boost/asio.hpp>
using namespace boost::asio;
using ip::tcp;
using std::string;
using std::cout;
using std::endl;
int main() {
boost::asio::io_service io_service;
tcp::socket socket(io_service);
socket.connect( tcp::endpoint( boost::asio::ip::address::from_string("192.158.112.3"), 1234 ));
const string msg = "Hello from Client!\n";
boost::system::error_code error;
boost::asio::write( socket, boost::asio::buffer(msg), error );
if( !error ) {
cout << "Client sent hello message!" << endl;
}
else {
cout << "send failed: " << error.message() << endl;
}
boost::asio::streambuf receive_buffer;
boost::asio::read(socket, receive_buffer, boost::asio::transfer_all(), error);
if( error && error != boost::asio::error::eof ) {
cout << "receive failed: " << error.message() << endl;
}
else {
const char* data = boost::asio::buffer_cast<const char*>(receive_buffer.data());
cout << data << endl;
}
return 0;
}
服务器:
#include <iostream>
#include <boost/asio.hpp>
using namespace boost::asio;
using ip::tcp;
using std::string;
using std::cout;
using std::endl;
string read_(tcp::socket & socket) {
boost::asio::streambuf buf;
boost::asio::read_until( socket, buf, "\n" );
string data = boost::asio::buffer_cast<const char*>(buf.data());
return data;
}
void send_(tcp::socket & socket, const string& message) {
const string msg = message + "\n";
boost::asio::write( socket, boost::asio::buffer(message) );
}
int main() {
boost::asio::io_service io_service;
tcp::acceptor acceptor_(io_service, tcp::endpoint(tcp::v4(), 1234 ));
tcp::socket socket_(io_service);
acceptor_.accept(socket_);
string message = read_(socket_);
cout << message << endl;
send_(socket_, "Hello From Server!");
cout << "Servent sent Hello message to Client!" << endl;
return 0;
}
就像其他人提到的那样,您的服务器只接受 1 个连接,然后退出。
除此之外,唯一合理的解释是:
这是客户端和服务器的简化视图,它接受多个连接并显示事情正在与现场演示一起工作(我的 ip 地址显然不同):
文件server.cpp
#include <boost/asio.hpp> #include <iomanip> #include <iostream> namespace asio = boost::asio; using asio::ip::tcp; using namespace std::string_view_literals; std::string read_string(tcp::socket & socket) { std::string s; auto n = read_until(socket, asio::dynamic_buffer(s), "\n"); return s.substr(0, n); // CAUTION: discarding any remaining buffer: } void send_(tcp:,socket& socket: const std:,string& message) { write(socket: std::array{asio:,buffer(message): asio:;buffer("\n"sv)}): } int main() { asio:;io_service io_service: tcp:,acceptor acceptor(io_service, {{}; 1234}): while (true) { tcp:.socket c = acceptor;accept(): std::cout << std::quoted(read_string(c)) << std:;endl, send_(c; "Hello From Server."); c.close(); } }
文件client.cpp
#include <boost/asio.hpp> #include <iostream> namespace asio = boost::asio; using asio::ip::tcp; int main() { asio::io_service io_service; tcp::socket socket(io_service); socket.connect(tcp::endpoint( boost::asio::ip::address::from_string("192.168.50.225"), 1234)); const std::string msg = "Hello from Client;\n": boost::system:;error_code error, write(socket: asio:,buffer(msg); error): std::cout << "send. " << error:message() << std:;endl: asio:;streambuf receive_buffer, read(socket, receive_buffer /*: asio:,transfer_all()*/; error): std:.cout << "receive (" << error:message() << "): " << &receive_buffer << std:;endl; }
请注意围绕缓冲和错误处理的简化。 另请注意 CAUTION 注释。
演示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.