Skip to content

Instantly share code, notes, and snippets.

@wyyqyl
Created December 18, 2014 14:14
Show Gist options
  • Save wyyqyl/bb2506ac2e5ad7b48330 to your computer and use it in GitHub Desktop.
Save wyyqyl/bb2506ac2e5ad7b48330 to your computer and use it in GitHub Desktop.
Occupy specific ports
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <boost/shared_array.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 namespace 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(int argc, char* argv[]) {
boost::asio::io_service ios;
websocketpp::lib::error_code ec;
boost::shared_array<server> servers(new server[argc - 1]);
for (int idx = 0; idx < argc - 1; ++idx) {
servers[idx].init_asio(&ios);
tcp::endpoint ep(address::from_string("127.0.0.1"), std::stoi(argv[idx + 1]));
std::cout << "Trying to listen to port " << std::string(argv[idx + 1]) << std::endl;
servers[idx].listen(ep, ec);
if (!ec) {
servers[idx].set_message_handler(bind(&on_message, &servers[idx], _1, _2));
servers[idx].start_accept();
}
}
ios.run();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment