|
-
September 9th, 2002, 03:36 PM
#1
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.
-
September 9th, 2002, 03:52 PM
#2
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!
-
September 9th, 2002, 04:17 PM
#3
you could also create seperate threads for the send and recv part of your code.
Thanks
Tom Wright
-
September 9th, 2002, 04:18 PM
#4
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
-
September 10th, 2002, 11:19 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|