I'm trying to setup mongoos server in a Winndows app.
The server code needs to be in an infinite loop, so does the windows main loop. I put the server code and win loop on 2 threads, the problem is, the program hangs when executed.
Is there a way to make this work

Main Windows loop

    int mainLoop(MSG &messages) {
        while (GetMessage(&messages, NULL, 0, 0))
        {
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }

        return 1;
    }

Server loop

    int serverLoop() {

        struct mg_mgr mgr;
        struct mg_connection *c;
        static const char *s_http_port = "80";

        mg_mgr_init(&mgr, NULL);
        c = mg_bind(&mgr, s_http_port, ev_handler);
        mg_set_protocol_http_websocket(c);

        for (;;) {
            mg_mgr_poll(&mgr, 1000);
        }
        mg_mgr_free(&mgr);

        return 0;
    }

Main

int WINAPI WinMain(HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nCmdShow){

 // ...

    thread first(mainLoop,messages);   
    thread second(serverLoop);

    first.join();    

second.join();

PS. I couldn't find "Mongoos" examples. Mongoose, yes, but that is not what you asked for.

In case you need a Windows Mongoose server example, read https://github.com/cesanta/mongoose Scroll down to "Study mongoose example code". However this won't be all you need to know about Windows and tight loops.

How do I create a socket server and start receiving clients, if my main thread is processing UI messages?

I tried putting all of the server code in a thread using CreateThread() from WinMain(), but it just crashes with an error:

@jacklin. This is Addison111's post. You should create a new post with your failing code so you have your own discussion.

Also, use prior examples until you learn how to create such a system.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.