Click to See Complete Forum and Search --> : Implementing non-blocking sockets
Navin
March 29th, 1999, 05:57 AM
Hi Friends,
By default member functions( e.g. Connect(), Receive() ) of CSocket class operate in blocking mode, how do I change it to non-blocking mode.
Also is there any way I can specify timeout value for such type of functions, especially Receive() function ??
Regards,
Navin.
Steve Dwire
March 29th, 1999, 08:47 AM
To make it non-blocking, use CAsyncSocket instead. CSocket is derived from CAsyncSocket, and what it adds is the blocking code.
bsmanoj
March 29th, 1999, 09:54 AM
Dear navin,
use a derived class from CSocket and try, a sample can be like this
class NavinSocekt::public CSocket
{
public:
NavinSocket();
~NavinSocket();
/*all these function are the local implementation of the virtuall callback functions
provided in the CAsyncSocket*/
void OnReceive(int nErrorCode); //this function willbe called when paket arrives
void OnSend(int nErrorCode); // will be called when a packet is sent
void OnConnect(int nErrorCode); // will be called when gets connected
void OnAccept(int nErrorCode); // will be called when accepted
void OnError(int nErrorCode); // will be calle dwhen erro happpends
void OnClose(int nErrorCode); //
}
// all the virtual call back function should be locally implemented depending on your need
void NavinSocekt::OnReceive(int nErrorCode)
{
/*will be called when a packet arrives*/
if(nErrorCode)
printf("error in on receive of NAvin socket");
char buffer[max_size];
/* this function call won't be a blocking call*/
int recdBytes = receive(buffer,max_size,.....);
/*received packet is available at buffer*/
return;
}
with this you don't have to poll for the packet arrival, instead it will call back your virtual functions
whenevera packet is arrived.
hope this will solv your issue.
thanks
bsmanoj
Samar Aarkotti
April 20th, 1999, 10:46 AM
this makes the socket non-blocking
or else AsyncSocket will do....
ioctl (Socket, FIONBIO, &action);
Dima Semenov
May 23rd, 1999, 04:32 PM
see Timeout with MFC sockets here
Ravi Bhavnani
May 23rd, 1999, 04:54 PM
Navin,
Check out http://www.codeguru.com/internet/timeoutsocket.shtml.
/ravi
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.