CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2000
    Location
    India, Tamilnadu
    Posts
    279

    Close CAsyncSocket on server side

    Hi
    After sending data using "Send(...)", I would like to close an CAsyncSocket socket. But, how do I know that complete data is sent?
    If I close after "Send(...)", I get socket error.

    Thanks for the help in advance
    Anita Eugene

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Close CAsyncSocket on server side

    Read "Graceful Shutdown, Linger Options, and Socket Closure" at http://msdn2.microsoft.com/en-us/library/ms738547.aspx

    You should not be getting an error after calling send() and then closesocket(). Show us the code, and tell us what error you are getting (i.e, tell us the return value for the close function, and also tell us the return value for WSAGetLastError()).

    Mike

  3. #3
    Join Date
    Jan 2000
    Location
    India, Tamilnadu
    Posts
    279

    Re: Close CAsyncSocket on server side

    Hi Thanks for the response. Yes. I tried ShutDown(0) and then Close().
    I get error on the function below on the CAsyncSocket
    -----------------------------------------------------------------------------------
    void CAsyncSocket::Close()
    {
    if (m_hSocket != INVALID_SOCKET)
    {
    VERIFY(SOCKET_ERROR != closesocket(m_hSocket));
    CAsyncSocket::KillSocket(m_hSocket, this);
    (Error here) m_hSocket = INVALID_SOCKET;
    }
    }
    -----------------------------------------------------------------------------------
    asserting here
    ASSERT(CAsyncSocket::LookupHandle(hSocket, FALSE) != NULL);

    ----------------------------------------------------------------------------------------
    My stack
    CAsyncSocket::KillSocket(unsigned int 1884, CAsyncSocket * 0x00326f0c {CClientSock}) line 482 + 34 bytes
    CAsyncSocket::Close() line 235
    MTProcessRequestThread(void * 0x00000000) line 380 + 17 bytes
    _AfxThreadEntry(void * 0x0012f934) line 112 + 13 bytes
    _threadstartex(void * 0x00422d70) line 212 + 13 bytes
    KERNEL32! 7c80b683()

    I tried with Shutdown(0) after send. I get a FD_CLOSE after a very long time and all good. But, the time it takes for FD_CLOSE to come is very long.


    Thanks
    Anita Eugene

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Close CAsyncSocket on server side

    Is your application multi-threaded? Are you correctly Detach()'ing the socket handle in one thread when passing it in and Attach()'ing it in the other?

    Mike

  5. #5
    Join Date
    Jan 2000
    Location
    India, Tamilnadu
    Posts
    279

    Re: Close CAsyncSocket on server side

    Its a HTTP server I am working on,
    On receving GET request on "OnReceive", I call the thread below to send the data and thn suutdown.

    AfxBeginThread(MTProcessRequestThread,(LPVOID)wParam);

    Basically, its not thread based except this.

    Thanks
    Anita EUgene

  6. #6
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Close CAsyncSocket on server side

    Quote Originally Posted by AnitaEugene
    ... Basically, its not thread based except this.
    Hehe, that seems like a pretty big thread to me.

    Anyway, it explains your ASSERTs. Look at this article from Joseph Newcomer's site, which explains how to Attach/Detach the socket handle between threads, so as to avoid the ASSERTs you are seeing: http://www.flounder.com/detach.htm#Sockets

    If your code is based on KB192570, or by coincidence is similar to it, then you might also want to see his scathing (and correct) criticism of the code that Microsoft gives there: "A Rewrite of KB192570: An MFC Asynchronous Socket Example Done Right" at http://www.flounder.com/kb192570.htm

    Mike

  7. #7
    Join Date
    Jan 2000
    Location
    India, Tamilnadu
    Posts
    279

    Re: Close CAsyncSocket on server side

    Thanks. Let me go through this articles. I know I am doing some mistake. Let me try to fix it and post the result.
    Thanks
    Anita Eugene

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