View Single Post
Old May 25th, 2006, 5:15 PM   #1
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
Wsanetworkevents

alright what i want to do is listen to a socket for something to be recieved from the client side. in that time, i want to keep couting "there"
and when i recieve something, it trigerrs a network event.
this is what my main looks like:
com->sd returns the socket descriptor integer.
char*buffer = new char[50];

WSANETWORKEVENTS NetworkEvents;
HANDLE NetworkEvent;
::WSAEventSelect(com->sd, NetworkEvent,  FD_ACCEPT );

if ((WSAWaitForMultipleEvents(1, &NetworkEvent, FALSE,WSA_INFINITE, FALSE))== WSA_WAIT_FAILED)
		cout<<"error1"<<endl;
if (WSAEnumNetworkEvents(com->sd ,NetworkEvent, &NetworkEvents) == SOCKET_ERROR)
         cout<<"error2"<<endl;
while(true)
 {
     if (NetworkEvents.lNetworkEvents & FD_ACCEPT)
      {
           if (NetworkEvents.lNetworkEvents & FD_ACCEPT &&  NetworkEvents.iErrorCode [FD_ACCEPT_BIT] == 0)
           {
				 cout<<"here";
				 break;      	
           }
           else
           {
                  cout<<"\rthere";
				  com->Recieve(host,buffer);
           }
    }
}
and this is the com->Recieve(host,buffer) function
bool Mysocket::Recieve(sockaddr_in & client, char * & buffer)
{
	int clen = sizeof (sockaddr_in);
	cout<<"Listening on Socket "<<sd<<"...."<<endl;
		listen(sd,1);   //listen to created socket for connection (1 connection max)
	
		if(accept(sd,0,0) != -1) //accept connection on socket
		{
			return false;	
		}
		else
		{
			char* temp = new char[25000];
		
			int size = 	recvfrom(sd,temp,strlen(temp),0,(struct sockaddr *) &client,&clen);	//get recieved input and put in temp
			
			for (int i = 0 ; i <size ; i++)
			{
				buffer[i] = temp[i];
			}
			buffer[i] = NULL;
			
			
			delete temp;
			return true;
		
		}
		
}

this is the first time i use WSAevents, i cant seem to find a good resource to learn more, i have a feeling the structure is all wrong and all over the place.

Appreciate the help guys.
hbe02 is offline   Reply With Quote