Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to send a text file at a socket after every 10ms. The code works fine and keeps on sending the text file over the socket after an interval of 10ms. But after some period of time (like after 3-4 minutes), fopen() fils (though fopen() works fine for some duration) and I get an error "Unhandled exception at 0x011f28f7 in Client2.exe: 0xC00000FD: Stack overflow." and a break at

test dword ptr [eax],eax ; probe page.
in "chkstk.asm".

what could be the possible reason for this? How fopen() works fine for sometime and fails afterwards?

Please help me :(

CODE:

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   char timer[1000];
    switch(msg)
      {      case WM_TIMER: 
            switch(wParam) 
               { 
                  case IDT_TIMER1: 
                   {        
                    FILE *fpSend ;
                    if((fpSend = fopen("Client4.txt", "r+b")) == NULL)
                    {
                  MessageBox( NULL,
                     "Unable to open the File",
                     "Error!",
                     MB_ICONEXCLAMATION | 
                     MB_OK);
              exit(EXIT_FAILURE);
                    }
   char file_buffer[100000];
   fseek(fpSend, 0, SEEK_END);
   size_t file_size = ftell(fpSend);
   fseek(fpSend, 0, SEEK_SET);
   if(file_size>0)   //if file size>0
      {
         int bytes_read=0;
         if((bytes_read=fread(file_buffer, file_size, 1, fpSend))<=0)
            {
            //"Unable to copy file into buffer",
            }
            //"File copied in Buffer",

         if(sendto(socketIdentifier, file_buffer, file_size, 0, (struct sockaddr *) &AH_glb_connectedSocket, sizeof(AH_glb_connectedSocket))<0)
            {
                //"Not Sent"
            }
         else
            {
                //"File Sent Successfully!",
                sendCount = sendCount+1;
                memset(file_buffer, 0, sizeof(file_buffer));
            }

               }
          break;
              default:
                 return 0;
               }
     break;
    }  
}

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
   //window created.

   while(GetMessage(&Msg, NULL, 0, 0) > 0)
      {         
         TranslateMessage(&Msg);
         DispatchMessage(&Msg);
      }
   return Msg.wParam;


   closesocket(socketIdentifier);
   WSACleanup();

   return 0;
}
Posted
Comments
OriginalGriff 9-May-13 3:50am    
Before we even start to look at that, you need to go through it and fix the indentation.
You have nested switches, with code all over the place, and it is not obvious from a quick look what part is related to what.
Indent it properly, make it readable - then we will start to look at your problem. Help us to help you!
Use the "Improve question" widget to edit your question and provide better information.
ayesha hassan 9-May-13 4:39am    
I am sorry for improper indentation. I am going to fix that :)

1 solution

You didn't bother to close the file, did you?
Call
C
fclose(fpSend);
when you've done with it (before returning from the WndProc).
 
Share this answer
 
Comments
ayesha hassan 9-May-13 4:41am    
@CPallini : Once again you have solved it :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900