-
July 3rd, 2006, 07:18 AM
#1
TCP-Socket.receive does not return. Why?
Hi!
Globally defined:
CSocket sock;
In a function:
TRACE("\r\nTCPRead before sock.Receive");
temp = sock.Receive(IBuf,64);
TRACE("\r\nTCPRead after sock.Receive");
in InitInstance:
AfxSocketInit(&WsaData);
Doing this the Receive-function never returns. Any hint?
Thanks a lot!
Marc
-
July 3rd, 2006, 07:28 AM
#2
Re: TCP-Socket.receive does not return. Why?
If you're using blocking sockets the Receive call will wait until 1 or more bytes are read (socket is closed or an error occurs).
- petter
-
July 3rd, 2006, 07:32 AM
#3
Re: TCP-Socket.receive does not return. Why?
Maybe no one sends data to that socket?
-
July 3rd, 2006, 07:37 AM
#4
Re: TCP-Socket.receive does not return. Why?
OK: I even expect that no one sends a byte. But in this case there should be a timout?
How can I make the function waiting only a certain time?
But already you helped me!
Thanks!
Marc
-
July 3rd, 2006, 07:42 AM
#5
Re: TCP-Socket.receive does not return. Why?
You should take a look at non-blocking or asynchronous sockets. CAsyncSocket is one solution.
- petter
-
July 3rd, 2006, 07:50 AM
#6
Re: TCP-Socket.receive does not return. Why?
Indeed you should use CASyncSocket or the WinSock API, because there are some problems with CSocket (though not in your example), because it tries to emulate blocking using a non-blocking socket. I don't know why most network programming beginners use CSocket. Maybe because of bad tutorials?
Please don't forget to rate users who helped you!
-
July 3rd, 2006, 07:55 AM
#7
Re: TCP-Socket.receive does not return. Why?
Originally Posted by Marc from D
How can I make the function waiting only a certain time?
I think this can be done with select function:
Code:
long ms = ... // timeout in milliseconds
SOCKET s = sock;
fd_set set;
FD_ZERO(&set);
FD_SET(s, &set);
timeval timeout;
timeout.tv_sec = ms / 1000;
timeout.tv_usec = (ms % 1000) * 1000;
int const r = select(((int)s) + 1, &set, 000, 000, &timeout);
switch(r)
{
case 0:
// timeout
break;
case 1:
// OK. Data available
break;
default:
// Error
}
I hope it helps.
-
July 5th, 2006, 03:18 PM
#8
Re: TCP-Socket.receive does not return. Why?
Originally Posted by Marc from D
OK: I even expect that no one sends a byte. But in this case there should be a timout?
How can I make the function waiting only a certain time?
Changing to raw winsock API calls, or to CAsyncSocket, will add to the complexity of your program.
If all you want is a CSocket with a timeout, then see the CTimeoutScoket class in this MSDN article: "How To Configure a Time-Out on a CSocket Operation" at http://support.microsoft.com/kb/q138692/
The class is very short. It extends CSocket functionality by adding two public functions ( SetTimeOut and KillTimeOut ) and one protected function ( OnMessagePending ).
Mike
-
July 5th, 2006, 09:52 PM
#9
Re: TCP-Socket.receive does not return. Why?
Originally Posted by Marc from D
OK: I even expect that no one sends a byte. But in this case there should be a timout?
How can I make the function waiting only a certain time?
But already you helped me!
Thanks!
Marc
You can use non-blocking socket.
There are 2 ways to realize non-blocking socket:
1. Using setsockopt() function to set timeout of read/write operation.
Note: According to MSDN, SO_RCVTIMEO and SO_SNDTIMEO are not
supported in CAsyncSocket.SetSockOpt().
2. Using timer to stop blocking.
If you make a program using MFC, you can use CAsyncSocket and
overwrite event handler OnReceive()/OnSend() to read/write data.
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
|