<?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[Sockets und das http Protokoll Verbindung fehlgeschlagen]]></title><description><![CDATA[<p>Hi ich habe mir in den Artikeln aus dem Forum mal den Artikel Sockets und das http Protokoll reingetan. Mein Problem mit dem Programm was dadrin erklärt wird ist, dass es bei mir immer mit &quot;Verbindung Fehlgeschlagen!&quot; beendet. Ich vermute das es dadran liegt, dass in dem Netzwerk wo ich bin Browser nur über einen Proxy auf I-Net seiten kommen. Kann mir jemand erklären wie ich dieses Programm zum laufen bringe bzw wie ich es umschreibe das es ne Verbindung aufbaut.<br />
Vielen Dank<br />
Hier nochmal der Code:</p>
<pre><code>// Client.cpp : Defines the entry point for the console application.
//

#include &quot;stdafx.h&quot;
#include &lt;iostream&gt;
#include&lt;String&gt;
#include &lt;time.h&gt;
#include &lt;fstream&gt;
#include &lt;stdexcept&gt; // runtime_error
#include &lt;sstream&gt;
#ifdef linux
#include &lt;sys/socket.h&gt; // socket(), connect()
#include &lt;arpa/inet.h&gt; // sockaddr_in
#include &lt;netdb.h&gt; // gethostbyname(), hostent
#include &lt;errno.h&gt; // errno
#else
#include &lt;winsock2.h&gt;
#endif

std::runtime_error CreateSocketError()
{
    std::ostringstream temp;
#ifdef linux
    temp &lt;&lt; &quot;Socket-Fehler #&quot; &lt;&lt; errno &lt;&lt; &quot;: &quot; &lt;&lt; strerror(errno);
#else
    int error = WSAGetLastError();
    temp &lt;&lt; &quot;Socket-Fehler #&quot; &lt;&lt; error;
    char* msg;
    if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                     NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                     reinterpret_cast&lt;char*&gt;(&amp;msg), 0, NULL))
    {
        try
        {
            temp &lt;&lt; &quot;: &quot; &lt;&lt; msg;
            LocalFree(msg);
        }
        catch(...)
        {
            LocalFree(msg);
            throw;
        }
    }
#endif
    return std::runtime_error(temp.str());
}

void SendAll(int socket, const char* const buf, const int size)
{
    int bytesSent = 0; // Anzahl Bytes die wir bereits vom Buffer gesendet haben
    do
    {
        int result = send(socket, buf + bytesSent, size - bytesSent, 0);
        if(result &lt; 0) // Wenn send einen Wert &lt; 0 zurück gibt deutet dies auf einen Fehler hin.
        {
            throw CreateSocketError();
        }
        bytesSent += result;
    } while(bytesSent &lt; size);
}

// Liest eine Zeile des Sockets in einen stringstream
void GetLine(int socket, std::stringstream&amp; line)
{
    for(char c; recv(socket, &amp;c, 1, 0) &gt; 0; line &lt;&lt; c)
    {
        if(c == '\n')
        {
            return;
        }
    }
    throw CreateSocketError();
}

