Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Server failed to listen again if it has already failed to listen #390

Closed
wyyqyl opened this issue Dec 15, 2014 · 3 comments
Closed

Server failed to listen again if it has already failed to listen #390

wyyqyl opened this issue Dec 15, 2014 · 3 comments

Comments

@wyyqyl
Copy link

wyyqyl commented Dec 15, 2014

Sample code.
Env: Visual Studio 2008, Windows 7

#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS

#include <websocketpp/config/asio_no_tls.hpp>

#include <websocketpp/server.hpp>

#include <iostream>

typedef websocketpp::server<websocketpp::config::asio> server;

using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using boost::asio::ip;

typedef server::message_ptr message_ptr;

void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
  try {
    s->send(hdl, msg->get_payload(), msg->get_opcode());
  } catch (const websocketpp::lib::error_code& e) {
    std::cout << "Echo failed because: " << e
      << "(" << e.message() << ")" << std::endl;
  }
}

int main() {
  server server1;
  server server2;

  websocketpp::lib::error_code ec;

  server1.init_asio();
  server2.init_asio();

  tcp::endpoint ep1(address::from_string("127.0.0.1"), 12345);
  tcp::endpoint ep2(address::from_string("127.0.0.1"), 23456);

  server1.listen(ep1, ec);
  if (!ec) {
    server1.set_message_handler(bind(&on_message,&server1,::_1,::_2));
  }

  server2.listen(ep1, ec);
  if (ec) {
    server2.listen(ep2, ec);
    if (ec) {
      return 1;
    }
  }

  server1.start_accept();
  server1.run();
}

Here, server1 is listening on port 12345, then server2 failed to bind to port 12345, but it failed to bind any port from now on.
The reason is that you forgot to call m_acceptor->close() if listen failed. https://github.com/zaphoyd/websocketpp/blob/master/websocketpp/transport/asio/endpoint.hpp#L357

if (bec) {
    log_err(log::elevel::info,"asio listen",bec);
    ec = make_error_code(error::pass_through);
} else {
    m_state = LISTENING;
    ec = lib::error_code();
}

I think the correct way should be

if (bec) {
    if (m_acceptor->is_open()) {
      m_acceptor->close();
    }
    log_err(log::elevel::info,"asio listen",bec);
    ec = make_error_code(error::pass_through);
} else {
    m_state = LISTENING;
    ec = lib::error_code();
}

Correct me if I am wrong. Thanks!

@zaphoyd
Copy link
Owner

zaphoyd commented Dec 15, 2014

I'll do some more testing on this, but from a first glance, it looks like you are correct.

@zaphoyd
Copy link
Owner

zaphoyd commented Jan 22, 2015

This is fixed now on the develop branch

@zaphoyd
Copy link
Owner

zaphoyd commented Jan 22, 2015

The fix for this issue is now present in the 0.5.0 release on the master branch.

@zaphoyd zaphoyd closed this as completed Jan 22, 2015
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants