Click to See Complete Forum and Search --> : Getting Socket Events


jamesbr
September 21st, 2002, 02:30 PM
Hi,
I have a server/client program that uses winsock (im not using the CSocket class). Is it possible to have a function called when data is recieved by the socket? Thanks

Andreas Masur
September 21st, 2002, 03:41 PM
If you are talking about any kind of event that will occur then the answer is no - at least to my knowledge. and depending on the operating system. In Windows you can work with 'WSA...' events but they are not available on other operating systems...

However you can use the 'select()' function to determine when more data arrives...

int iRC = 0;
timeval SendTimeout;

// Set timeout
SendTimeout.tv_sec = 0;
SendTimeout.tv_usec = 250000; // 250 ms

fd_set fds;

FD_ZERO(&fds);
FD_SET(Socket, &fds);

iRC = select(0, &fds, NULL, NULL, &SendReceiveTimeout);

if(!iRC)
// Timeout occured
else if(iRC < 0)
// Error -> call 'WSAGetLastError()'
else
// Data arrives

Based on this you can implement some kind of notification on your own...

kuphryn
September 22nd, 2002, 11:49 AM
Yes. You could call a function to receive data. Receiving data has nothing to do with your software design. Heck, you could call a function in another class to receive data. You just have to pass a handle of the socket to the function.

What I/O model are you using?

Kuphryn