Home > Software engineering >  How to simulate EAGAIN or EWOULDBLOCK when send data to socket
How to simulate EAGAIN or EWOULDBLOCK when send data to socket

Time:07-09

I have a non-blocking socket to send data to. When sending data to a socket in non-blocking mode, we can get EAGAIN or EWOULDBLOCK if there is not enough space in the socket buffer. If the send call returns EAGAIN or EWOULDBLOCK, I subscribe to the EPOLLOUT event in epoll to know when the socket is ready to resend data in future.

However, in practice, I have never been in a situation where send would return EAGAIN or EWOULDBLOCK and i am not get EPOLLOUT event from epoll. For this reason, I can't test code that resend data when the EPOLLOUT event is received. How i can test my code in this case?

CodePudding user response:

How i can test my code in this case?

This depends a lot on your kind of application and protocol. In general to trigger the condition the socket write buffer needs to fill up.

This can be done by sending lots of data and making sure that the receiver application does not read from the socket. But this will not work if your specific application protocol does not allow for this, like application protocol has small message size and expects every message to be acknowledged by recipient before transmitting next.

This can also be done by decreasing the size of the socket buffer (using setsockopt with SO_SNDBUFSIZ) - but it is not clear if the minimal buffer size is sufficient to trigger the condition in your specific use case.

  • Related