tanqiuwei
tanqiuwei
2016-03-09 08:59

socket编程,服务器断开,程序卡死在send函数

10
  • socket

请问大神们。下面的代码有什么问题的呢?为什么服务器断开。select超时是不跑的呢。谢谢!
int cflags = fcntl(pLink->iSocketFd,F_GETFL,0);
fcntl(pLink->iSocketFd,F_SETFL, cflags|O_NONBLOCK);
fd_set rds, wrs, exs;

FD_ZERO(&rds);     // 初始化  
FD_ZERO(&wrs);  
FD_ZERO(&exs);  

FD_SET(pLink->iSocketFd, &rds);    //设置socket句柄到select的FD参数中  
FD_SET(pLink->iSocketFd, &wrs);  
FD_SET(pLink->iSocketFd, &exs);  

struct timeval timeout;

if (pLink && 0 < pLink->iSocketFd)
{
    PC_SERVER_LOG("tcp send data start: addr:%s,port:%d\n", pLink->strRemoteAddr, pLink->iRemotePort);
    timeout.tv_sec  = 3;  
        timeout.tv_usec = 0;  
        PC_SERVER_LOG("lhw_mark pLink->iSocketFd = (%d)  line @@@@@\r\n", pLink->iSocketFd);
            int retSelect = select(pLink->iSocketFd, NULL, &wrs, NULL, &timeout);

            if ( retSelect == 0 ) {  
           PC_SERVER_LOG("lhw_mark select time out  429 line \r\n");
           return ET_AT_CMD_ERROR;

            } else if ( retSelect == -1) {  
            PC_SERVER_LOG("lhw_mark select Error  433 line \r\n");
            return ET_AT_CMD_SELECT_ERROR;
               // break;  
            } else {  
            PC_SERVER_LOG("lhw_mark select Total  437 line\r\n");
               // break;  
            }  

    while (0 < nRemain)
    {
        nSend = nRemain < 1024 ? nRemain : 1024;

        PC_SERVER_LOG("lhw_mark pLink->iSocketFd = (%d)  line @@@@@\r\n", pLink->iSocketFd);
        ret = send(pLink->iSocketFd, pSendPos, nSend, 0);
        if (-1 == ret)
        {
            PC_SERVER_LOG("lhw_mark error = (%d),error() @@@@@\r\n", errno);

            PC_SERVER_LOG("[et]link:(%d),send data   line\r\n", pLink->iLinkId);
            break;
        }
        PC_SERVER_LOG("lhw_mark 3333333 ret = (%d)  line @@@@@\r\n", ret);
        PC_SERVER_LOG("lhw_mark pLink->iSocketFd = (%d)  line @@@@@\r\n", pLink->iSocketFd);
        nRemain = nRemain - nSend;
        pSendPos = pSendPos + nSend;
        PC_SERVER_LOG("[et]link:%d,send %d\r\n", pLink->iLinkId, nSend);
        pLink->stDataFlow.nTxToSocket += nSend;
        pLink->stDataFlow.nTxAckSum += ret;
    }
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答