Discussion:
recv() not blocking?
(too old to reply)
Andrue Cope
2007-04-24 09:12:24 UTC
Permalink
I am developing a simple server application and I have a primary thread
which looks for incoming connections (using accept()). This thread is
correctly blocking on the socket. When an incoming connection is detected
this primary thread creates a service thread to handle the connection.

My problem is twofold:
1.Originally the service thread used WSAWaitForMultipleEvents() but it never
seemed to receive any events:

KOWinsockConnector(SOCKET connectionSocket)
:mySocket(connectionSocket),
myRunning(false),
myNetworkEvent(WSACreateEvent()),
myThreadID(GetCurrentThreadId())
{
}

// Service thread
virtual DWORD Run()
{
myRunning=true;

printf("%u:Listener thread started.\n",myThreadID);

while(true)
{
DWORD
eventID=WSAWaitForMultipleEvents(1,&myNetworkEvent,FALSE,WSA_INFINITE,FALSE);
[snip]

2.I changed the code to just call recv(). This echoes incoming data
correctly but it isn't blocking.

// Service thread
virtual DWORD Run()
{
myRunning=true;

printf("%u:Listener thread started.\n",myThreadID);

while(true)
{
char str[1024];
int recvLength = recv(mySocket,str,sizeof(str),0);
if(recvLength&&
(recvLength<sizeof(str)))
{
str[recvLength] = '\0';
printf("%u:%s\n",myThreadID,str);
}
[snip]

Can anyone shed any light on this?
Andrue Cope
2007-04-24 09:40:26 UTC
Permalink
Correction:The call to recv() blocks until it first receives something but
after that it just returns zero. At no point does it ever return a value
greater than 1024 so apparently never has an error. Even when the sender
calls closesocket() then WSACleanup() and terminates the receiver happily
sits in a loop reading zero bytes from the socket.

What gives?
Ben Voigt
2007-05-24 17:44:44 UTC
Permalink
Post by Andrue Cope
Correction:The call to recv() blocks until it first receives something but
after that it just returns zero. At no point does it ever return a value
greater than 1024 so apparently never has an error. Even when the sender
calls closesocket() then WSACleanup() and terminates the receiver happily
sits in a loop reading zero bytes from the socket.
That is how recv tells you that the socket is closed.

From http://www.die.net/doc/linux/man/man2/recv.2.html

These calls return the number of bytes received, or -1 if an error occurred.
The return value will be 0 when the peer has performed an orderly shutdown.
Post by Andrue Cope
What gives?
Arkady Frenkel
2007-04-24 16:28:52 UTC
Permalink
But accept() have no connection to WSAWaitForMultipleEvents() due to your
code. If you use aceept() just send to the working thread returned socket
handle and call recv()
for data
Arkady
Post by Andrue Cope
I am developing a simple server application and I have a primary thread
which looks for incoming connections (using accept()). This thread is
correctly blocking on the socket. When an incoming connection is detected
this primary thread creates a service thread to handle the connection.
1.Originally the service thread used WSAWaitForMultipleEvents() but it
KOWinsockConnector(SOCKET connectionSocket)
:mySocket(connectionSocket),
myRunning(false),
myNetworkEvent(WSACreateEvent()),
myThreadID(GetCurrentThreadId())
{
}
// Service thread
virtual DWORD Run()
{
myRunning=true;
printf("%u:Listener thread started.\n",myThreadID);
while(true)
{
DWORD
eventID=WSAWaitForMultipleEvents(1,&myNetworkEvent,FALSE,WSA_INFINITE,FALSE);
[snip]
2.I changed the code to just call recv(). This echoes incoming data
correctly but it isn't blocking.
// Service thread
virtual DWORD Run()
{
myRunning=true;
printf("%u:Listener thread started.\n",myThreadID);
while(true)
{
char str[1024];
int recvLength = recv(mySocket,str,sizeof(str),0);
if(recvLength&&
(recvLength<sizeof(str)))
{
str[recvLength] = '\0';
printf("%u:%s\n",myThreadID,str);
}
[snip]
Can anyone shed any light on this?
Continue reading on narkive:
Loading...