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
}