Binding to specific IP address using Websocket++

893 Views Asked by At

Using websocket++ as a client to a websocket server I want to connect using a specific IP of my client server. So I am trying to figure out how to bind to a specific IP, but it's not working out.

I've figured the on_socket_init handler might be used to configure the socket, however this handler:

void on_socket_init(websocketpp::connection_hdl hdl, boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &s) {
    boost::asio::ip::tcp::endpoint localEndpoint(
            boost::asio::ip::address::from_string("192.168.178.28"), 0);

    std::cout << "Before bind is done " << s.lowest_layer().local_endpoint().address().to_string() << std::endl;
    s.lowest_layer().bind(localEndpoint);
    std::cout << "Bind is done " << s.lowest_layer().local_endpoint().address().to_string() << std::endl;

causes this to happen:

Before bind is done 192.168.178.28
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
  what():  bind: Invalid argument

On my local machine I do not have more than one IP, so I am just trying to tell it to bind to the one IP I have, which should just not change anything for now, except validate that I can call bind(). However it blows up and I have no idea why. Testing on a server with a 2nd IP I get the same error.

Using gdb I've only found that the exception is thrown by a function that checks for some error code of some internal bind call. I have no idea where the code of that internal bind call would be and there is no further information except "Invalid argument" or error code 22. Which is just the number for invalid argument.

Trying to assign a bogus address I do not have on my machine yields a different error "bind: Cannot assign requested address", so guess the address I pass in is fine.

Is this the right way of doing this at all?

0

There are 0 best solutions below