Connecting to a domain to host a server with Boost.Asio - c++

How can I connect to my domain through my server, so that I can "host" it?
I've tried changing the code snippet below to match the IP with my domain's name, but a exception is caught saying that a invalid argument was supplied, I assumed I should resolve the domain name, get the IP and use the IP I got and not the one in the code snippet below, but it seems like I'm connecting to my external IP, and this doesn't let me host the server, as it says the machine actively refused the connection.
That's the code snippet:
boost::asio::ip::tcp::acceptor acceptor(service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("192.168.1.3"), 8001));
That's the full code:
Main.cpp
#include "Server.h"
#include <string>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
int main()
{
Server server;
boost::thread([&server] {
server.handleConnections();
}).join();
return 0;
}
Server.h
#ifndef SERVER_H
#define SERVER_H
#include <string>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
class Server
{
private:
boost::asio::io_service service;
boost::asio::ip::tcp::socket sock;
protected:
public:
Server() : service(), sock(service) {
}
~Server() {
}
void handleConnections();
};
#endif
Server.cpp
#include "Server.h"
void Server::handleConnections() {
boost::system::error_code err;
try {
boost::asio::ip::tcp::acceptor acceptor(service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("192.168.1.3"), 8001));
std::cout << acceptor.local_endpoint(err) << std::endl;
while (true) {
sock = boost::asio::ip::tcp::socket(service);
boost::system::error_code errCode;
acceptor.accept(this->sock, acceptor.local_endpoint(), errCode);
boost::asio::write(sock, boost::asio::buffer("Olá"), errCode);
}
this->sock.close();
}
catch (std::exception & e)
{
std::cout << e.what();
}
}

Firstly
acceptor.accept(this->sock, acceptor.local_endpoint(), errCode);
The documentation says:
This function is used to accept a new connection from a peer into the given socket, and additionally provide the endpoint of the remote peer. The function call will block until a new connection has been accepted successfully or an error occurs.
You ... pass the acceptor's local endpoint as the endpoint to receive the peer's remote endpoint (I'm surprised if that compiles). That makes little sense. I suggest you don't need to know the remote endpoint¹:
acceptor.accept(this->sock, errCode);
Secondly, you've bound to the loopback adaptor:
boost::asio::ip::tcp::acceptor acceptor(service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("192.168.1.3"), 8001));
This directly implies you cannot access it from the network². Change it to be NIC-agnostic:
tcp::acceptor acceptor(service, tcp::endpoint(ip::address(), 8001));
¹ you can usually get it from the socket later using socket_->remote_endpoint() unless the socket has become invalid (e.g. closed)
² unless you implement some funky routing/tunneling logic, which is far stretch
UPDATE
Self contained demo with error handling:
Live On Coliru
#include <iostream>
#include <boost/asio.hpp>
class Server
{
boost::asio::io_service service;
public:
void handleConnections()
{
using namespace boost::asio;
boost::system::error_code err;
try {
ip::tcp::acceptor acceptor(service, ip::tcp::endpoint(ip::address(), 6768));
std::cout << acceptor.local_endpoint(err) << std::endl;
while (!err) {
ip::tcp::socket sock(service);
if ( !acceptor.accept(sock, err)
&& !write(sock, buffer("Olá"), err)
&& !sock.close(err))
{
std::cout << "Error in connection: " << err << " " << err.message() << "\n";
}
}
} catch (std::exception & e) {
std::cout << e.what();
}
}
};
#include <boost/thread.hpp>
int main() {
Server server;
boost::thread([&server] { server.handleConnections(); }).join();
}
Output:
$ ./a.out&
0.0.0.0:6768
$ while sleep 1; do nc 127.0.0.1 6768; done
OláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOlá

Related

asio tcp server hanging

I know this is probably a really simple problem but ive been trying to get the asio examples to work correctly for over a week now. whenever I run the program, the terminal hangs and dosent print anything and dosent send any info to the client. Im using Ubuntu Linux and a basic compiler command
g++ main.cpp -o main.exe -I include
#define ASIO_STANDALONE;
#include <ctime>
#include <iostream>
#include <string>
#include <asio.hpp>
using asio::ip::tcp;
int main()
{
try
{
asio::io_context io_context;
tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), 1326));
for (;;)
{
std::cout << "hi";
tcp::socket socket(io_context);
acceptor.accept(socket);
std::string message = "e";
asio::error_code ignored_error;
asio::write(socket, asio::buffer(message), ignored_error);
break;
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
any help would be much appreciated
the terminal hangs and dosent print anything and dosent send any info to the client
You need to connect a client first, because the first thing you do is a blocking accept which never completes unless a connection arrives.
I've compiled your program (with minor modification for Boost Asio):
Live On Coliru
//#define ASIO_STANDALONE
#include <boost/asio.hpp>
#include <ctime>
#include <iostream>
#include <string>
namespace asio = boost::asio;
using asio::ip::tcp;
using boost::system::error_code;
int main() {
try {
asio::io_context io_context;
tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), 1326));
for (;;) {
tcp::socket socket(io_context);
acceptor.accept(socket);
std::cout << "hi " << socket.remote_endpoint() << std::endl;
std::string message = "server message works\n";
error_code ignored_error;
asio::write(socket, asio::buffer(message), ignored_error);
break;
}
} catch (std::exception const& e) {
std::cerr << e.what() << std::endl;
}
}
Using netcat to emulate a client:
nc 127.0.0.1 1326 -w 1 <<< "Hello world"
We see:
hi 127.0.0.1:45448
server message works
Or more clearly in separate terminals:

Connection lost just after connection -> Server TCP boost.ASIO

I did the boost tutorial : An asynchronous TCP daytime server http://think-async.com/Asio/asio-1.1.1/doc/asio/tutorial/tutdaytime3.html
When I want to test it, the server is running so that's good but if I do nc -C localhost 4242 the client got the message of the server but the client is directly disconnected after.
Here my code :
#include "server.h"
#include "connection.h"
Server::Server(boost::asio::io_service& io_service) : accept(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 4242))
{
wait_connection();
}
Server::~Server()
{
}
void Server::wait_connection()
{
std::cout << "wait_connection" << std::endl;
boost::shared_ptr<Connection> new_connection =
Connection::start_connection(accept.get_io_service());
accept.async_accept(new_connection->getSocket(), boost::bind(&Server::callback_accept, this, new_connection, boost::asio::placeholders::error));
}
void Server::callback_accept(boost::shared_ptr<Connection> new_connection, const boost::system::error_code &error)
{
if (!error)
{
new_connection->send_message_to_client();
wait_connection();
}
}
Connection::Connection(boost::asio::io_service& io_service) : socket(io_service)
{
}
Connection::~Connection()
{
std::cout << "destructeur Connection" << std::endl;
}
boost::shared_ptr<Connection> Connection::start_connection(boost::asio::io_service& io_service)
{
return (boost::shared_ptr<Connection>(new Connection(io_service)));
}
boost::asio::ip::tcp::socket& Connection::getSocket()
{
return (this->socket);
}
void Connection::send_message_to_client()
{
message = "Bienvenue!\n";
boost::asio::async_write(socket, boost::asio::buffer(message), boost::bind(&Connection::callback_send, shared_from_this()));
}
void Connection::callback_send()
{
}
int main()
{
try {
boost::asio::io_service io_service;
Server server(io_service);
io_service.run();
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
return (0);
}
#ifndef SERVER_H
#define SERVER_H
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include "connection.h"
class Server {
private:
boost::asio::ip::tcp::acceptor accept;
public:
Server (boost::asio::io_service&);
~Server ();
void wait_connection();
void callback_accept(boost::shared_ptr<Connection> new_connection, const boost::system::error_code& error);
};
#endif /* end of include guard: SERVER_H */
#ifndef CONNECTION_H
#define CONNECTION_H
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
class Connection : public boost::enable_shared_from_this<Connection>
{
private:
boost::asio::ip::tcp::socket socket;
std::string message;
public:
Connection (boost::asio::io_service&);
~Connection ();
static boost::shared_ptr<Connection> start_connection(boost::asio::io_service&);
boost::asio::ip::tcp::socket& getSocket();
void send_message_to_client();
void callback_send();
};
#endif /* end of include guard: CONNECTION_H */
Crux: Shared pointers employ keep the object alive until the reference count reaches zero.
You write the message to the client here. When it's complete, you will execute callback_send:
boost::asio::async_write(socket, boost::asio::buffer(message),
boost::bind(&Connection::callback_send, shared_from_this()));
So what do we do next?
void Connection::callback_send() {}
Oh. That's... not a lot. So. Nothing?
Well. Almost nothing.
It's a case of "not doing something is also doing something". By not posting another operation that keeps the socket/connection alive, this means that the connection is going to be released.
Because nothing else keeps the shared_ptr to the connection, shared_ptr will delete the connection (invoking the destructor, which you could see because it prints destructeur Connection every time).
So. What is the solution? We Don't Know. It's up to you what you want to do after you said "welcome". In most likely-hood you will want to wait for some kind of message from the client. This would involve some async_read* call which happily keeps the connection alive (shared_from_this() again).
Demo
Let's assume you want to keep receiving lines, and you send the same lines back, reversed:
void Connection::callback_send() {
boost::asio::async_read_until(socket, request, "\n",
boost::bind(&Connection::on_request_received, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void Connection::on_request_received(boost::system::error_code ec, size_t n) {
if (ec && !((ec == boost::asio::error::eof) && n))
std::cout << "Receive error: " << ec.message() << "\n";
else
{
std::cout << "Received request\n";
{
std::istream is(&request);
std::getline(is, message);
}
std::reverse(message.begin(), message.end());
std::cout << "Sending response: " << message << "\n";
message += '\n';
if (!ec) boost::asio::async_write(socket, boost::asio::buffer(message),
boost::bind(&Connection::callback_send, shared_from_this()));
}
}

C++ boost tcp-server

I tried to create an tcp-server with the boost libs. I basically took example code from boost(the whole code was in 1 file there) and split it up into a class.
Seems like something went wrong there and i dont really know whats missing.
The main error is at the end of "tcp_server.cpp" i put the error message into a comment there. It looks like he doesnt know the function session(at least thats how i interpreted it), i get the same error with "tcp_server::session" and "this->session".
The second thing is an error i get from "e.what()"(cannot resolve identifer what). It worked in the example code and i didnt change anything at the exceptions.
Thanks in advance.
tcp_server.h
#include <cstdlib>
#include <iostream>
#include <exception>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
using boost::asio::ip::tcp;
class tcp_server
{
public:
typedef boost::shared_ptr<tcp::socket> socket_ptr;
tcp_server();
~tcp_server();
void server(boost::asio::io_service&);
private:
const short port = 8888;
const int max_length = 1024;
void session(socket_ptr);
};
tcp_server.cpp
#include "tcp_server.h"
tcp_server::tcp_server()
{
}
tcp_server::~tcp_server()
{
}
void tcp_server::session(socket_ptr sock)
{
try
{
for (;;)
{
char data[max_length];
boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.
boost::asio::write(*sock, boost::asio::buffer(data, length));
}
}
catch (std::exception& e)
{
std::cerr << "Exception in thread: " << e.what() << "\n"; //
}
}
void tcp_server::server(boost::asio::io_service& io_service)
{
tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
for (;;)
{
socket_ptr sock(new tcp::socket(io_service));
a.accept(*sock);
boost::thread t(boost::bind(session, sock)); //error: no matching function for call to ‘bind(<unresolved overloaded function type>, tcp_server::socket_ptr&)’`
}
}
main.cpp
#include "tcp_server.h"
int main() {
tcp_server server;
try
{
boost::asio::io_service io_service;
server.server(io_service);
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
You cannot bind the non-static member function tcp_server::session because it cannot be called without an object instance, which is not available to the bind.
You can however make tcp_server::session static (in tcp_header.h) to fix this:
static void session(socket_ptr);
Edit:
You can bind session as non-static member using:
boost::thread t(boost::bind(&tcp_server::session, this, sock));

c++ boost asio: bind: Address already in use

I am using c++ boost asio for making a server client application.
I followed the guide lines from here.
And I am still wondering why I get the following result:
./server #ok
./client # error
bind: Address already in use
server.cpp:
#include <ctime>
#include <iostream>
#include <stdio.h>
#include <string>
#include <boost/array.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::udp;
struct UDP_Message
{
double number;
};
int main()
{
try
{
boost::asio::io_service io_service;
udp::socket socket(io_service, udp::endpoint(udp::v4(), config::udp_port));
UDP_Message message;
message.number=0;
for (;;)
{
udp::endpoint remote_endpoint;
message.number=message.number+0.001;
boost::system::error_code ignored_error;
socket.send_to(boost::asio::buffer(&message,sizeof(message)),
remote_endpoint, 0, ignored_error);
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
client.cpp:
#include <ctime>
#include <iostream>
#include <stdio.h>
#include <string>
#include <boost/array.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::udp;
namespace config
{
const unsigned short udp_port=1414;
}
struct UDP_Message
{
double number;
};
int main()
{
try
{
boost::asio::io_service io_service;
boost::asio::socket_base::reuse_address option(true);
udp::socket socket(io_service, udp::v4());
socket.set_option(option);
socket.bind(udp::endpoint(udp::v4(), config::udp_port));
UDP_Message message;
for (;;)
{
boost::array<char, 1> recv_buf;
udp::endpoint remote_endpoint;
boost::system::error_code error;
socket.receive_from(boost::asio::buffer(recv_buf),
remote_endpoint, 0, error);
if (error && error != boost::asio::error::message_size)
throw boost::system::system_error(error);
std::memcpy(&message,recv_buf.data(),sizeof(message));
std::cout<<message.number<<std::endl;
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
You are trying to bind both your client and server to the same port, udp_port=1414. This you can not do.

Async Read server BoostAsio not calling handler

I'm currently working with Boost::Asio to do a basic Read/Write server, I have a little problem when it comes to the usage of the async_read function of the library.
Here's my code snippet :
main.cpp :
#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include "TCPServer.hpp"
#include "TCPConnection.hpp"
using boost::asio::ip::tcp;
int main()
{
try
{
boost::asio::io_service io_service;
TCPServer server(io_service);
io_service.run();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
TCPServer.hpp :
#ifndef TCPSERVER_HPP_
#define TCPSERVER_HPP_
#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include "TCPConnection.hpp"
using boost::asio::ip::tcp;
class TCPServer
{
private:
tcp::acceptor _acceptor;
public:
TCPServer(boost::asio::io_service& ioService)
: _acceptor(ioService, tcp::endpoint(tcp::v4(), 4040))
{
startAccept();
}
private:
void startAccept()
{
TCPConnection::pointer newConnection =
TCPConnection::create(_acceptor.get_io_service());
_acceptor.async_accept(newConnection->getSocket(),
boost::bind(&TCPServer::handleAccept, this, newConnection,
boost::asio::placeholders::error));
}
void handleAccept(TCPConnection::pointer newConnection, const boost::system::error_code& error)
{
if (!error)
{
newConnection->asyncWrite("JIOLE");
startAccept();
}
}
};
#endif
TCPConnection.hpp :
#ifndef TCPCONNECTION_HPP_
#define TCPCONNECTION_HPP_
#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
class TCPConnection : public boost::enable_shared_from_this<TCPConnection>
{
private:
boost::asio::ip::tcp::socket _socket;
std::string _readMessage;
boost::asio::streambuf _response;
public:
typedef boost::shared_ptr<TCPConnection> pointer;
static pointer create(boost::asio::io_service& ios)
{
return pointer(new TCPConnection(ios));
}
boost::asio::ip::tcp::socket& getSocket()
{
return _socket;
}
void asyncWrite(const std::string &message)
{
boost::asio::async_write(_socket,
boost::asio::buffer(message),
boost::bind(&TCPConnection::handleWrite, shared_from_this(),
boost::asio::placeholders::error));
std::cout << "AsyncWrite" << std::endl;
}
void asyncRead()
{
std::cout << "1st \"asyncRead\"" << std::endl;
boost::asio::async_read(_socket,
_response,
boost::bind(&TCPConnection::handleRead, shared_from_this(),
boost::asio::placeholders::error));
std::cout << "2nd \"asyncRead\"" << std::endl;
}
void close()
{
_socket.close();
}
private:
TCPConnection(boost::asio::io_service &ioS) : _socket(ioS) {}
void handleWrite(const boost::system::error_code& error)
{
std::cout << "Write Handler" << std::endl;
if (!error)
{
asyncRead();
}
//SEE WHAT TO DO IN CASE OF ERROR
}
void handleRead(const boost::system::error_code& error)
{
std::cout << "Read Handler" << std::endl;
if (!error)
{
std::cout << &_response << std::endl;
asyncRead();
}
else
{
std::cout << &error << std::endl;
_socket.close();
}
//CREATE SENDER(RSEPONSE::ERROR)
}
};
#endif
The problem is that my async_read doesn't call the handler. Here is the output :
Output :
AsyncWrite
Write Handler
1st "asyncRead"
2nd "asyncRead"
When I'm writing something on a NetCat Client nothing is being received.
Here what happens when i'm pressing ctrl+C :
Read Handler
0x7fff645f3be0
I don't understand why nothing is received.
The problem is that you did not tell async_read when it should call the handler function. There are two ways to specify this: Either specify the input size after which the handler shall be called or specifiy a delimiter through async_read_until.
In your case, you could use the following:
boost::asio::async_read_until(_socket,
_response,
"\n",
boost::bind(&TCPConnection::handleRead,
shared_from_this(),
boost::asio::placeholders::error)
);
This will call the handler when a newline is sent from the client.