// Entfernt das http:// vor dem URL
void RemoveHttp(std::string&amp; URL)
{
    size_t pos = URL.find(&quot;http://&quot;);
    if(pos != std::string::npos)
    {
        URL.erase(0, 7);
    }
}

// Gibt die Dateiendung im URL zurück
std::string GetFileEnding(std::string&amp; URL)
{
    using namespace std;
    size_t pos = URL.rfind(&quot;.&quot;);
    if(pos == string::npos)
    {
        return &quot;&quot;;
    }
    URL.erase(0, pos);
    string ending = &quot;.&quot;;
    // Algorithmus um Sachen wie ?index=home nicht zuzulassen
    for(string::iterator it = URL.begin() + 1; it != URL.end(); ++it)
    {
        if(isalpha(*it))
        {
            ending += *it;
        }
        else
        {
            break;
        }
    }
    return ending;
}

// Gibt den Hostnamen zurück und entfernt ihn aus der URL, sodass nur noch der Pfad übrigbleibt
std::string RemoveHostname(std::string&amp; URL)
{
    size_t pos = URL.find(&quot;/&quot;);
    if(pos == std::string::npos)
    {
        std::string temp = URL;
        URL = &quot;/&quot;;
        return temp;
    }
    std::string temp = URL.substr(0, pos);
    URL.erase(0, pos);
    return temp;
}

int _tmain(int argc, _TCHAR* argv[])
{
    using namespace std;

    cout &lt;&lt; &quot;URL: &quot;;
    string URL;
    cin &gt;&gt; URL; // User gibt URL der Datei ein, die herruntergeladen werden soll

#ifndef linux
    WSADATA w;
    if(int result = WSAStartup(MAKEWORD(2,2), &amp;w) != 0)
    {
        cout &lt;&lt; &quot;Winsock 2 konnte nicht gestartet werden! Error #&quot; &lt;&lt; result &lt;&lt; endl;
		Sleep(3000);
        return 1;
    }
#endif

    RemoveHttp(URL);

    string hostname = RemoveHostname(URL);

    hostent* phe = gethostbyname(hostname.c_str());

    if(phe == NULL)
    {
        cout &lt;&lt; &quot;Host konnte nicht aufgeloest werden!&quot; &lt;&lt; endl;
		Sleep(3000);
        return 1;
    }

    if(phe-&gt;h_addrtype != AF_INET)
    {
        cout &lt;&lt; &quot;Ungueltiger Adresstyp!&quot; &lt;&lt; endl;
		Sleep(3000);
        return 1;
    }

    if(phe-&gt;h_length != 4)
    {
        cout &lt;&lt; &quot;Ungueltiger IP-Typ!&quot; &lt;&lt; endl;
		Sleep(3000);
        return 1;
    }

    int Socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(Socket == -1)
    {
        cout &lt;&lt; &quot;Socket konnte nicht erstellt werden!&quot; &lt;&lt; endl;
		Sleep(3000);
        return 1;
    }

    sockaddr_in service;
    service.sin_family = AF_INET;
    service.sin_port = htons(80); // Das HTTP-Protokoll benutzt Port 80

    char** p = phe-&gt;h_addr_list; // p mit erstem Listenelement initialisieren
    int result; // Ergebnis von connect
    do
    {
        if(*p == NULL) // Ende der Liste
        {
            cout &lt;&lt; &quot;Verbindung fehlgschlagen!&quot; &lt;&lt; endl;
			Sleep(4000);
            return 1;
        }

        service.sin_addr.s_addr = *reinterpret_cast&lt;unsigned long*&gt;(*p);
        ++p;
        result = connect(Socket, reinterpret_cast&lt;sockaddr*&gt;(&amp;service), sizeof(service));
    }
    while(result == -1);

    cout &lt;&lt; &quot;Verbindung erfolgreich!&quot; &lt;&lt; endl;

    string request = &quot;GET &quot;;
    request += URL;    // z.B. /faq/index.html
    request += &quot; HTTP/1.1\n&quot;;
    request += &quot;Host: &quot; + hostname + &quot;\nConnection: close\n\n&quot;;

    try
    {
        SendAll(Socket, request.c_str(), request.size());

        int code = 100; // 100 = Continue
        string Protokoll;
        stringstream firstLine; // Die erste Linie ist anders aufgebaut als der Rest
        while(code == 100)
        {
            GetLine(Socket, firstLine);
            firstLine &gt;&gt; Protokoll;
            firstLine &gt;&gt; code;
            if(code == 100)
            {
                GetLine(Socket, firstLine); // Leere Zeile nach Continue ignorieren
            }
        }
        cout &lt;&lt; &quot;Protokoll: &quot; &lt;&lt; Protokoll &lt;&lt; endl;

        if(code != 200)
        {
            firstLine.ignore(); // Leerzeichen nach dem Statuscode ignorieren
            string msg;
            getline(firstLine, msg);
            cout &lt;&lt; &quot;Error #&quot; &lt;&lt; code &lt;&lt; &quot; - &quot; &lt;&lt; msg &lt;&lt; endl;
			Sleep(3000);
            return 1;
        }

        bool chunked = false;
        const int noSizeGiven = -1;
        int size = noSizeGiven;

        while(true)
        {
            stringstream sstream;
            GetLine(Socket, sstream);
            if(sstream.str() == &quot;\r&quot;) // Header zu Ende?
            {
                break;
            }
            string left; // Das was links steht
            sstream &gt;&gt; left;
            sstream.ignore(); // ignoriert Leerzeichen
            if(left == &quot;Content-Length:&quot;)
            {
                sstream &gt;&gt; size;
            }
            if(left == &quot;Transfer-Encoding:&quot;)
            {
                string transferEncoding;
                sstream &gt;&gt; transferEncoding;
                if(transferEncoding == &quot;chunked&quot;)
                {
                    chunked = true;
                }
            }
        }

        string filename = &quot;download&quot; + GetFileEnding(URL);
        cout &lt;&lt; &quot;Filename: &quot; &lt;&lt; filename &lt;&lt; endl;
        fstream fout(filename.c_str(), ios::binary | ios::out);
        if(!fout)
        {
            cout &lt;&lt; &quot;Could Not Create File!&quot; &lt;&lt; endl;
			Sleep(3000);
            return 1;
        }
        int recvSize = 0; // Empfangene Bytes insgesamt
        char buf[1024];
        int bytesRecv = -1; // Empfangene Bytes des letzten recv

        if(size != noSizeGiven) // Wenn die Größe über Content-length gegeben wurde
        {
            cout &lt;&lt; &quot;0%&quot;;
            while(recvSize &lt; size)
            {
                if((bytesRecv = recv(Socket, buf, sizeof(buf), 0)) &lt;= 0)
                {
                    throw CreateSocketError();
                }
                recvSize += bytesRecv;
                fout.write(buf, bytesRecv);
                cout &lt;&lt; &quot;\r&quot; &lt;&lt; recvSize * 100 / size &lt;&lt; &quot;%&quot; &lt;&lt; flush; // Mit \r springen wir an den Anfang der Zeile
            }
        }
        else
        {
            if(!chunked)
            {
                cout &lt;&lt; &quot;Downloading... (Unknown Filesize)&quot; &lt;&lt; endl;
                while(bytesRecv != 0) // Wenn recv 0 zurück gibt, wurde die Verbindung beendet
                {
                    if((bytesRecv = recv(Socket, buf, sizeof(buf), 0)) &lt; 0)
                    {
                        throw CreateSocketError();
                    }
                    fout.write(buf, bytesRecv);
                }
            }
            else
            {
                cout &lt;&lt; &quot;Downloading... (Chunked)&quot; &lt;&lt; endl;
                while(true)
                {
                    stringstream sstream;
                    GetLine(Socket, sstream);
                    int chunkSize = -1;
                    sstream &gt;&gt; hex &gt;&gt; chunkSize; // Größe des nächsten Parts einlesen
                    if(chunkSize &lt;= 0)
                    {
                        break;
                    }
                    cout &lt;&lt; &quot;Downloading Part (&quot; &lt;&lt; chunkSize &lt;&lt; &quot; Bytes)... &quot; &lt;&lt; endl;
                    recvSize = 0; // Vor jeder Schleife wieder auf 0 setzen
                    while(recvSize &lt; chunkSize)
                    {
                        int bytesToRecv = chunkSize - recvSize;
                        if((bytesRecv = recv(Socket, buf, bytesToRecv &gt; sizeof(buf) ? sizeof(buf) : bytesToRecv, 0)) &lt;= 0)
                        {
                            throw CreateSocketError();
                        }
                        recvSize += bytesRecv;
                        fout.write(buf, bytesRecv);
                        cout &lt;&lt; &quot;\r&quot; &lt;&lt; recvSize * 100 / chunkSize &lt;&lt; &quot;%&quot; &lt;&lt; flush;
                    }
                    cout &lt;&lt; endl;
                    for(int i = 0; i &lt; 2; ++i)
                    {
                        char temp;
                        recv(Socket, &amp;temp, 1, 0);
                    }
                }
            }
        }
        cout &lt;&lt; endl &lt;&lt; &quot;Finished!&quot; &lt;&lt; endl;
    }
    catch(exception&amp; e)
    {
        cout &lt;&lt; endl;
        cerr &lt;&lt; e.what() &lt;&lt; endl;
    }
#ifdef linux
    close(Socket); // Verbindung beenden
#else
    closesocket(Socket); // Windows-Variante
#endif
	cout&lt;&lt;&quot;Success\n&quot;;
	Sleep(3000);
	return 0;
}
</code></pre>
<p>Sleep commandos habe ich zu testzwecken eingbaut.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/221186/sockets-und-das-http-protokoll-verbindung-fehlgeschlagen</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Apr 2026 05:58:13 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/221186.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 26 Aug 2008 07:30:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Sockets und das http Protokoll Verbindung fehlgeschlagen on Tue, 26 Aug 2008 07:30:47 GMT]]></title><description><![CDATA[<p>Hi ich habe mir in den Artikeln aus dem Forum mal den Artikel Sockets und das http Protokoll reingetan. Mein Problem mit dem Programm was dadrin erklärt wird ist, dass es bei mir immer mit &quot;Verbindung Fehlgeschlagen!&quot; beendet. Ich vermute das es dadran liegt, dass in dem Netzwerk wo ich bin Browser nur über einen Proxy auf I-Net seiten kommen. Kann mir jemand erklären wie ich dieses Programm zum laufen bringe bzw wie ich es umschreibe das es ne Verbindung aufbaut.<br />
Vielen Dank<br />
Hier nochmal der Code:</p>
<pre><code>// Client.cpp : Defines the entry point for the console application.
//

#include &quot;stdafx.h&quot;
#include &lt;iostream&gt;
#include&lt;String&gt;
#include &lt;time.h&gt;
#include &lt;fstream&gt;
#include &lt;stdexcept&gt; // runtime_error
#include &lt;sstream&gt;
#ifdef linux
#include &lt;sys/socket.h&gt; // socket(), connect()
#include &lt;arpa/inet.h&gt; // sockaddr_in
#include &lt;netdb.h&gt; // gethostbyname(), hostent
#include &lt;errno.h&gt; // errno
#else
#include &lt;winsock2.h&gt;
#endif

std::runtime_error CreateSocketError()
{
    std::ostringstream temp;
#ifdef linux
    temp &lt;&lt; &quot;Socket-Fehler #&quot; &lt;&lt; errno &lt;&lt; &quot;: &quot; &lt;&lt; strerror(errno);
#else
    int error = WSAGetLastError();
    temp &lt;&lt; &quot;Socket-Fehler #&quot; &lt;&lt; error;
    char* msg;
    if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                     NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                     reinterpret_cast&lt;char*&gt;(&amp;msg), 0, NULL))
    {
        try
        {
            temp &lt;&lt; &quot;: &quot; &lt;&lt; msg;
            LocalFree(msg);
        }
        catch(...)
        {
            LocalFree(msg);
            throw;
        }
    }
#endif
    return std::runtime_error(temp.str());
}

