79665891

Date: 2025-06-14 14:00:16
Score: 2
Natty:
Report link

@Patrik Mathur Thank you, sir. I didn’t realize it provides a ready to use loop

My previous code in the original post is working now by correctly setting the reuse addr and reuse port like this

```server->setsockopt<int>(SOL_SOCKET, SO_REUSEADDR, 1);

server->setsockopt<int>(SOL_SOCKET, SO_REUSEPORT, 1);

```

But the performance is very low, probably because it spawns a new thread for every incoming connection

I'm trying the built in loop now like this

```

void HttpServer::start() {
    photon::init(photon::INIT_EVENT_DEFAULT, photon::INIT_IO_DEFAULT);
    DEFER(photon::fini());

    auto server = photon::net::new_tcp_socket_server();
    if (server == nullptr) {
        throw std::runtime_error("Failed to create TCP server");
    }
    DEFER(delete server);

    auto handler = [this](photon::net::ISocketStream* stream) -> int {
        DEFER(delete stream);
        stream->timeout(30UL * 1000 * 1000);
        this->handle_connection(stream);
        return 0;
    };

    server->set_handler(handler);

    int bind_result = server->bind(options_.port, photon::net::IPAddr());
    if (bind_result != 0) {
        throw std::runtime_error("Failed to bind to localhost:" + std::to_string(options_.port));
    }

    if (server->listen() != 0) {
        throw std::runtime_error("Failed to listen on port " + std::to_string(options_.port));
    }

    LOG_INFO("Server is listening on port ", options_.port, " ...");
    LOG_INFO("Server starting main loop...");
    server->start_loop(true);
}

```

But I’m still trying to fix it because I’m getting a segmentation fault :(

Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Blacklisted phrase (1): :(
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Patrik
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Fuji