我的以下代码已成功编译,并且可以完美运行,有时可以处理 40 个客户端,但一段时间后,它会给出错误“Debug Assertiion Failed expression: (_osfile(fh) & fopen)”。
我在互联网上搜索并找到了解决方案:
项目 + 属性、配置属性、C/C++、代码生成,将运行时库更改为“多线程 DLL (/MD)”(或使用 /MT)。
现在没有显示 Debug Assertion Failed 错误,但现在的错误是“Fatal error at line fwrite()”。我在代码中突出显示了这一行以供参考。
谁能解释一下为什么会这样?我犯了什么错误?我被卡住了:(
抱歉,代码太长了。
代码:
DWORD WINAPI ThreadProc(LPVOID param)
{
int threadNumber= (int)param;
int PORT = 8888+threadNumber; //so that each thread bind()s its socket to a different Port number.
WSADATA wsa;
//Initialise winsock//
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
//"WinSock Initialization FAILED",
}
//Create a socket//
SOCKET newSocketIdentifier;
SOCKADDR_IN newSocket;
if((newSocketIdentifier = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
{
//Socket Creation Failed
}
//Socket Created//
//Prepare the sockaddr_in structure//
newSocket.sin_family = AF_INET;
newSocket.sin_addr.s_addr = INADDR_ANY;
newSocket.sin_port = htons(PORT);
//Bind//
if( bind(newSocketIdentifier ,(struct sockaddr *)&newSocket, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
{
//Bind Failed
}
//Bind Done// char threadNumberBuffer[32] = "Thread Number : ";
char threadNumberBuffer[32] = "Thread Number : ";
char buff[12];
itoa(threadNumber,buff,10);
strcat(threadNumberBuffer,buff);
/*MessageBox( NULL,
threadNumberBuffer,
"Thread Created :)",
MB_ICONINFORMATION);*/
char data[256];
int bytes, waitRet;
WSAEVENT hEvent = WSACreateEvent();
WSANETWORKEVENTS events;
WSAEventSelect(newSocketIdentifier, hEvent, FD_READ | FD_WRITE);
SOCKADDR_IN clientSocket;
int clientSocketLength = sizeof(SOCKADDR_IN);
char receiveBuffer[8192]={0};
int recv_len=0;
char file[128]="Client";
char threadno[32];
itoa(threadNumber,threadno,10);
strcat(threadno,".txt");
strcat(file,threadno);
//FILE *fprecv;
FILE *fprecv = fopen(TEXT(file),"w+b");
rewind(fprecv);
int receiveCount=0;
while(1)
{ //while(1) starts
waitRet = WSAWaitForMultipleEvents(1, &hEvent, FALSE, WSA_INFINITE, FALSE);
//WSAResetEvent(hEvent);
if(WSAEnumNetworkEvents(newSocketIdentifier,hEvent,&events) == SOCKET_ERROR)
{
MessageBox( NULL,
threadNumberBuffer,
"FAILURE",
MB_ICONINFORMATION);
}
else
{ //else event occurred starts
if(events.lNetworkEvents & FD_READ)
{ //check for network event starts
/*MessageBox( NULL,
buf,
"FD_READ",
MB_ICONINFORMATION);*/
if((recv_len = recvfrom(newSocketIdentifier, receiveBuffer, sizeof(receiveBuffer), 0, (struct sockaddr *) &clientSocket, &clientSocketLength)) == SOCKET_ERROR)
{
MessageBox( NULL,
"ERROR",
"Could not Receive Data",
MB_ICONINFORMATION);
exit(EXIT_FAILURE);
return FALSE;
}
receiveCount = receiveCount+1;
char display[2000] = "Number of Receives = ";
if(memcmp(receiveBuffer,"EXIT",4) == 0)
{
char receiveCountBuffer[1000];
itoa(receiveCount,receiveCountBuffer,10);
strcat(display,receiveCountBuffer);
MessageBox( NULL,
display,
threadNumberBuffer,
MB_ICONINFORMATION);
break;
//return FALSE;
}
/*MessageBox( NULL,
receiveBuffer,
"File Read",
MB_ICONINFORMATION);*/
else
{
//fprecv= freopen(TEXT(file),"w+b",stdout);
**if(fwrite(receiveBuffer, 1, recv_len, fprecv)<0)** //Fatal Error here
{
MessageBox( NULL,
"problem while writing file",
"Error!",
MB_ICONINFORMATION);
exit(1);
}
//fclose(fprecv);
}
/* MessageBox( NULL,
"Written",
"Success",
MB_ICONINFORMATION);*/
rewind(fprecv);
} //check for network event ends
} //else event occurred ends
} //while(1) ends
WSACloseEvent(hEvent);
return 0;
}