CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Socket problem

  1. #1
    Join Date
    Mar 2002
    Location
    California
    Posts
    11

    Angry Socket problem

    Hi all!
    I'm working on an application which Send and Receive messages on the same PC (win2000). However, it freezes whenever I run the appl. I've a feeling that the ReceiveMessage keeps waiting for the message so It uses all the time of CPU that lead to freeze the PC. If you have any solution for this problem, please help.

    thanks in advance.

  2. #2
    Join Date
    Sep 2002
    Location
    DC Metro Area, USA
    Posts
    1,509
    Not quite sure what the problem is, but if when you're debugging the app and hit the "break execution" button and the call stack shows that you're in the receive method, you're probably right.

    Generally, Receiving on a socket waits until data is present and has been read, so what you want to do is prior to receiving do a loop to check if there's any data to be read. The following is code to do so, if you're just using straight socket calls.

    while (ret <= 0)
    {
    // Reset these before every select call
    FD_ZERO(&setRd);
    FD_SET(m_sockClient, &setRd);

    timeout.tv_sec = 0;
    timeout.tv_usec = 0;

    Sleep(50);
    ret = select (0, &setRd, NULL, NULL, &timeout);

    if (ret < 0)
    {
    err = WSAGetLastError ();
    }
    }
    // do your recv here.

    There are several other methods to accomplish the same effect. WSAEventSelect sends a message to a hwnd when a specific event, like data being present, occurs. Good Luck!

  3. #3
    Join Date
    Sep 2002
    Location
    St.Louis, MO
    Posts
    27
    you could also create seperate threads for the send and recv part of your code.
    Thanks
    Tom Wright

  4. #4
    Join Date
    Sep 2002
    Location
    St.Louis, MO
    Posts
    27
    one other thing. Step through your code and see where it hangs on then let us know. It is difficult to tell with out much info.
    Thanks
    Tom Wright

  5. #5
    Join Date
    Sep 2002
    Location
    DC Metro Area, USA
    Posts
    1,509
    tawright915 & all,

    From my limited experience, even if the socket that's receiving is in a thread, it will still peg the cpu (extrodinary cpu usage). You're right how ever that it will do a lot to keep the rest of the app somewhat responsive.

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