Bradmage
Bradmage

Reputation: 1231

C++ Win32 sockets

I have a working socket console server with multithreaded clients app. I've only created console or OpenGL apps. So a windows app is new.

I'm porting my stocket server code to a Win32 API app, and running into some trouble. The server runs, and the clients connect and communicate with each other correctly, but the server window freezes.

From my screenshot, you can see my server messages are being output to my text box. But on reaching the msg loop the app freezes.

I've updated my message loop from

while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

to

boolean running = TRUE;

while (running) {
    if (PeekMessage(&sys.msg, NULL, 0, 0, PM_REMOVE)) {
        if (sys.msg.message == WM_QUIT)
        {
            running = FALSE;
            //exitCode = msg.wParam;
            break;
        }
        TranslateMessage(&sys.msg);
        DispatchMessage(&sys.msg);
    }

    try {
        pseucode_socket_checking();
    }
    catch (std::exception& e) {
        running = FALSE;
    }
}


pseucode_socket_checking() {
    SOCKET incoming = INVALID_SOCKET;
    incoming = accept(server_socket, NULL, NULL);

    if (incoming == INVALID_SOCKET) return;

    ...

    // create thread for socket.
}

enter image description here

Upvotes: 0

Views: 234

Answers (1)

Michael Chourdakis
Michael Chourdakis

Reputation: 11148

accept() is a blocking function, it waits until a connection is made. Suggestion: put your accept() loop in a thread.

Windows needs that the message loop is unblocked. When something stucks it (like accept()), the app will freeze. So, let the message loop free from anything blocking.

Upvotes: 2

Related Questions