Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 25th, 2006, 4: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
Old May 25th, 2006, 4:23 PM   #2
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
This is what i was trying to do before i dove into WSAnetwork events
	while(true)
	{
             if(com->Recieve(host,buffer))
		{
		           //read buffer and do something	
		}
		else
			cout<<"\rthere";
      }
so i practically want to keep printing "there" until forever . but when i recieve a buffer over the socket form the client. then i read it and do some other operation.
hbe02 is offline   Reply With Quote
Old May 25th, 2006, 4:29 PM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
I don't know the answer to your question, I have not worked with Wsanetworkevents yet. But why are you trying to print out \r ?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 25th, 2006, 4:35 PM   #4
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
i have a lot of code to be executed instead of cout.. i just couted to make my question breif and straight to the point.
to answer you anyways.. if i used
cout<<"there"<<endl;
This is what i will get:
there
there
there
there
there
there
there
there
.. and forever
but if i use \r before "there" it will just overwrite it instead of priniting after it. so you will only have one ouputed line instead of a kazillion .
i usually use that when im debugging in an infinite loop as such. or to make a counter that keeps incrementing and views on one line only.
hbe02 is offline   Reply With Quote
Old May 25th, 2006, 7:32 PM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
I haven't use the WSA Network Events before, but a quick google showed a few examples. It looks like you need to create the handle with WSACreateEvent before you can call WSAEventSelect.
Then you need to move your WSAWaitForMultipleEvents and WSAEnumNetworkEvents calls to inside the loop, as at the moment your loop isn't waiting for any more data before looping around.
This page has a good description of what to do.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC