2 tanqiuwei tanqiuwei 于 2016.03.09 16:59 提问

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

请问大神们。下面的代码有什么问题的呢?为什么服务器断开。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个回答

oyljerry
oyljerry   Ds   Rxr 2016.03.09 17:06

先看你的超时是否设置成功了,然后send确实是否没有返回。

tanqiuwei
tanqiuwei   2016.03.09 18:16

超时是成功了,send也没返回值的。

tanqiuwei
tanqiuwei   2016.03.18 15:52

怎么能看出超时是成功的呢?
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;

        } 
        这里一直没有打印信息的。
        谢谢!
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!