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..
If you don’t want
sendto 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_DONTWAITflag can be used on a per-call basis. But you will need to deal withEAGAIN/EWOULDBLOCKerror codes.From the man page linked above:
so you can combine that with
MSG_NOSIGNAL.