Reputation: 13998
119 while(remainLength > 0){
120 if(remainLength >= MAX_LENGTH){
121 log("WorkHandler::workLoop, remain %d > max %d \n", remainLength, MAX_LENGTH);
122 currentSentLength = send(client->getFd(), sBuffer, MAX_LENGTH, MSG_NOSIGNAL);
123 log("currentSentLength %d \n", currentSentLength);
124 }
125 else{
126 log("WorkHandler::workLoop, remain %d < max %d \n", remainLength, MAX_LENGTH);
127 currentSentLength = send(client->getFd(), sBuffer, remainLength, MSG_NOSIGNAL);
128 log("currentSentLength %d \n", currentSentLength);
129 }
130
131
132 if(currentSentLength == -1){
133 log("WorkHandler::workLoop, connection has been lost \n");
134 break;
135 }
136 sBuffer += currentSentLength;
137 log("sBuffer %d\n", sBuffer);
138
139 remainLength -= currentSentLength;
140 log("remainLength %d \n", remainLength);
141
142 }
I have this code and the send function sometimes gets stuck. Some people points out that using non bloking I/O. I am using epoll so I think it is a little bit hard to change the whole design to non blocking mode. Is there any way to prevent blocking the send function?
Thanks in advance..
Upvotes: 0
Views: 243
Reputation: 206831
If you don't want send
to block, you'll need non-blocking I/O. There's no way around that.
You don't have to put the socket in non-blocking mode though, the MSG_DONTWAIT
flag can be used on a per-call basis. But you will need to deal with EAGAIN
/EWOULDBLOCK
error codes.
From the man page linked above:
The flags argument is the bitwise OR of zero or more of the following flags.
so you can combine that with MSG_NOSIGNAL
.
Upvotes: 3