因此,我尝试遵循此教程: https://www.c-sharpcorner.com/article/socket-programming-in-cpp-using-boost-asio-tcp-server-and-client/
但是我在第4行using ip::tcp
上遇到错误,错误是“标识符ip未定义”
我将Visual Studio 2019与WSL一起使用,我做了一些挖掘工作,并且在我的boost / asio / ip / tcp.hpp文件中也存在一些错误。在typedef basic_endpoint<tcp> endpoint;
行上,出现错误,指出basic_endpoint不是模板。
希望你们能有所帮助。
#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;
//listen for new connection
tcp::acceptor acceptor_(io_service, tcp::endpoint(tcp::v4(), 1234));
//socket creation
tcp::socket socket_(io_service);
//waiting for the connection
acceptor_.accept(socket_);
//read operation
string message = read_(socket_);
cout << message << endl;
//write operation
send_(socket_, "Hello From Server!");
cout << "Servent sent Hello message to Client!" << endl;
return 0;
}