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 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页
  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信