[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 691
  • Last Modified:

accepted socket not recieving events

The connected socket doesn't seem to be recieving any notifications of events, like when the client sends information, or shutdown the connection the connected socket on the server should recieve either FD_READ or FD_CLOSE which ever is appropriate but neither seems to happening.
DWORD WINAPI Network::MonitorEvents(void *ptr)
{
	WSAEVENT eventArray[EVENT_ARRAY_SIZE];
	int iEventCount = 0;
	SOCKET *sockListening = &network->sockListening;
	SOCKET *sockConnected = &network->sockConnected;
	int iReturn;
	bool *bContinue = &network->bContinue;
	int i;
	int iIndex;
	WSANETWORKEVENTS wsaNetEvents;
	int iSize;
	char szText[64];
 
	eventArray[iEventCount] = WSACreateEvent();
	if( eventArray[iEventCount] == WSA_INVALID_EVENT ){
		printf("WSACreateEvent error %d\n", WSAGetLastError() );
	}else{
		if( WSAEventSelect(*sockListening, eventArray[iEventCount], FD_ACCEPT | FD_CLOSE) == SOCKET_ERROR ){
			printf("WSAEventSelect error %d\n", WSAGetLastError() );
		}else{
			iEventCount++;
		}
	}
	
	while( *bContinue ){
		iReturn = WSAWaitForMultipleEvents(iEventCount, eventArray, FALSE, 5000, TRUE);
 
		if( iReturn == WSA_WAIT_IO_COMPLETION ){
			printf("WSA_WAIT_IO_COMPLETION...\n");
			continue;
		}else if( iReturn == WSA_WAIT_TIMEOUT ){
			printf("WSA_WAIT_TIMEOUT...\n");
			continue;
		}else if( iReturn == WSA_WAIT_FAILED ){
			printf("WSA_WAIT_FAILED... error %d\n", WSAGetLastError() );
			break;
		}else if( iReturn == WSA_WAIT_EVENT_0 ){
			iIndex = iReturn - WSA_WAIT_EVENT_0;
			
			if( iIndex == 0 ){
				WSAEnumNetworkEvents(*sockListening, eventArray[iIndex], &wsaNetEvents);
 
				if( wsaNetEvents.lNetworkEvents == FD_ACCEPT ){
					*sockConnected = accept( *sockListening, NULL, NULL);
					if( *sockConnected == INVALID_SOCKET ){
						printf("accept error %d\n", WSAGetLastError() );
					}
 
					eventArray[iEventCount] = WSACreateEvent();
					if( eventArray[iEventCount] == WSA_INVALID_EVENT ){
						printf("WSACreateEvent error %d\n", WSAGetLastError() );
					}else{
						if( WSAEventSelect(*sockConnected, eventArray[iEventCount], FD_READ | FD_CLOSE) == SOCKET_ERROR ){
							printf("WSAEventSelect error %d\n", WSAGetLastError() );
						}else{
							iEventCount++;
						}
					}
 
					printf("Client Connected\n");
				}else if( wsaNetEvents.lNetworkEvents == FD_CLOSE ){
					if( closesocket(*sockListening) == SOCKET_ERROR ){
						printf("closesocket error %d\n", WSAGetLastError() );
					}
					printf("Listening socket closed\n");
				}else if( wsaNetEvents.lNetworkEvents == FD_READ ){
					iSize = recv(*sockListening, szText, sizeof(szText), 0);
					if( iSize == SOCKET_ERROR ){
						printf("recv error %d\n", WSAGetLastError() );
					}else{
						printf(szText);
					}
				}
			}else if( iIndex == 1 ){
				WSAEnumNetworkEvents(*sockConnected, eventArray[iIndex], &wsaNetEvents);
 
				if( wsaNetEvents.lNetworkEvents == FD_READ ){
					iSize = recv(*sockConnected, szText, sizeof(szText), 0);
					if( iSize == SOCKET_ERROR ){
						printf("recv error %d\n", WSAGetLastError() );
					}else{
						printf(szText);
					}
				}else if( wsaNetEvents.lNetworkEvents == FD_CLOSE ){
					if( closesocket(*sockConnected) == SOCKET_ERROR ){
						printf("closesocket error %d\n", WSAGetLastError() );
					}
					printf("Connected socket closed\n");
				}
			}
		}
	}
 
	for(i = 0; i < iEventCount; i++){
		if( !WSACloseEvent( eventArray[i] ) ){
			printf("WSACloseEvent Error.\t\tError Code: %d\n", WSAGetLastError() );
		}
	}
	
	return 0;
}

Open in new window

0
shaun_hester
Asked:
shaun_hester
  • 2
1 Solution
 
romanmCommented:
line 38: }else if( iReturn == WSA_WAIT_EVENT_0 ){
you only hit it when your listening socket has an event

it should be:
}else if( (iReturn >= WSA_WAIT_EVENT_0) && (iReturn < (WSA_WAIT_EVENT_0+iEventCount)) ){
so you get the index for any of the events
0
 
shaun_hesterAuthor Commented:
I have been staring at this code for i don't know how long.  It's always the small things!!!
0
 
romanmCommented:
:) always good to have a second set of eyes
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now