Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » C / C++ (WinAPI, Konsole) » Sending file via send and recv through WinSocks

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
000
03.07.2008, 10:29 Uhr
~ImNewToC
Gast


Hi!
I have a problem and I cant solve it. I'm new to c++. I have a server application and a client and I want to send a file from the Client to the Server (after a request from the server). I already have some stuff but it doesn't work correctly (copies to much und at the end the file is not the file anymore...). Could anyone fix it for me ...and explain it to me?
(I'm working on windows.)


Server (requests the transfer and receives the file.):


Code:
else if (LOWORD(wParam) == 7 &&
                HIWORD(wParam) == BN_CLICKED &&
                (HWND) lParam == downloadButton)
            { // when the download button is clicked it initiates the download...

                
                char location[501];
                char buffer[102400];
                char temp[101] = {"\0"};
                
                int selected = ListView_GetNextItem(fileBrowse,-1,LVNI_SELECTED);
                ListView_GetItemText(
                    fileBrowse,
                    selected,
                    0,
                    temp,
                    100
                ); //file name and location is taken from another field (i hope you don't need it as well...
                
                int length =  GetWindowTextLength (listFilesLocation) + 1;
                GetWindowText (listFilesLocation, location, length);
                strcat(location,temp);
                
                strcpy(buf,"downloadfile");
                send(client, buf, sizeof(buf), 0);
                strcpy(buf,location);
                send(client, buf, sizeof(buf), 0);
                
  
                
                char num[10];
                int fileSize = recv(client, num, sizeof(num), 0);
               // int fileSize = atoi(num);
                int count = 0;
  
                std::ofstream myfile;
                
                char myFile[100];
                strcpy(myFile,"C:\\theDownloads\\");
                strcat(myFile,temp);
                myfile.open (myFile, std::ios::out | std::ios::binary);
                if (myfile.is_open())
                {  
                    myfile.seekp (0, std::ios::beg);
                    
                    while(count<fileSize)
                    {                  
                        count++;
                        recv(client, buffer, sizeof(buffer), 0);
                        myfile.write (buffer, sizeof(buffer));
                        Sleep(1);
                    }
                    myfile.close();
                }
                SendMessage(terminalWindow, LB_ADDSTRING, 0, (LPARAM) num);
                SendMessage(terminalWindow, LB_ADDSTRING, 0, (LPARAM) "File download complete\0");
                strcpy(buf,"EOR");
            }





Client ( waits for a request and sends the file to the server):


Code:
else if(!strcmp(buf,"downloadfile"))
        { //starts sending the file after it got the request "downloadfile"
            char filePath[256];
            char buffer[102400];
            recv(kSock, filePath, sizeof(filePath), 0);
            std::ifstream myfile;
            myfile.open (filePath, std::ios::in | std::ios::binary);
            if (myfile.is_open())
            {
                std::ifstream::pos_type size;
                char * memblock;
                myfile.seekg (0, std::ios::beg);
                int begin = myfile.tellg();
                myfile.seekg (0, std::ios::end);
                int end = myfile.tellg();
                size = (end - begin);
                myfile.seekg (0, std::ios::beg);
                int i=0;
                char num[10];
                itoa(size,num,10);
                send(kSock, num, sizeof(num), 0);
                strcpy(buf,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
                while(i<size)
                {
                    i = i*102400;
                    myfile.read (buffer, sizeof(buffer));
                    send(kSock, buffer, sizeof(buffer), 0);
                    std::cout<<"Packet "<<i<<" sent: "<<sizeof(buffer)<<"\n";
                }
                delete[] memblock;
                myfile.close();
            }
            
            strcpy(buf,"EOR\0");
            send(kSock, buf, sizeof(buf), 0);
        }



Best regards!
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
05.07.2008, 20:10 Uhr
~ImNewToC
Gast


Thx for your help... If somebody has the same problem, here you can read som answers to my post

www.codeguru.com/forum/showthread.php?p=1736715

Best Regards!
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ C / C++ (WinAPI, Konsole) ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: