CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2002
    Location
    Romania
    Posts
    67

    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?

  2. #2
    Join Date
    Aug 2001
    Posts
    507

    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

  3. #3
    Join Date
    Sep 2002
    Location
    Romania
    Posts
    67
    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?

  4. #4
    Join Date
    Aug 2001
    Posts
    507

    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

  5. #5
    Join Date
    Sep 2002
    Location
    Romania
    Posts
    67
    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?

  6. #6
    Join Date
    Jul 1999
    Location
    Israel
    Posts
    1,793
    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 );
    }

  7. #7
    Join Date
    Jul 1999
    Location
    Israel
    Posts
    1,793
    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...
    }

  8. #8
    Join Date
    Jul 1999
    Location
    Israel
    Posts
    1,793
    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

  9. #9
    Join Date
    Jan 2003
    Location
    China
    Posts
    20
    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
  •  





Click Here to Expand Forum to Full Width

Featured