<?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[HTTP Protokoll mhhh]]></title><description><![CDATA[<p>PalimPalim,</p>
<p>ich hab mir ein kleines Programm geschrieben, das eine Datei via Socket und HTTP Protokoll von einer Seite auf den lokalen Rechner runterlädt. Also erst Verbindung, dann GET-anfrage und dann Übertragung mit recv(); . So nun kommt das Problem, es werden immer nur 32 KB übertragen, egal, wie groß die Datei ist. Woran kann das liegen? Außerdem: Wenn der Buffer meinetwegen 1024 Bytes groß ist, werden dann auch immer nur 1024 Bytes pro Schleifendruchlauf übertragen?</p>
<p>Gruß PillePalle</p>
<p>Hier der Code</p>
<pre><code class="language-cpp">int main() {
  //----------------------
  // Initialize Winsock
  WSADATA wsaData;
  char command[250];
  int bsend;
  int brevc;
  char buf[1025];
  FILE *fp;

  int iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
  if (iResult != NO_ERROR)
    printf(&quot;Error at WSAStartup()\n&quot;);

  //----------------------
  // Create a SOCKET for connecting to server
  SOCKET ConnectSocket;
  ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (ConnectSocket == INVALID_SOCKET) {
    printf(&quot;Error at socket(): %ld\n&quot;, WSAGetLastError());
    WSACleanup();
    system(&quot;pause&quot;);
    return 0;
  }

  //----------------------
  // The sockaddr_in structure specifies the address family,
  // IP address, and port of the server to be connected to.
  sockaddr_in clientService;
  clientService.sin_family = AF_INET;
  clientService.sin_addr.s_addr = inet_addr( &quot;213.203.239.230&quot; );
  clientService.sin_port = htons( 80 );

  //----------------------
  // Connect to server.
  if ( connect( ConnectSocket, (SOCKADDR*) &amp;clientService, sizeof(clientService) ) == SOCKET_ERROR) {
    printf( &quot;Failed to connect.\n&quot; );
    WSACleanup();
    system(&quot;pause&quot;);
    return 0;
  }

  sprintf(command,&quot;GET /downloads/sonstiges/lichtschalter.zip HTTP/1.0\nHost: maggot.de\n\n&quot;);

  bsend= send(ConnectSocket,command,strlen(command),0);
  if(bsend== -1)
  {
  	printf( &quot;Fehler bei send().\n&quot; );
    WSACleanup();
    system(&quot;pause&quot;);
    return 0;
  }

  fp=fopen(&quot;huhu.zip&quot;,&quot;w+b&quot;);

  while((brevc=recv(ConnectSocket,buf,sizeof(buf)-1,0)) &gt;0)
  {
  	fputs(buf,fp);
  }
  fclose(fp);

  printf(&quot;\nConnected to server.\n&quot;);
  WSACleanup();
  system(&quot;pause&quot;);
  return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/180134/http-protokoll-mhhh</link><generator>RSS for Node</generator><lastBuildDate>Fri, 03 Jul 2026 21:40:31 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/180134.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 29 Apr 2007 11:45:04 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Sun, 29 Apr 2007 11:45:34 GMT]]></title><description><![CDATA[<p>PalimPalim,</p>
<p>ich hab mir ein kleines Programm geschrieben, das eine Datei via Socket und HTTP Protokoll von einer Seite auf den lokalen Rechner runterlädt. Also erst Verbindung, dann GET-anfrage und dann Übertragung mit recv(); . So nun kommt das Problem, es werden immer nur 32 KB übertragen, egal, wie groß die Datei ist. Woran kann das liegen? Außerdem: Wenn der Buffer meinetwegen 1024 Bytes groß ist, werden dann auch immer nur 1024 Bytes pro Schleifendruchlauf übertragen?</p>
<p>Gruß PillePalle</p>
<p>Hier der Code</p>
<pre><code class="language-cpp">int main() {
  //----------------------
  // Initialize Winsock
  WSADATA wsaData;
  char command[250];
  int bsend;
  int brevc;
  char buf[1025];
  FILE *fp;

  int iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
  if (iResult != NO_ERROR)
    printf(&quot;Error at WSAStartup()\n&quot;);

  //----------------------
  // Create a SOCKET for connecting to server
  SOCKET ConnectSocket;
  ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (ConnectSocket == INVALID_SOCKET) {
    printf(&quot;Error at socket(): %ld\n&quot;, WSAGetLastError());
    WSACleanup();
    system(&quot;pause&quot;);
    return 0;
  }

  //----------------------
  // The sockaddr_in structure specifies the address family,
  // IP address, and port of the server to be connected to.
  sockaddr_in clientService;
  clientService.sin_family = AF_INET;
  clientService.sin_addr.s_addr = inet_addr( &quot;213.203.239.230&quot; );
  clientService.sin_port = htons( 80 );

  //----------------------
  // Connect to server.
  if ( connect( ConnectSocket, (SOCKADDR*) &amp;clientService, sizeof(clientService) ) == SOCKET_ERROR) {
    printf( &quot;Failed to connect.\n&quot; );
    WSACleanup();
    system(&quot;pause&quot;);
    return 0;
  }

  sprintf(command,&quot;GET /downloads/sonstiges/lichtschalter.zip HTTP/1.0\nHost: maggot.de\n\n&quot;);

  bsend= send(ConnectSocket,command,strlen(command),0);
  if(bsend== -1)
  {
  	printf( &quot;Fehler bei send().\n&quot; );
    WSACleanup();
    system(&quot;pause&quot;);
    return 0;
  }

  fp=fopen(&quot;huhu.zip&quot;,&quot;w+b&quot;);

  while((brevc=recv(ConnectSocket,buf,sizeof(buf)-1,0)) &gt;0)
  {
  	fputs(buf,fp);
  }
  fclose(fp);

  printf(&quot;\nConnected to server.\n&quot;);
  WSACleanup();
  system(&quot;pause&quot;);
  return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1275593</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1275593</guid><dc:creator><![CDATA[PillePalle]]></dc:creator><pubDate>Sun, 29 Apr 2007 11:45:34 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Sun, 29 Apr 2007 11:50:33 GMT]]></title><description><![CDATA[<p>fwrite</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1275599</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1275599</guid><dc:creator><![CDATA[.......]]></dc:creator><pubDate>Sun, 29 Apr 2007 11:50:33 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Sun, 29 Apr 2007 12:33:12 GMT]]></title><description><![CDATA[<p>PillePalle schrieb:</p>
<blockquote>
<p>Wenn der Buffer meinetwegen 1024 Bytes groß ist, werden dann auch immer nur 1024 Bytes pro Schleifendruchlauf übertragen?</p>
</blockquote>
<p>Wenn man recv(ConnectSocket,buf,len,0) schreibt dann wird maximal len Bytes übertragen, wenn Datei &gt;= len Byte.</p>
<p>Kann aber auch weniger sein, das wird Server-Seitig entschieden, der Server kann also auch weniger reinpacken. Wenn Datei &gt; len Byte, dann leerst du ja auch den Puffer in einer Schleife und lässt ihn neu beschreiben, bis Datei zu ende gelesen ist:</p>
<pre><code class="language-cpp">while(( brevc = recv (ConnectSocket, buf, sizeof(buf), 0 )) &gt;0 ) 
{ 
 fwrite( buf, sizeof(char), brevc, fp )
}
</code></pre>
<p>oder:</p>
<pre><code class="language-cpp">while(( brevc = recv (ConnectSocket, buf, sizeof(buf), 0 )) &gt;0 ) 
{ 
	fwrite( buf, brevc, 1, fp )
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1275621</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1275621</guid><dc:creator><![CDATA[kuckuck]]></dc:creator><pubDate>Sun, 29 Apr 2007 12:33:12 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Sun, 29 Apr 2007 12:39:32 GMT]]></title><description><![CDATA[<p>Dankeschön klappt wunderbar. Nochwas: Hat jemand eine Idee wie man einfach den Header herausnehmen kann? Kann man das mit den Stringfunktionen machen? einfach nach dem ersten\n\n suchen?</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1275630</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1275630</guid><dc:creator><![CDATA[PillePalle]]></dc:creator><pubDate>Sun, 29 Apr 2007 12:39:32 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Sun, 29 Apr 2007 18:15:27 GMT]]></title><description><![CDATA[<blockquote>
<p>einfach nach dem ersten\n\n suchen?</p>
</blockquote>
<p>Ganz genau.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1275830</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1275830</guid><dc:creator><![CDATA[ja genau]]></dc:creator><pubDate>Sun, 29 Apr 2007 18:15:27 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Sun, 29 Apr 2007 18:18:49 GMT]]></title><description><![CDATA[<p>Sorry wegen oben, war zu voreilig. Nicht \n\n sondern \r\n\r\n.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1275833</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1275833</guid><dc:creator><![CDATA[nicht so ganz genau]]></dc:creator><pubDate>Sun, 29 Apr 2007 18:18:49 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Sun, 29 Apr 2007 18:26:04 GMT]]></title><description><![CDATA[<p>Ist ein bisschen schwierig so zu suchen da die Zeichen ja über mehrere Buffer verteilt sein können.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1275836</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1275836</guid><dc:creator><![CDATA[na ja]]></dc:creator><pubDate>Sun, 29 Apr 2007 18:26:04 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Sun, 29 Apr 2007 19:26:56 GMT]]></title><description><![CDATA[<p>Ja genau. Hatte ich auch festgestellt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> . Aber wenn man will geht alles <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1275877</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1275877</guid><dc:creator><![CDATA[PillePalle]]></dc:creator><pubDate>Sun, 29 Apr 2007 19:26:56 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Sun, 29 Apr 2007 19:35:53 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>so hab mal eine provisorische Lösung, ohne mit Berücksichtigung der zerteilten Buffer, gebastelt. Nur ein Problem: Das Programm stürzt ab wenn ich es starte. Woran liegt das?</p>
<pre><code class="language-cpp">while(( brevc = recv (ConnectSocket, buf, sizeof(buf), 0 )) &gt;0 )
{
	for(i=0;i&lt;strlen(buf);i+=1)
	{
		if(buf[i]=='\n' &amp;&amp; buf[i+1]=='\n')
		{
				begin=1;
		}
	}
	if(begin==1)
	{

 fwrite( buf, brevc, 1, fp );
 printf(&quot;\r%0.2f MB empfangen.&quot;,((float)FileSize(&quot;tmp.txt&quot;) /1024)/1024);
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1275885</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1275885</guid><dc:creator><![CDATA[PillePalle]]></dc:creator><pubDate>Sun, 29 Apr 2007 19:35:53 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Sun, 29 Apr 2007 20:16:31 GMT]]></title><description><![CDATA[<p>Der Buffer den du von recv bekommst ist nicht 0 terminiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1275913</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1275913</guid><dc:creator><![CDATA[rüdiger]]></dc:creator><pubDate>Sun, 29 Apr 2007 20:16:31 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Sun, 29 Apr 2007 21:08:49 GMT]]></title><description><![CDATA[<p>Und deswegen belibt das Programm stehen?<br />
Wie kann man das lösen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1275939</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1275939</guid><dc:creator><![CDATA[PillePalle]]></dc:creator><pubDate>Sun, 29 Apr 2007 21:08:49 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Sun, 29 Apr 2007 21:55:22 GMT]]></title><description><![CDATA[<p>Kuckuck.</p>
<p>Ich hatte mir auch mal son Ding getippselt, das Dateien runterladen kann.<br />
Die meisten http-header, die ich als Antwort bekam, waren &lt;512 Byte.<br />
Nur ganz selten mal ein Server, der einen etwas längeren Header zurückgeschickt hat.</p>
<p>Mit dem String-Ende-Zeichen '\0' würde ich gar nicht erst anfangen.<br />
Weder künstlich anhängen, nach suchen, oder sonstwas.<br />
Hatte ich erst auch so und mich dann gewundert, warum es mit Bild- und Videodateien<br />
nicht geklappt hat.</p>
<p>Die benötigte Länge steckt ja bereits in brevc.<br />
for( i = 0; i &lt; brevc - 1; i++ ) // -1 weil du buf[i+1] abfragst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1275960</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1275960</guid><dc:creator><![CDATA[proggingmania]]></dc:creator><pubDate>Sun, 29 Apr 2007 21:55:22 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Mon, 30 Apr 2007 10:46:12 GMT]]></title><description><![CDATA[<p>Aber warum will dann Windows ein Problembericht senden, wenn ich das Prog starte?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1276200</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1276200</guid><dc:creator><![CDATA[PillePalle]]></dc:creator><pubDate>Mon, 30 Apr 2007 10:46:12 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Mon, 30 Apr 2007 11:08:17 GMT]]></title><description><![CDATA[<p>Rüdiger sagte es bereits. Der buffer den recv liefert ist nicht nullterminiert (wie auch, es könnte ja eine Datei übertragen werden, die Nullbytes enthält*). Deshalb schlägt strlen fehl, weil es das Nullbyte gottweisswo, nur nicht in Deinem buffer sucht. Aber zum Glück liefert ja recv die benötigten Informationen als Rückgabewert.</p>
<p>[OT] * oder 0xFF <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> (SCNR) [/OT]</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1276205</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1276205</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Mon, 30 Apr 2007 11:08:17 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Mon, 30 Apr 2007 13:38:54 GMT]]></title><description><![CDATA[<p>Also ich baue mir das, was von recv() kommt erstmal in einen eigenen Buffer zusammen.</p>
<p>An diesen Buffer wird nach jedem Aufruf von recv() zusätzlich 0 angehängt, um mit strstr(Buffer, &quot;Content-Length&quot;) die Länge der (Binär-)Daten zu ermitteln und mit strstr(Buffer, &quot;\r\n\r\n&quot;) nach dem Ende des Headers (=Beginn Content / =Begin (Binär-)Daten) suchen zu können.</p>
<p>recv() == Fehler =&gt; Schleifenabbruch</p>
<p>recv() == 0 =&gt; &quot;gracefully closed&quot; (sollte nicht vorkommen) =&gt; Schleifenabbruch</p>
<p><strong>&quot;Content-Length&quot;:</strong></p>
<p>Länge Buffer &gt;= Beginn Content + Length Content =&gt; Schleifenabbruch</p>
<p><strong>kein &quot;Content-Length&quot;:</strong></p>
<p>strstr(Buffer, &quot;\r\n0\r\n\r\n) =&gt; Ende der Nicht-Binär-Daten =&gt; Schleifenabbruch</p>
<p><strong>Zusätzlich können (HTML-)Texte &quot;chunked&quot; aufgebaut sein.</strong></p>
<pre><code>a\r\n &lt;- Länge des nächsten Teilstücks im hex-Format + \r\n
0123456789\r\n &lt;- Teilstück + \r\n
8\r\n
89012345\r\n
0\r\n\r\n &lt;- Endekennzeichen &quot;\r\n0\r\n\r\n&quot;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1276318</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1276318</guid><dc:creator><![CDATA[keksekekse]]></dc:creator><pubDate>Mon, 30 Apr 2007 13:38:54 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Mon, 30 Apr 2007 15:38:19 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/profile-var-mode-is-viewprofile-and-u-is-14811.html" rel="nofollow">Tim</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-10.html" rel="nofollow">ANSI C</a> in das Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-8.html" rel="nofollow">Rund um die Programmierung</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-39405.html" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1276415</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1276415</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Mon, 30 Apr 2007 15:38:19 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP Protokoll mhhh on Tue, 01 May 2007 00:03:16 GMT]]></title><description><![CDATA[<p>Ich zeig dir mal wie ich das in C++ gemacht gemacht habe. Ist aber nicht so gut getestet. An die Funktion processChunk kannst du den Puffer den dir recv befüllt hat übergeben.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cstring&gt;
#include &lt;cassert&gt;

class LineBuffer
{
public:
	LineBuffer(std::size_t maximumLineLength)
		: maximumLineLength_(maximumLineLength)
		, isLineComplete_(false)
	{
	}

	std::size_t consumeChunk(const char* pBuffer, std::size_t bufferSize)
	{
		if(isLineComplete_)	{ return 0;	}

		std::size_t newlineCharacterPosition;
		bool newlineCharacterFound = findNewlineCharacter(pBuffer, bufferSize, newlineCharacterPosition);
		std::size_t bytesToConsume = newlineCharacterFound ? newlineCharacterPosition + 1 : bufferSize;

		if(line_.length() + bytesToConsume &gt; maximumLineLength_)
		{
			throw LineTooLongException();
		}

		line_ += std::string(pBuffer, bytesToConsume);

		if(newlineCharacterFound)
		{
			isLineComplete_ = true;
			removeLineTerminator();
		}

		return bytesToConsume;
	}

	bool isLineComplete() const
	{
		return isLineComplete_;
	}

	const std::string&amp; getLine() const
	{
		return line_;
	}

	void reset()
	{
		line_.erase();
		isLineComplete_ = false;
	}
private:
	std::size_t getLineTerminatorLength() const
	{
		assert(isLineComplete());
		assert(line_.length() &gt;= 1 &amp;&amp; line_[line_.length() - 1] == '\n');

		if(line_.length() &gt;= 2 &amp;&amp; line_[line_.length() - 2] == '\r')
		{
			return 2;
		}
		else
		{
			return 1;
		}
	}

	void removeLineTerminator()
	{
		assert(isLineComplete());

		line_.erase(line_.length() - getLineTerminatorLength(), std::string::npos);
	}

	bool findNewlineCharacter(const char* pBuffer,
		std::size_t bufferSize,
		std::size_t&amp; newlineCharacterPosition) const
	{
		const char* pNewlineCharacter = static_cast&lt;const char*&gt;(std::memchr(pBuffer, '\n', bufferSize));

		if(pNewlineCharacter == NULL)
		{
			return false;
		}
		else
		{
			newlineCharacterPosition = pNewlineCharacter - pBuffer;
			return true;
		}
	}
private:
	std::string line_;
	bool isLineComplete_;
	const std::size_t maximumLineLength_;
public:
	class LineTooLongException
	{
	};
};

LineBuffer lineBuffer(4096);

enum State
{
	HEADERS,
	BODY
};

State state = HEADERS;

void processLine(const std::string&amp; line)
{
	if(line.empty())
	{
		state = BODY;
	}
	/*else
	{
		std::cout &lt;&lt; &quot;|&quot; &lt;&lt; line &lt;&lt; &quot;|&quot; &lt;&lt; std::endl;
	}*/
}

std::size_t processLineChunk(const char* pBuffer, std::size_t bufferSize)
{
	std::size_t bytesConsumed = lineBuffer.consumeChunk(pBuffer, bufferSize);

	if(lineBuffer.isLineComplete())
	{
		processLine(lineBuffer.getLine());
		lineBuffer.reset();
	}

	return bytesConsumed; 
}

std::size_t processBodyChunk(const char* pBuffer, std::size_t bufferSize)
{
	std::cout &lt;&lt; std::string(pBuffer, bufferSize);
	return bufferSize;
}

void processChunk(const char* pBuffer, std::size_t bufferSize)
{
	std::size_t totalBytesConsumed = 0;

	while(totalBytesConsumed &lt; bufferSize)
	{
		switch(state)
		{
		case HEADERS:
			totalBytesConsumed += processLineChunk(pBuffer + totalBytesConsumed, bufferSize - totalBytesConsumed);
			break;
		case BODY:
			totalBytesConsumed += processBodyChunk(pBuffer + totalBytesConsumed, bufferSize - totalBytesConsumed);
			break;
		}
	}
}

int main()
{
	try
	{
		std::string input = &quot;HTTP/1.1 200 OK\r\n&quot;
							&quot;Server: Apache\r\n&quot;
							&quot;Connection: close\r\n&quot;
							&quot;Content-Type: text/html\r\n&quot;
							&quot;\r\n&quot;
							&quot;&lt;html&gt;Hallo Welt&lt;/html&gt;&quot;;

		processChunk(input.data(), input.length());
	}
	catch(LineBuffer::LineTooLongException&amp;)
	{
		std::cout &lt;&lt; &quot;Line too long&quot; &lt;&lt; std::endl;
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1276697</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1276697</guid><dc:creator><![CDATA[trustee.]]></dc:creator><pubDate>Tue, 01 May 2007 00:03:16 GMT</pubDate></item></channel></rss>