Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Windows Win32 Visual-Studio file , +
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 Frown | :(
 
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 8-May-13 22:42pm
Comments
OriginalGriff at 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 at 9-May-13 4:39am
   
I am sorry for improper indentation. I am going to fix that :)

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You didn't bother to close the file, did you?
Call
fclose(fpSend);
when you've done with it (before returning from the WndProc).
  Permalink  
Comments
ayesha hassan at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 9 May 2013
Copyright © CodeProject, 1999-2014
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