void SendAll(int socket, const char* const buf, const int size)
{
    int bytesSent = 0; // Anzahl Bytes die wir bereits vom Buffer gesendet haben
    do
    {
        int result = send(socket, buf + bytesSent, size - bytesSent, 0);
        if(result &lt; 0) // Wenn send einen Wert &lt; 0 zurück gibt deutet dies auf einen Fehler hin.
        {
            throw CreateSocketError();
        }
        bytesSent += result;
    } while(bytesSent &lt; size);
}

// Liest eine Zeile des Sockets in einen stringstream
void GetLine(int socket, std::stringstream&amp; line)
{
    for(char c; recv(socket, &amp;c, 1, 0) &gt; 0; line &lt;&lt; c)
    {
        if(c == '\n')
        {
            return;
        }
    }
    throw CreateSocketError();
}

// Entfernt das http:// vor dem URL
void RemoveHttp(std::string&amp; URL)
{
    size_t pos = URL.find(&quot;http://&quot;);
    if(pos != std::string::npos)
    {
        URL.erase(0, 7);
    }
}

// Gibt die Dateiendung im URL zurück
std::string GetFileEnding(std::string&amp; URL)
{
    using namespace std;
    size_t pos = URL.rfind(&quot;.&quot;);
    if(pos == string::npos)
    {
        return &quot;&quot;;
    }
    URL.erase(0, pos);
    string ending = &quot;.&quot;;
    // Algorithmus um Sachen wie ?index=home nicht zuzulassen
    for(string::iterator it = URL.begin() + 1; it != URL.end(); ++it)
    {
        if(isalpha(*it))
        {
            ending += *it;
        }
        else
        {
            break;
        }
    }
    return ending;
}

// Gibt den Hostnamen zurück und entfernt ihn aus der URL, sodass nur noch der Pfad übrigbleibt
std::string RemoveHostname(std::string&amp; URL)
{
    size_t pos = URL.find(&quot;/&quot;);
    if(pos == std::string::npos)
    {
        std::string temp = URL;
        URL = &quot;/&quot;;
        return temp;
    }
    std::string temp = URL.substr(0, pos);
    URL.erase(0, pos);
    return temp;
}

int _tmain(int argc, _TCHAR* argv[])
{
    using namespace std;

    cout &lt;&lt; &quot;URL: &quot;;
    string URL;
    cin &gt;&gt; URL; // User gibt URL der Datei ein, die herruntergeladen werden soll

#ifndef linux
    WSADATA w;
    if(int result = WSAStartup(MAKEWORD(2,2), &amp;w) != 0)
    {
        cout &lt;&lt; &quot;Winsock 2 konnte nicht gestartet werden! Error #&quot; &lt;&lt; result &lt;&lt; endl;
		Sleep(3000);
        return 1;
    }
#endif

    RemoveHttp(URL);

    string hostname = RemoveHostname(URL);

    hostent* phe = gethostbyname(hostname.c_str());

    if(phe == NULL)
    {
        cout &lt;&lt; &quot;Host konnte nicht aufgeloest werden!&quot; &lt;&lt; endl;
		Sleep(3000);
        return 1;
    }

    if(phe-&gt;h_addrtype != AF_INET)
    {
        cout &lt;&lt; &quot;Ungueltiger Adresstyp!&quot; &lt;&lt; endl;
		Sleep(3000);
        return 1;
    }

    if(phe-&gt;h_length != 4)
    {
        cout &lt;&lt; &quot;Ungueltiger IP-Typ!&quot; &lt;&lt; endl;
		Sleep(3000);
        return 1;
    }

    int Socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(Socket == -1)
    {
        cout &lt;&lt; &quot;Socket konnte nicht erstellt werden!&quot; &lt;&lt; endl;
		Sleep(3000);
        return 1;
    }

    sockaddr_in service;
    service.sin_family = AF_INET;
    service.sin_port = htons(80); // Das HTTP-Protokoll benutzt Port 80

    char** p = phe-&gt;h_addr_list; // p mit erstem Listenelement initialisieren
    int result; // Ergebnis von connect
    do
    {
        if(*p == NULL) // Ende der Liste
        {
            cout &lt;&lt; &quot;Verbindung fehlgschlagen!&quot; &lt;&lt; endl;
			Sleep(4000);
            return 1;
        }

        service.sin_addr.s_addr = *reinterpret_cast&lt;unsigned long*&gt;(*p);
        ++p;
        result = connect(Socket, reinterpret_cast&lt;sockaddr*&gt;(&amp;service), sizeof(service));
    }
    while(result == -1);

    cout &lt;&lt; &quot;Verbindung erfolgreich!&quot; &lt;&lt; endl;

    string request = &quot;GET &quot;;
    request += URL;    // z.B. /faq/index.html
    request += &quot; HTTP/1.1\n&quot;;
    request += &quot;Host: &quot; + hostname + &quot;\nConnection: close\n\n&quot;;

    try
    {
        SendAll(Socket, request.c_str(), request.size());

        int code = 100; // 100 = Continue
        string Protokoll;
        stringstream firstLine; // Die erste Linie ist anders aufgebaut als der Rest
        while(code == 100)
        {
            GetLine(Socket, firstLine);
            firstLine &gt;&gt; Protokoll;
            firstLine &gt;&gt; code;
            if(code == 100)
            {
                GetLine(Socket, firstLine); // Leere Zeile nach Continue ignorieren
            }
        }
        cout &lt;&lt; &quot;Protokoll: &quot; &lt;&lt; Protokoll &lt;&lt; endl;

        if(code != 200)
        {
            firstLine.ignore(); // Leerzeichen nach dem Statuscode ignorieren
            string msg;
            getline(firstLine, msg);
            cout &lt;&lt; &quot;Error #&quot; &lt;&lt; code &lt;&lt; &quot; - &quot; &lt;&lt; msg &lt;&lt; endl;
			Sleep(3000);
            return 1;
        }

        bool chunked = false;
        const int noSizeGiven = -1;
        int size = noSizeGiven;

        while(true)
        {
            stringstream sstream;
            GetLine(Socket, sstream);
            if(sstream.str() == &quot;\r&quot;) // Header zu Ende?
            {
                break;
            }
            string left; // Das was links steht
            sstream &gt;&gt; left;
            sstream.ignore(); // ignoriert Leerzeichen
            if(left == &quot;Content-Length:&quot;)
            {
                sstream &gt;&gt; size;
            }
            if(left == &quot;Transfer-Encoding:&quot;)
            {
                string transferEncoding;
                sstream &gt;&gt; transferEncoding;
                if(transferEncoding == &quot;chunked&quot;)
                {
                    chunked = true;
                }
            }
        }

        string filename = &quot;download&quot; + GetFileEnding(URL);
        cout &lt;&lt; &quot;Filename: &quot; &lt;&lt; filename &lt;&lt; endl;
        fstream fout(filename.c_str(), ios::binary | ios::out);
        if(!fout)
        {
            cout &lt;&lt; &quot;Could Not Create File!&quot; &lt;&lt; endl;
			Sleep(3000);
            return 1;
        }
        int recvSize = 0; // Empfangene Bytes insgesamt
        char buf[1024];
        int bytesRecv = -1; // Empfangene Bytes des letzten recv

        if(size != noSizeGiven) // Wenn die Größe über Content-length gegeben wurde
        {
            cout &lt;&lt; &quot;0%&quot;;
            while(recvSize &lt; size)
            {
                if((bytesRecv = recv(Socket, buf, sizeof(buf), 0)) &lt;= 0)
                {
                    throw CreateSocketError();
                }
                recvSize += bytesRecv;
                fout.write(buf, bytesRecv);
                cout &lt;&lt; &quot;\r&quot; &lt;&lt; recvSize * 100 / size &lt;&lt; &quot;%&quot; &lt;&lt; flush; // Mit \r springen wir an den Anfang der Zeile
            }
        }
        else
        {
            if(!chunked)
            {
                cout &lt;&lt; &quot;Downloading... (Unknown Filesize)&quot; &lt;&lt; endl;
                while(bytesRecv != 0) // Wenn recv 0 zurück gibt, wurde die Verbindung beendet
                {
                    if((bytesRecv = recv(Socket, buf, sizeof(buf), 0)) &lt; 0)
                    {
                        throw CreateSocketError();
                    }
                    fout.write(buf, bytesRecv);
                }
            }
            else
            {
                cout &lt;&lt; &quot;Downloading... (Chunked)&quot; &lt;&lt; endl;
                while(true)
                {
                    stringstream sstream;
                    GetLine(Socket, sstream);
                    int chunkSize = -1;
                    sstream &gt;&gt; hex &gt;&gt; chunkSize; // Größe des nächsten Parts einlesen
                    if(chunkSize &lt;= 0)
                    {
                        break;
                    }
                    cout &lt;&lt; &quot;Downloading Part (&quot; &lt;&lt; chunkSize &lt;&lt; &quot; Bytes)... &quot; &lt;&lt; endl;
                    recvSize = 0; // Vor jeder Schleife wieder auf 0 setzen
                    while(recvSize &lt; chunkSize)
                    {
                        int bytesToRecv = chunkSize - recvSize;
                        if((bytesRecv = recv(Socket, buf, bytesToRecv &gt; sizeof(buf) ? sizeof(buf) : bytesToRecv, 0)) &lt;= 0)
                        {
                            throw CreateSocketError();
                        }
                        recvSize += bytesRecv;
                        fout.write(buf, bytesRecv);
                        cout &lt;&lt; &quot;\r&quot; &lt;&lt; recvSize * 100 / chunkSize &lt;&lt; &quot;%&quot; &lt;&lt; flush;
                    }
                    cout &lt;&lt; endl;
                    for(int i = 0; i &lt; 2; ++i)
                    {
                        char temp;
                        recv(Socket, &amp;temp, 1, 0);
                    }
                }
            }
        }
        cout &lt;&lt; endl &lt;&lt; &quot;Finished!&quot; &lt;&lt; endl;
    }
    catch(exception&amp; e)
    {
        cout &lt;&lt; endl;
        cerr &lt;&lt; e.what() &lt;&lt; endl;
    }
#ifdef linux
    close(Socket); // Verbindung beenden
#else
    closesocket(Socket); // Windows-Variante
#endif
	cout&lt;&lt;&quot;Success\n&quot;;
	Sleep(3000);
	return 0;
}
</code></pre>
<p>Sleep commandos habe ich zu testzwecken eingbaut.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1571503</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1571503</guid><dc:creator><![CDATA[KOR]]></dc:creator><pubDate>Tue, 26 Aug 2008 07:30:47 GMT</pubDate></item></channel></rss>