multi_threaded_server.c
Go to the documentation of this file.
00001 #include "mongoose.h"
00002 
00003 // Start a browser and hit refresh couple of times. The replies will
00004 // come from both server instances.
00005 static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
00006   if (ev == MG_REQUEST) {
00007     mg_send_header(conn, "Content-Type", "text/plain");
00008     mg_printf_data(conn, "This is a reply from server instance # %s",
00009                    (char *) conn->server_param);
00010     return MG_TRUE;
00011   } else if (ev == MG_AUTH) {
00012     return MG_TRUE;
00013   } else {
00014     return MG_FALSE;
00015   }
00016 }
00017 
00018 static void *serve(void *server) {
00019   for (;;) mg_poll_server((struct mg_server *) server, 1000);
00020   return NULL;
00021 }
00022 
00023 int main(void) {
00024   struct mg_server *server1, *server2;
00025 
00026   server1 = mg_create_server((void *) "1", ev_handler);
00027   server2 = mg_create_server((void *) "2", ev_handler);
00028 
00029   // Make both server1 and server2 listen on the same sockets
00030   mg_set_option(server1, "listening_port", "8080");
00031   mg_copy_listeners(server1, server2);
00032 
00033   // server1 goes to separate thread, server 2 runs in main thread.
00034   // IMPORTANT: NEVER LET DIFFERENT THREADS HANDLE THE SAME SERVER.
00035   mg_start_thread(serve, server1);
00036   mg_start_thread(serve, server2);
00037   getchar();
00038 
00039   return 0;
00040 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05