同时处理2条消息

问题描述

我有以下代码能够通过tcp接收数据并能够发送 数据同时超过winsock。但是,队列中只有一条消息得到处理,我希望能够同时处理击键并通过套接字异步接收数据。

在不阻塞消息队列之一的情况下如何做到这一点

#define WM_SOCKET_MSG (WM_APP + 1)

int main()
{
    Init();
    S = ConnectToServer("192.168.2.10",54782);

    char _buf[1024] = { 0 };

    /*
    HHOOK WINAPI SetwindowsHookEx(
      _In_ int       idHook,_In_ HOOKPROC  lpfn,_In_ HINSTANCE hMod,_In_ DWORD     dwThreadId
    );
    */
    // Start the hook of the keyboard
    KeyboardHook = SetwindowsHookEx(
        WH_KEYBOARD_LL,// low-level keyboard input events
        HookProcedure,// pointer to the hook procedure
        GetModuleHandle(NULL),// A handle to the DLL containing the hook procedure 
        NULL //desktop apps,if this parameter is zero
    );
    if (!KeyboardHook) {
        // Hook returned NULL and Failed
        GetLastError();
    }
    else
    {

        WSAAsyncSelect(S,NULL,WM_SOCKET_MSG,FD_READ | FD_WRITE | FD_CLOSE);

        int n;
        char command[20];
        MSG Msg;
        while (GetMessageW(&Msg,0) > 0)
        {
            switch (Msg.message)
            {
                case WM_SOCKET_MSG:
                {
                    int event = WSAGETSELECTEVENT(Msg.lParam);
                    int error = WSAGETSELECTERROR(Msg.lParam);
                    if (error != 0)
                    {

                        break;
                    }

                    switch (event)
                    {
                    case FD_READ:
                        n = recv(S,_buf,sizeof(_buf) - 1,0);
                        if (n > 0) {
                            // process data as needed ...
                            MessageBoxA(0,0);
                        }
                        else {
                            //error
                        }
                        break;
                    }
                }

            break;
            }

            // process other messages as needed ... 
            TranslateMessage(&Msg);
            dispatchMessage(&Msg);
        }
    }

    unhookKeyboard();
    // Exit if failure
    return 0;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)