<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Sending file via send and recv through WinSocks]]></title><description><![CDATA[<p>Hi!<br />
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?<br />
(I'm working on windows.)</p>
<p>Server (GUI ... requests the transfer and receives the file.):</p>
<pre><code>else if (LOWORD(wParam) == 7 &amp;&amp;
                HIWORD(wParam) == BN_CLICKED &amp;&amp;
                (HWND) lParam == downloadButton)
            { // when the download button is clicked it initiates the download...

                char location[501];
                char buffer[102400];
                char temp[101] = {&quot;\0&quot;};

                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,&quot;downloadfile&quot;);
                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,&quot;C:\\theDownloads\\&quot;);
                strcat(myFile,temp);
                myfile.open (myFile, std::ios::out | std::ios::binary);
                if (myfile.is_open())
                {   
                    myfile.seekp (0, std::ios::beg);

                    while(count&lt;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) &quot;File download complete\0&quot;);
                strcpy(buf,&quot;EOR&quot;);
            }
</code></pre>
<p>Client (commandline porgram) ( waits for a request and sends the file to the server):</p>
<pre><code>else if(!strcmp(buf,&quot;downloadfile&quot;))
        { //starts sending the file after it got the request &quot;downloadfile&quot;
            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,&quot;\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&quot;);
                while(i&lt;size)
                {
                    i = i*102400;
                    myfile.read (buffer, sizeof(buffer));
                    send(kSock, buffer, sizeof(buffer), 0);
                    std::cout&lt;&lt;&quot;Packet &quot;&lt;&lt;i&lt;&lt;&quot; sent: &quot;&lt;&lt;sizeof(buffer)&lt;&lt;&quot;\n&quot;;
                }
                delete[] memblock;
                myfile.close();
            }

            strcpy(buf,&quot;EOR\0&quot;);
            send(kSock, buf, sizeof(buf), 0);
        }
</code></pre>
<p>Best regards!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/217120/sending-file-via-send-and-recv-through-winsocks</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 09:33:58 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/217120.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 03 Jul 2008 10:06:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Sending file via send and recv through WinSocks on Thu, 03 Jul 2008 10:06:10 GMT]]></title><description><![CDATA[<p>Hi!<br />
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?<br />
(I'm working on windows.)</p>
<p>Server (GUI ... requests the transfer and receives the file.):</p>
<pre><code>else if (LOWORD(wParam) == 7 &amp;&amp;
                HIWORD(wParam) == BN_CLICKED &amp;&amp;
                (HWND) lParam == downloadButton)
            { // when the download button is clicked it initiates the download...

                char location[501];
                char buffer[102400];
                char temp[101] = {&quot;\0&quot;};

                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,&quot;downloadfile&quot;);
                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,&quot;C:\\theDownloads\\&quot;);
                strcat(myFile,temp);
                myfile.open (myFile, std::ios::out | std::ios::binary);
                if (myfile.is_open())
                {   
                    myfile.seekp (0, std::ios::beg);

                    while(count&lt;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) &quot;File download complete\0&quot;);
                strcpy(buf,&quot;EOR&quot;);
            }
</code></pre>
<p>Client (commandline porgram) ( waits for a request and sends the file to the server):</p>
<pre><code>else if(!strcmp(buf,&quot;downloadfile&quot;))
        { //starts sending the file after it got the request &quot;downloadfile&quot;
            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,&quot;\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&quot;);
                while(i&lt;size)
                {
                    i = i*102400;
                    myfile.read (buffer, sizeof(buffer));
                    send(kSock, buffer, sizeof(buffer), 0);
                    std::cout&lt;&lt;&quot;Packet &quot;&lt;&lt;i&lt;&lt;&quot; sent: &quot;&lt;&lt;sizeof(buffer)&lt;&lt;&quot;\n&quot;;
                }
                delete[] memblock;
                myfile.close();
            }

            strcpy(buf,&quot;EOR\0&quot;);
            send(kSock, buf, sizeof(buf), 0);
        }
</code></pre>
<p>Best regards!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1540297</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1540297</guid><dc:creator><![CDATA[beginnerinc]]></dc:creator><pubDate>Thu, 03 Jul 2008 10:06:10 GMT</pubDate></item><item><title><![CDATA[Reply to Sending file via send and recv through WinSocks on Fri, 04 Jul 2008 20:06:24 GMT]]></title><description><![CDATA[<p>Your question is not precise and nobody in this forum likes to read long source codes. Tell us, what you want! Maybe a simple file-sharing between server and client can help.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1541255</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1541255</guid><dc:creator><![CDATA[berniebutt]]></dc:creator><pubDate>Fri, 04 Jul 2008 20:06:24 GMT</pubDate></item><item><title><![CDATA[Reply to Sending file via send and recv through WinSocks on Sat, 05 Jul 2008 18:08:35 GMT]]></title><description><![CDATA[<p>Thank you for your response. This IS ONLY the code for the communication between server and client!! Can't make it shorter but still usefull. ...I already found some help at another Forum. If somebody needs to read it: <a href="http://www.codeguru.com/forum/showthread.php?p=1736715" rel="nofollow">http://www.codeguru.com/forum/showthread.php?p=1736715</a></p>
<p>best Regards!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1541528</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1541528</guid><dc:creator><![CDATA[beginnerinc]]></dc:creator><pubDate>Sat, 05 Jul 2008 18:08:35 GMT</pubDate></item><item><title><![CDATA[Reply to Sending file via send and recv through WinSocks on Mon, 07 Jul 2008 17:33:41 GMT]]></title><description><![CDATA[<p>o-k. - Good luck, you have found it! - Some fellows in this forum are experienced in English language too. Come here again, if you need an answer to your specific new problems. We can help worldwide - only Chinese may cause some trouble.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1542917</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1542917</guid><dc:creator><![CDATA[berniebutt]]></dc:creator><pubDate>Mon, 07 Jul 2008 17:33:41 GMT</pubDate></item><item><title><![CDATA[Reply to Sending file via send and recv through WinSocks on Mon, 07 Jul 2008 22:46:44 GMT]]></title><description><![CDATA[<p>Your code does not check the return-value of send() and recv() !!<br />
You need to know the meaning of the return-values: Check the documentation of recv() and send() in the msdn...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1543045</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1543045</guid><dc:creator><![CDATA[geeky]]></dc:creator><pubDate>Mon, 07 Jul 2008 22:46:44 GMT</pubDate></item></channel></rss>