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 }
我有这个代码,发送功能有时会卡住。有些人指出使用非bloking I / O.我正在使用epoll所以我认为将整个设计改为非阻塞模式有点困难。有什么方法可以阻止阻止发送功能吗?
提前致谢..
答案 0 :(得分:3)
如果您不希望send
阻止,则需要非阻塞I / O.没有办法解决这个问题。
您不必将套接字置于非阻塞模式,MSG_DONTWAIT
标志可以在每个呼叫的基础上使用。但是 需要处理EAGAIN
/ EWOULDBLOCK
错误代码。
从上面链接的手册页:
flags参数是零个或多个以下标志的按位OR。
因此您可以将其与MSG_NOSIGNAL
结合使用。