Andrue Cope
2007-04-24 09:12:24 UTC
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?
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?