Click here to Skip to main content
13,144,309 members (32,026 online)
Rate this:
 
Please Sign up or sign in to vote.
My following code is compiled successfully and runs perfectly for sometime handling 40 clients but after sometime, it gives an error "Debug Assertiion Failed expression: (_osfile(fh) & fopen)".

I searched on internet and found a solution:

Project + Properties, Configuration properties, C/C++, Code generation, change Runtime library to "Multi-threaded DLL (/MD)" (or use /MT).<br />

Now the Debug Assertion Failed error is not shown but the error now is "Fatal error at line fwrite()". I have Highlighted this line in code for reference.
Can anyone explain me please why does this happen? What mistake have I made? I am stuck :(

Sorry for the long code.

CODE:

   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;
}
Posted 8-May-13 20:46pm
Updated 8-May-13 21:17pm
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 9 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100