Thread: The Winsock same code works in a console program, but doesn't in a windows app

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    2

    The Winsock same code works in a console program, but doesn't in a windows app

    I have a server code that works when run in a console program, but hangs when run with windows API. The server code is placed in wWinMain in the windows main loop


    Code:
        WSADATA wsaData;
        SOCKET  listenfd, socketfd;
        static struct sockaddr_in cli_addr;  /* static = initialised to zeros */
        static struct sockaddr_in serv_addr; /* static = initialised to zeros */
        int hit;
        int  length;
    
    
    
    
        WSAStartup(MAKEWORD(2, 2), &wsaData);
    
    
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        serv_addr.sin_port = htons(80);
    
    
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
        bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
        listen(listenfd, 64);
    
    
    
    
        while (GetMessage(&msg, nullptr, 0, 0))
        {
            if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
    
    
            length = sizeof(cli_addr);
            socketfd = accept(listenfd, (struct sockaddr *) &cli_addr, &length);
            recv(socketfd, buffer, 8096, 0);
            sprintf_s(buffer, "<html><body><H1>Hello world</H1></body></html>", 46);
            send(socketfd, buffer, strlen(buffer), 0);
            shutdown(socketfd, SD_BOTH);
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,483
    Because your windows message loop must not block.

    You can't bum around waiting for recv to eventually return and still have a responsive GUI.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2018
    Posts
    2
    Quote Originally Posted by Salem View Post
    Because your windows message loop must not block.

    You can't bum around waiting for recv to eventually return and still have a responsive GUI.
    Is there a way around this?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,483
    Well "it depends".
    Winsock Programmer’s FAQ: Which I/O Strategy Should I Use?

    You could put the bulk of the work into a thread, then use WM_TIMER to trigger periodic checking to see if the thread has done it's work.
    Processes and Threads | Microsoft Docs
    But beware of the many traps in the outward simplicity of just creating a thread.
    Shared data between threads is a real PITA, and if you get it wrong, it's nigh on impossible to debug.

    Or you could go for a more involved asynchronous I/O approach.
    Synchronous and Asynchronous I/O | Microsoft Docs
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-09-2017, 01:46 AM
  2. Replies: 41
    Last Post: 12-16-2010, 10:17 PM
  3. Why <complete program> doesn't works right?
    By Gamer_Jimbo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2006, 02:28 AM
  4. Code doesn't output to console
    By smitsky in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2004, 08:41 AM
  5. Replies: 6
    Last Post: 01-07-2002, 02:46 AM

Tags for this Thread