![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
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);
}
}
}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. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
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";
} |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
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; there there there there there there there there .. and forever 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. |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|