我试图在没有客户端连接到服务器的情况下保存服务器的IP。服务器是迭代同步的,等待客户端连接。如上所述,我需要在客户端开始连接服务器之前保存IP。注释掉的代码实际上是获取套接字端点的地址。我不希望有人写代码,但也许可以解释一下我的错误......所以这是我的代码:
string ds_IP;DS::DS(void) {
cout << "Data Server is UP!" << endl;
boost::asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1)); for (;;) { tcp::socket incoming(io_service); acceptor.accept(incoming); /*//identify IP tcp::endpoint remote = incoming.remote_endpoint(); boost::asio::ip::address remote_address = remote.address(); ds_IP = remote_address.to_string(); cout << ds_IP << endl;*/ cout << "Client is Connected" << endl; }
}