Results 1 to 3 of 3

Thread: Problems when sending binary files

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241

    Problems when sending binary files

    I know how to use sockets, but I've been having so many problems with sending files that before I continue I would like to see some examples. So any examples would be nice, preferably ones that do not use MFC and are relatively simple. I have also posted my code below in case you can help me fix it.
    Last edited by WiKiDJeFF; Dec 7th, 2002 at 05:43 PM.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    here is my class for sending the file, it uses threads, which may be part of the problem.

    the file C:\testing.exe comes out perfect and works. However, the file that the receiving end outputs comes out to be the right size, but when I open both it, and the working testing.exe in notepad the binary data looks different, but parts that are string of text look the same.

    I have tried using unsigned char *, but the functions send() and read() wont take it.

    Code:
    class Upload
    {
    private:
    
    	
    
    protected:
      HANDLE  d_threadHandle;
      DWORD   d_threadID;
      bool    d_bIsRunning;
    
      void Begin();
      void End();
      bool IsRunning();
    
    public:
      cThread();
      //virtual ~Upload();
    
      SendFile();
    
      virtual DWORD ThreadProc();
    
      Upload();
    
      string FileName;
      MySocket sSend;
    
      
    };
    
    Upload::Upload()
    {
    	return;
    }
    
    Upload::SendFile()
    {	
    	Begin();
    }
    
    
    void Upload::Begin()
    {
    //#if defined( _WIN32 ) && defined( _MT )
      if( d_threadHandle )
        End();  // just to be safe.
    	
      // Start the thread.
    
      d_threadHandle = CreateThread( NULL,
                                     0,
                                     (LPTHREAD_START_ROUTINE)cThreadProc,
                                     this,
                                     0,
                                     (LPDWORD)&d_threadID );
    
      if( d_threadHandle == NULL )
      {
        // Arrooga! Dive, dive!  And deal with the error, too!
      }
      d_bIsRunning = true;
    //#endif
    }
    
    
    static DWORD WINAPI cThreadProc( Upload *pThis )
    {
      return pThis->ThreadProc();
    }
    
    
    DWORD Upload::ThreadProc()
    {
    
    	ifstream openfile(FileName.c_str(), ios::in | ios::binary);
    	fstream openfile2("C:\\testing.exe", ios::out | ios::binary);
    
    	//MessageBox(NULL, "wait", "wait", MB_OK);
    
    	char *tmpContent = new char[1024];
    	memset(tmpContent, 0, 1024);
    	int size = GetFileLen((char *)FileName.c_str()) + 1;
    	int totalread = 0;
    
    	int left = size - totalread;
    
    	char cleft[10000] = {0};
    
    	
    
    	if(openfile.is_open())
    	{
    
    		while(left > 1)
    		{
    
    			totalread = totalread + openfile.gcount();
    
    			left = size - totalread;
    
    			sprintf(cleft, "%d %d %d", left, totalread, openfile.gcount());
    
    			if((left < 1024) && (left > 0))
    			{
    				openfile.read(tmpContent, left);
    
    				//sSend.SendBData((char *)tmpContent, 1024);
    
    				send(sSend.sock, tmpContent, openfile.gcount(), 0);
    
    				openfile2.write(tmpContent, openfile.gcount());
    
    				//Sleep(100);
    
    				//MessageBox(NULL, cleft, "hi2", MB_OK);
    
    				
    
    			}
    			else
    			{
    				openfile.read(tmpContent, 1024);
    
    				//sSend.SendBData((char *)tmpContent, 1024);
    
    				send(sSend.sock, tmpContent, 1024, 0);
    
    				openfile2.write(tmpContent, 1024);
    
    				//Sleep(100);
    
    				//MessageBox(NULL, cleft, "hi1", MB_OK);
    
    				
    			}
    
    			
    
    			
    
    		}
    
    		//Sleep(100);
    
    		//MessageBox(NULL, "done2", "done2", MB_OK);
    
    		sSend.Close();
    		openfile.close();
    		openfile2.close();
    
    	}
    
    	End();
    
    	return 0;
    }
    
    
    void Upload::End()
    {
    #if defined( _WIN32 ) && defined( _MT )
      if( d_threadHandle != NULL )
      {
        d_bIsRunning = false;
        WaitForSingleObject( d_threadHandle, INFINITE );
        CloseHandle( d_threadHandle );
        d_threadHandle = NULL;
      }
    #endif
    }
    Last edited by WiKiDJeFF; Dec 10th, 2002 at 02:29 PM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    the program now works when it the connection is local "127.0.0.1", but when its not local it does not receive the whole program, and only receive part of it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width