Answer the question
In order to leave comments, you need to log in
How to make a web socket available outside the same network?
Good afternoon!
I started to study the boost library, asio. Everything works on the local network, now I'm trying to make the socket accessible from outside, there is a server, a static ip address. On the router, I made port forwarding static_ip: 8083-> 192.168.0.52: 8083, but the socket is not available. Tell me, please, where to dig to make the socket available?
Here is the code:
auto const address = boost::asio::ip::make_address("192.168.0.52");
auto const port = static_cast<unsigned short>(std::atoi("8083"));
boost::asio::io_context ioc{1};
tcp::acceptor acceptor{ioc, {address, port}};
while(1){
tcp::socket socket{ioc};
acceptor.accept(socket);
std::cout<<"socket accepted"<<std::endl;
std::thread{[q = std::move(socket)]() mutable {
boost::beast::websocket::stream<tcp::socket> ws {std::move(q)};
ws.write_buffer_bytes(32789);
ws.accept();
ws.binary(true);
while(1){
try{
boost::beast::flat_buffer buffer;
ws.read(buffer);
ws.write(boost::asio::buffer(std::string("Hello World")));
}
catch(boost::beast::system_error const& se){
if(se.code() != boost::beast::websocket::error::closed){
std::cout << se.code().message() << std::endl;
break;
}
}
}
}}.detach();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question