eidheim / Simple-Web-Server

A very simple, fast, multithreaded, platform independent HTTP and HTTPS server and client library implemented using C++11 and Boost.Asio. Created to be an easy way to make REST resources available from C++ applications.
MIT License
2.62k stars 758 forks source link

Serve more than one socket #102

Closed sebt3 closed 6 years ago

sebt3 commented 7 years ago

Hi,

currently the code allow to listen on a single pair<address, port>. It would be nice to actually be able to use more than one.

eidheim commented 7 years ago

Thank you. I'm occupied at the moment, but I'll look into this next week.

SethHamilton commented 7 years ago

Can you not make two server objects, one listening on 80 and the other on 443 for example (start them each in a thread)?

killoctal commented 6 years ago

Same problem, I must have two different ports with different services but the service with the lower port number never anwers. The problem is that acceptor.async_accept() never wake up...

eidheim commented 6 years ago

@killoctal here is an example with two servers running on two different ports:

#include "server_http.hpp"

using namespace std;

using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>;

int main() {
  HttpServer server1;
  server1.config.port = 8080;

  server1.resource["^/$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> /*request*/) {
    response->write("test1");
  };

  HttpServer server2;
  server2.config.port = 8081;

  server2.resource["^/$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> /*request*/) {
    response->write("test2");
  };

  thread server1_thread([&server1]() {
    // Start server 1
    server1.start();
  });
  thread server2_thread([&server2]() {
    // Start server 2
    server2.start();
  });

  server1_thread.join();
  server2_thread.join();
}
killoctal commented 6 years ago

Hi I am sorry It was not working because I had an old legacy code hidden in the darkness which was already using the port. Now it works Best regards Gabriel