Windows sockets
-
This is probably a very basic question, so bear with me :) I'm writing a console program and need to use asynchronous sockets. I wish to use WSAAsynchSelect(). The second parameter is a HWND to the window you want to handle the messages. The third parameter is the message you want handled. The events are specified in the fourth parameter. What I thought I could do was use a message loop, like while( bRet = GetMessage( &msg, NULL, 0, 0 ) != 0 ) { if( bRet == -1 ) { // handle } TranslateMessage( &msg ); DispatchMessage( &msg ); } and have the WinProc handle the message specified in WSAAsynchSelect(). The problem I'm having is that I get an "Invalid Argument" error (runtime) when WSAAsynchSelect() is called and I think it's because I'm sending a NULL instead of a valid HWND. My question is, does anyone know have advice how to use WSAAsynchSelect() in a console application? Thanks! mweiss
-
This is probably a very basic question, so bear with me :) I'm writing a console program and need to use asynchronous sockets. I wish to use WSAAsynchSelect(). The second parameter is a HWND to the window you want to handle the messages. The third parameter is the message you want handled. The events are specified in the fourth parameter. What I thought I could do was use a message loop, like while( bRet = GetMessage( &msg, NULL, 0, 0 ) != 0 ) { if( bRet == -1 ) { // handle } TranslateMessage( &msg ); DispatchMessage( &msg ); } and have the WinProc handle the message specified in WSAAsynchSelect(). The problem I'm having is that I get an "Invalid Argument" error (runtime) when WSAAsynchSelect() is called and I think it's because I'm sending a NULL instead of a valid HWND. My question is, does anyone know have advice how to use WSAAsynchSelect() in a console application? Thanks! mweiss
I guess you need to create some window to receive the messages - even in console app you can create windows - best choice will be some invisible - message only window (parent is
HWND_MESSAGE
). Then you'll have all - validHWND
, valid MsgProc for that window etc. Hope this helps -
I guess you need to create some window to receive the messages - even in console app you can create windows - best choice will be some invisible - message only window (parent is
HWND_MESSAGE
). Then you'll have all - validHWND
, valid MsgProc for that window etc. Hope this helpsThanks for the reply. I found a way to do it without using the windows. I use WSAEventSelect(), WSAWaitForMultipleEvents() and WSAEnumNetworkEvents(). I set up the events in the main thread and in a worker thread wait for events to occur, check which event it was and handle it. I haven't tested it yet, tho. Thanks. melinda
-
Thanks for the reply. I found a way to do it without using the windows. I use WSAEventSelect(), WSAWaitForMultipleEvents() and WSAEnumNetworkEvents(). I set up the events in the main thread and in a worker thread wait for events to occur, check which event it was and handle it. I haven't tested it yet, tho. Thanks. melinda
Yes, that's the better method, but you stated that you wish to use the before method, and I prefer to respect ppl. wishes ;-) Anyway, this is much better what you do now, the messaging part (window) is useful only when you have to handle the winsock 1. Only disadvantage is that you have two threads - mean the whole design is more complicated due to resource locking etc. I usualy trying to design it that it uses only one thread for all that tasks. But it really depends on the application, so there cannot be a general advice except that I said. wish you good luck!