Thread: Sending image

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Sending image

    When i send a text file everything is fine.
    When i try to send a .gif it gives me the correct size but goes into a loop and sending the same 11 bytes.

    I open the files in binary mode so it cant be the problem.
    I got a hunch that it should meet an EOF character or such but i dont check for that so i dont know what could be the problem.

    Client sending:
    Code:
    void Send_File()
    {
         //***************************************
         //Read file and send size
         //***************************************
         ifstream file("1.gif", ios::binary);
         char cLength[256] = {0};
         unsigned int nBytesSent = 0, fileSize = 0, r = 0;
         // get length of file:
         file.seekg (0, ios::end); fileSize = file.tellg(); file.seekg (0, ios::beg);
         // allocate memory:
         char * buffer = new char [fileSize];
         // read data as a block:
         file.read (buffer,fileSize);
         file.close();  itoa(fileSize, cLength,10);
         send(inetsock, cLength, strlen(cLength), 0);
         //******************************************
         //Send data
         //******************************************
         char *buf = new char[256];
         cout <<  "fileSize "  <<  fileSize  << endl;  system("pause");
         while(nBytesSent<fileSize)
         {
               string str(buffer); //char* to string
    
               strcpy(buf, str.substr(nBytesSent, 255).c_str());
               r = send(inetsock, buf, strlen(buf), 0);
               if (r==SOCKET_ERROR)
               {
                   cout << "Connection unexpectedly closed \n"; closesocket(inetsock);
               }
               nBytesSent+=r;
               cout << "nBytesSent " << nBytesSent  << endl;
    
         }
         delete[] buffer; delete[] buf;
    }
    Server recieving:
    Code:
    void Recieve_File()
    {
       //Recieve size
          char recvBuff[256]={0}; string fileBuff="";
       unsigned int fileSize = 0,    nBytesRecieved = 0,  r = 0;
          recv(AcceptSock, recvBuff, sizeof(recvBuff), 0);
       fileSize=atof(recvBuff);
       //cout << fileSize << endl; system("pause");
       //******************************************
       //Recieve data
       //******************************************
          char *buf = new char[256];
          cout << "fileSize "       << fileSize  << endl;
       while(nBytesRecieved<fileSize)
       {
             memset(buf, 0, 256);
             r=recv(AcceptSock, buf, sizeof(buf), 0);
             fileBuff.append(buf);
             nBytesRecieved+=r+1;
             cout << "nBytesRecieved " << nBytesRecieved  << endl;
    
       }
       //cout << fileBuff << endl; system("pause");
       ofstream TheCopy("TheCopy.txt",ios::binary | ios::app);
       TheCopy.write (fileBuff.c_str(),fileSize);
       TheCopy.close();
       delete[] buf;
    }
    Last edited by Ducky; 08-09-2009 at 05:57 AM.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. Replies: 1
    Last Post: 05-27-2009, 12:46 PM
  3. Simple Image Processing
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2009, 12:10 PM
  4. Image rotation - doesn't always work
    By ulillillia in forum C Programming
    Replies: 12
    Last Post: 05-03-2007, 12:46 PM
  5. Replies: 4
    Last Post: 03-02-2003, 09:12 AM