x-2010 2018-01-08 10:26 采纳率: 100%
浏览 1262
已采纳

客户端断开服务端就会产生异常并断开

WIN10上开发一个服务端程序,它能监听来自多个客户端的请求并连接,其中客户端会往服务端传送文件,使用SOCKET tcp协议。现在是每次客户端发送完文件后,服务端也能正常接收完文件,但接收完后就会产生异常退出。是开的线程来运行接收函数,现在要怎样定位问题呢?exe直接退出要应该怎么抓取log呢? 接收部分代码如下:
DWORD CClient::RecvDataThread(void* pParam)
{
CClient pClient = (CClient)pParam; //pointer to client
int reVal; //return value
char temp[MAX_NUM_BUF]; //temp value

WriteToLog("RecvDataThread");
cout <<"RecvDataThread"<<endl;

while(pClient->m_bConning)              //connection status
{   
//  if(!pClient->m_bSendConnectionSuccess)
//  {
//      break;
//  }
    cout <<"pClient->m_bConning"<<endl;
    memset(temp, 0, MAX_NUM_BUF);
    reVal = recv(pClient->m_socket, temp, MAX_NUM_BUF, 0);  //receive data

    //handle error return values
    if (SOCKET_ERROR == reVal)
    {
        int nErrCode = WSAGetLastError();

        if ( WSAEWOULDBLOCK == nErrCode )   //receive data buffer is invalid
        {
            continue;                       //continue loop
        }else if (WSAENETDOWN == nErrCode ||//client close connection
                 WSAETIMEDOUT == nErrCode ||
                WSAECONNRESET == nErrCode )
        {
            break;                          //thread exit
        }
    }

    //client close the connection
    if ( reVal == 0)
    {
        break;
    }

    //receive data
    if (reVal > 0)
    {
        cout <<"reVal > 0"<<endl;
        EnterCriticalSection(&pClient->m_cs);
        char *pClientIP = inet_ntoa(pClient->m_addr.sin_addr);
        u_short  clientPort = ntohs(pClient->m_addr.sin_port);
    //  cout<<"IP: "<<pClientIP<<"\tPort: "<<clientPort<<":"<<temp<<endl;      //output and show data
        WriteToLog(temp);
        char file_name[MAX_NUM_BUF];
        char *pfile = temp; // indicate path
        memset(file_name, 0, MAX_NUM_BUF);
        strncpy_s(file_name, "F:\\receive\\", strlen("F:\\receive\\"));
        int file_len = strlen(temp), i = 0, tem = 0;

        for (i = 0; i < file_len; i++)
        {
            if (strncmp(pfile + file_len - 1 -i, "\\", 1))  //if equal, return 0; else, return Positive
            {
                tem++;
                continue; //not equal
            }
            else // if equal, strcat path after \\  to file_name, it's exact length of path after 
                {
                    strncat_s(file_name, pfile + file_len - i, i);
                    break;
                }
        }

        if (tem == file_len)
        {
            strncat_s(file_name, temp, strlen(temp) > 1024 ? 1024 : strlen(temp));
        }

        FILE *fp;
        fopen_s(&fp, file_name, "wb");
        int len = send(pClient->m_socket, "Ready to send", strlen("Ready to send") + 1, 0);
        memset(sendMsgLogA, 0, 1024);
        sprintf_s(sendMsgLogA, "%s%d", "send size: ", len );
        WriteToLog(sendMsgLogA);

        char datalength[20];
        long int length = 0;
        int lenRecv = recv(pClient->m_socket, datalength, 21, 0);  //send file size from Client
        length = atol(datalength);

        memset(sendMsgLogA, 0, 1024);
        sprintf_s(sendMsgLogA, "%s%ld%s%ld", "file size: ", length, " recv size:", lenRecv);
        WriteToLog(sendMsgLogA);                //ready to send

        double cent = 0.0;
        char receiveBuf[SIZE];
        long int x = 0;
        while (1)
        {           
            cout <<"while (1)"<<endl;
            x = x + SIZE;  //SIZE scope is from -128 to 127
            if (x < length) // ZJX
            {
                cent = (double)x*100.0 / (double)length;
                memset(sendMsgLogA, 0, 1024);
                sprintf_s(sendMsgLogA, "%s%4.2f", "have received: ", cent);
                WriteToLog(sendMsgLogA);                //ready to send
                recv(pClient->m_socket, receiveBuf, SIZE + 1, 0);      //recv SIZE files
                fwrite(receiveBuf, 1, SIZE, fp);             //write SIZE files into receiveBuf, and continue to loop
            }
            else  //excute the function directory while files is smaller, and loop exit
            {
                recv(pClient->m_socket, receiveBuf, length + SIZE - x + 1, 0);
                fwrite(receiveBuf, 1, length + SIZE - x, fp);
                fclose(fp);                     
                WriteToLog("file received done");
                break;
            }

        }
    }
    WriteToLog("out of reVal>0");

    LeaveCriticalSection(&pClient->m_cs);
    WriteToLog("out of LeaveCriticalSection");
    memset(temp, 0, MAX_NUM_BUF);   //clean up temp variables
}

WriteToLog("out of pClient->m_bConning");
pClient->m_bConning = FALSE;            //disconnect with client
return 0;                               //thread exit

}


  • 写回答

7条回答 默认 最新

  • 离水的鱼儿 2018-01-08 12:39
    关注

    那你打断点看是哪里运行出错了?肯定有地方异常了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序