-
February 18th, 2003, 06:38 AM
#1
CSocket notifications question
Is there any way to redirect the CSocket notifications to a message queue of a thread instead of a window? The WSAAsyncSelect function take as parameter the hwnd of window that will receive the notification and I don't want to create any window in my app. I saw that if I use CSocket class in an console app or in a service app I cannot receive notifications from my socket (the On-methods :OnAccept(),OnReceive() etc are never called). Is there any method to use these features of CSocket class without creating any window?
-
February 18th, 2003, 06:55 AM
#2
Socket Notifications
Hello Blue Monk:
Ur problem is a classic one, and there are some easy workarounds to this, every one gets stuck in this, that the call back functions are implemented in the CSock, or CAsyncSock class, where as they are needed from where we make objects of the above derived class and use them...... I think this is ur problem, Now the solution to it,
Sol 1. Make a custom Windows message, and it's handler shall be in the class where u want to use the socket notification, thus the operating system will call the socket call back funtion, the implementation of that funtion will send a WM_USER_DEFINED message which will be caught where u need the handling on the recv or send messages. Got it !! ?????
Sol 2. Make a Mutex, so that if u have multiple threads, the mutex shall pertain to a structure which will contain flags (bools) with respect to all the socket notification messages, .....
Sol 3. Implement a function in the class u derived from the CSocket class, that shall get/set a flag representing a socket call back,
I love the fst solutoin and trust me , it is the easiest and the most effecient,
If u find this helpful do remember to pull a poll
Just a word of appreciation
-
February 18th, 2003, 07:14 AM
#3
Let's see if I understood the first solution. You say that I should use PostThreadMessage(dwMyThreadID,WM_USER_DEFINED,0,0L) in the socket callback function? And then I have to call GetMessage or PeekMessage in my thread and that's all? Is logical, but how can I access this socket callback function using the CSocket class?
-
February 18th, 2003, 07:42 AM
#4
Sockets
No No No
This is not what i meant
See windows allows u to make ur own Messages,
so u make a message i.e WM_OurSockMsg
Then u Register that message
Then u send that Message
Then u call PeekMessage() to empty the queue
This all will be done in the class that u derived fromt eh CSocket or CAsyncSocket class.
In the thread portion, u will add the Window Messge handler, that will respond to the Message sent by the Above, for that u will also edit the messge map by hand
Got it. ?
It's like making a Custom Message, like (WM_KEYDOWN etc) that is owned by us, and that is sent by us, and handeled by us.
With this approach ur PRoblem with windows will also be solved, as u can send the message to all windows on a system
If you found my reply to be useful, please dont hesitate to rate it.
DO NOT kick the Axe if it doesnt fall on your foot.
Salman
-
February 19th, 2003, 01:23 AM
#5
So you say that I should use RegisterWindowMessage() in the class that I derived from CSocket and then I should post the message returned by RegisterWindowMessage to the thread that I want to receive the notification? Seems to work, but how do I know when to post the message (in the class derived from CSocket)? Overriding the On-methods? If I do not create any window in my app the On-methods of CSocket class are never called. Sorry for bothering you, I'm just new in sockets programming. Could you give me a few lines of code, please?
-
February 19th, 2003, 01:33 AM
#6
Hi...
i have another idea:
consider this code snipet:
// this called when i ACCEPT new connection NOT in my thread
void CSomeClass::Function()
{
m_MyListenSocket.Accept( sock );
}
-
February 19th, 2003, 01:34 AM
#7
Hi...
i have another idea:
consider this code snipet:
// this called when i ACCEPT new connection NOT in my thread
void CSomeClass::Function()
{
CSocket so;
m_MyListenSocket.Accept( so);
//Start you thread in sussped mode
AfxBeginThread( bla bla bla )
//then...
}
-
February 19th, 2003, 01:38 AM
#8
Hi...
i have another idea:
consider this code snipet:
// this called when i ACCEPT new connection NOT in my thread
void CSomeClass::Function()
{
// Class derived from win thread...
CMyWintheradClass pThread;
CSocket so;
m_MyListenSocket.Accept( so);
//Start you thread in sussped mode
pThread = (CMyWintheradClass )AfxBeginThread( bla bla bla )
//then...
pThread->SetSocket( so.Detach() )
// Resume the thread here
}
void CMyWinThreadClass::SetSocket( SOCKET so )
{
m_MySocket.Attach ( so );
}
This should work.... ( working for me )
Good luck
kishk
-
March 15th, 2003, 10:46 AM
#9
Who can give me some code or detail article about add SOCKET notifications message . Thanks.
chequan@chequan.com
CheQuan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|