- 论坛徽章:
- 1
|
各位,我在设置非堵塞socket下使用send函数存在极小的几率被堵塞,下面是代码:- static int32_t SendDataPacket4Schedule(const uint8_t *data, const int32_t len)
- {
- fd_set fdsWrite;
- struct timeval stTimeoutVal;
- int nRet, nSndBytes;
- FD_ZERO(&fdsWrite);
- FD_SET(GetDataInOutSocket(), &fdsWrite);
- stTimeoutVal.tv_sec = TIMEOUT_SOCKET_SEND;
- stTimeoutVal.tv_usec = 0;
- //TODO:这里的处理貌似是有问题的,参见下面的“不管”注释部分
- nRet = select(GetDataInOutSocket() + 1, NULL, &fdsWrite, NULL, &stTimeoutVal);
- if(nRet > 0)
- {
- if(FD_ISSET(GetDataInOutSocket(), &fdsWrite))
- {
- //* 如果相等,表明发送成功,立即发送下一个报文
- debug("[send prepare]SendDataPacket:[0x%x] length:%d.\r\n",
- GetDataInOutSocket(), len);
- nSndBytes = send(GetDataInOutSocket(), data, len, MSG_NOSIGNAL);
- if(len != nSndBytes)
- {
- PERROR("tcp_output() send error");
- debuginfo("[send]SendDataPacket() send [0x%x]:%d, error:%s\r\n",
- GetDataInOutSocket(), nSndBytes, strerror(errno));
- }else{
- #if 1
- debug("[send]SendDataPacket:[0x%x] length:%d.\r\n",
- GetDataInOutSocket(), len);
- showhex((uint8_t *)data, len);
- alert("[send]SendDataPacket OK.");
- #endif
- }
- }else{
- nSndBytes = 0;
- }
- }
- else if(nRet == 0)
- {
- debuginfo("send Schedule Data Time Out: [0x%x]!\r\n", GetDataInOutSocket());
- nSndBytes = -2;
- }
- else
- {
- PERROR("tcp_output() select error");
- debuginfo("tcp_output() select error [0x%x] :%s\r\n", GetDataInOutSocket(), strerror(errno));
- nSndBytes = -1;
- }
- return nSndBytes;
- }
复制代码 上面的send部分会被堵塞,我的日志输出显示:
debug("[send prepare]SendDataPacket:[0x%x] length:%d.\r\n",
GetDataInOutSocket(), len);
成功执行之后被挂起,各位觉得有这种可能么? |
|