Implementing non-blocking sockets
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.
Re: Implementing non-blocking sockets
To make it non-blocking, use CAsyncSocket instead. CSocket is derived from CAsyncSocket, and what it adds is the blocking code.
Re: Implementing non-blocking sockets
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
Re: Implementing non-blocking sockets
this makes the socket non-blocking
or else AsyncSocket will do....
ioctl (Socket, FIONBIO, &action);
Re: Implementing non-blocking sockets
see Timeout with MFC sockets here
Re: Implementing non-blocking sockets