tags:

views:

16

answers:

1

Hi All I am using a non blocking Socket for sending messages.We were getting EGAIN error occassioanally .So I have decided to use Flush(socket) to flush the buffer and make space for new space so that i can avoid EGAIN error .But the problem is Flush(socket) is stuck for indefinite time .

Here is the code

 int res = send(socket, buffer, size+lengthSize,0);


 delete buffer; 

 if ( res== -1 ) 

{

 int error = errno;

 cout("ERROR on SendOnPortString, errno = " << error);

 return 0 ;

 }

 else

 {

  cout<<"Send SucessFul = " << res << "Total Message size"<< size+lengthSize;

  if(res==size+lengthSize)

   flush((ostream&)socket);

  //flush(socket); 


   return 1  ; 

  }

This code printing Send SucessFul = 11Total Message size 11

But after that its getting stuck in flush(socket) method .Any Idea why its behaving like that

A: 

You cast a socket handle of type int to a reference to a std::ostream in order to avoid compiler warnings/errors when you tried to hand it to flush. I'm surprised it's not crashing.

You can't make more space. The problem isn't you: the problem is that the system's internal buffers are full, and they will drain at their own pace in their own time. You can either poll by trying to send over and over till it works (in which case, why are you using non-blocking sockets?), or you use select, poll, kqueue, epoll, libevent, etc. to sleep till the socket is able to accept more data.

Jeremy W. Sherman
Thanks for the reply .Can you please tell me what are the situation we may get EGAIN error .As I have set the buffer size also with gud value .And i am just sending 18 messages of 20 bytes.So I dont think its giving EGAIN because of buffer full.Any other reason it may happen?
It will reply `EAGAIN` if it was going to block for any reason. You don't mention your platform; if it's open, just check the kernel source code. Why guess unnecessarily?
Jeremy W. Sherman