Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problems with CSocket

Problems with CSocket

Scheduled Pinned Locked Moved C / C++ / MFC
c++sysadminquestionlearning
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sprudling
    wrote on last edited by
    #1

    I'm trying to create a simple IRC client for learning purposes. I made it singlethreaded to begin with and everything worked great. But after I added a thread to handle incoming messages from the IRC server i keep getting an assertion when there isn't anything to recieve. I've traced it down to this...

    while ((nResult = CAsyncSocket::Receive(lpBuf, nBufLen, nFlags)) == SOCKET_ERROR)
    {
    if (GetLastError() == WSAEWOULDBLOCK)
    {
    if (!PumpMessages(FD_READ))
    return SOCKET_ERROR;
    .
    .
    .

    This code above is taken from within the function (CSocket::Receive) I call to recieve data from the IRC server, and as long as there is data to recieve CAsyncSocket::Receive returns the number of bytes read and everything is ok. But when there isn't GetLastError() returns WSAEWOULDBLOCK and CSocket::PumpMessages is beeing called...:

    ASSERT(m_pbBlocking == NULL);
    _AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
    ASSERT(pState->m_hSocketWindow != NULL);

    The above code is the beginning of CSocket::PumpMessages, and this last assertion is the one I'm talking about. Why is PumpMessages called at all? I just want Recieve to return whatever data is waiting to be recieved, and if there is none just wait for some :) Sprudling :confused: PS! I created the new thread using ::CreateThread(), not using the CWinThread MFC class. Could that have anything to do with it?

    P 1 Reply Last reply
    0
    • S Sprudling

      I'm trying to create a simple IRC client for learning purposes. I made it singlethreaded to begin with and everything worked great. But after I added a thread to handle incoming messages from the IRC server i keep getting an assertion when there isn't anything to recieve. I've traced it down to this...

      while ((nResult = CAsyncSocket::Receive(lpBuf, nBufLen, nFlags)) == SOCKET_ERROR)
      {
      if (GetLastError() == WSAEWOULDBLOCK)
      {
      if (!PumpMessages(FD_READ))
      return SOCKET_ERROR;
      .
      .
      .

      This code above is taken from within the function (CSocket::Receive) I call to recieve data from the IRC server, and as long as there is data to recieve CAsyncSocket::Receive returns the number of bytes read and everything is ok. But when there isn't GetLastError() returns WSAEWOULDBLOCK and CSocket::PumpMessages is beeing called...:

      ASSERT(m_pbBlocking == NULL);
      _AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
      ASSERT(pState->m_hSocketWindow != NULL);

      The above code is the beginning of CSocket::PumpMessages, and this last assertion is the one I'm talking about. Why is PumpMessages called at all? I just want Recieve to return whatever data is waiting to be recieved, and if there is none just wait for some :) Sprudling :confused: PS! I created the new thread using ::CreateThread(), not using the CWinThread MFC class. Could that have anything to do with it?

      P Offline
      P Offline
      Paul A Howes
      wrote on last edited by
      #2

      When you create sockets in Windows using SDK functions, they are generally blocking, meaning that if no data is available for a receive, the application would hang until data is ready. However, the CAsyncSocket class hides a lot of implementation details from you. When you create a socket based on this class, the calls are non-blocking. The idea is that your program will respond to networking events, rather than waiting for something to happen. Receiving "WSAEWOULDBLOCK" is more of an informational message than an error. All it is telling you is that if you were using the blocking version of that function, it would be blocking right now. What you should do is create a subclass of CAsyncSocket, and overload the "OnReceive" function. Any time data is available from the network, this function is called. You can put it in your screen buffer and display it at that point. -- Paul "I drank... WHAT?"

      S 1 Reply Last reply
      0
      • P Paul A Howes

        When you create sockets in Windows using SDK functions, they are generally blocking, meaning that if no data is available for a receive, the application would hang until data is ready. However, the CAsyncSocket class hides a lot of implementation details from you. When you create a socket based on this class, the calls are non-blocking. The idea is that your program will respond to networking events, rather than waiting for something to happen. Receiving "WSAEWOULDBLOCK" is more of an informational message than an error. All it is telling you is that if you were using the blocking version of that function, it would be blocking right now. What you should do is create a subclass of CAsyncSocket, and overload the "OnReceive" function. Any time data is available from the network, this function is called. You can put it in your screen buffer and display it at that point. -- Paul "I drank... WHAT?"

        S Offline
        S Offline
        Sprudling
        wrote on last edited by
        #3

        Thank you VERY much, it worked perfectly :-D ;P Sprudling

        P 1 Reply Last reply
        0
        • S Sprudling

          Thank you VERY much, it worked perfectly :-D ;P Sprudling

          P Offline
          P Offline
          Paul A Howes
          wrote on last edited by
          #4

          You're welcome! I just spent the past few days working with MFC's CAsyncSocket class, so I through I might be able to help :) -- Paul "I drank... WHAT?"

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups