<?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[Socket und Co]]></title><description><![CDATA[<p>Hiho,</p>
<p>hab mich mal an die Socketprogrammierung in Cpp gewagt.</p>
<p>So weit alles kein Ding. Versteh die ganzen Zusammenhänge, Funktionen, Bla!</p>
<p>Komme mit allem klar. Außer mit einem.</p>
<p>Ich dachte mir mal: so... connecten wa den neuen Client mal auf <a href="http://google.de:80" rel="nofollow">google.de:80</a></p>
<p>.. kurz <a href="http://google.de" rel="nofollow">google.de</a> gepingt, IP in den Client gehackt und den Port auf 80 gestellt.</p>
<p>Zu Anfang vergaß ich, dass zuerst der Client an Port 80 schicken muss, ehe der ganze HTML Quatsch kommt.</p>
<p>Anschließend hab ich ne Sendmessage eingebunden.</p>
<p>Inhalt:</p>
<pre><code>GET ./index.html HTTP1.1
</code></pre>
<p>Vorher natürlich mit telnet getestet. Klappte dort prima.</p>
<p>Jedoch bekam ich nach dem erfolgreichen der Nachricht kein Reply.</p>
<p>Habs anschließend &quot;für Dumme&quot; auf</p>
<pre><code>GET ./ HTTP1.1
</code></pre>
<p>abgeändert, jedoch schickt er genau so wie beim ersten Versuch nur die Message erfolgreich raus.</p>
<p>Anschließend wartet er vergeblich auf eine Antwort. D.h. die Antwort ist nichtmal nichts, sondern es existiert keine.</p>
<p>Hier mal mein Codegedöhns ...</p>
<pre><code class="language-cpp">#include &quot;stdafx.h&quot;

#include &lt;iostream&gt;
#include &lt;string&gt;

#include &lt;winsock2.h&gt;
#include &lt;windows.h&gt;

using namespace std;

int startWinsock( void );

int main( void ) {

	long getWinsock;
	SOCKET mySocket;
	int SocketConnect;
	int SocketSend;
	int SocketReceive;
	SOCKADDR_IN SocketAddress;
	char SocketMessage[] = &quot;GET ./ HTTP1.1&quot;;
	char SocketReceive_Buffer[256];

	unsigned short addrPort = 80;
	char *addrIP = &quot;217.72.195.42&quot;;

	getWinsock = startWinsock();

	if( getWinsock == 0 ) {

		cout&lt;&lt;&quot;Winsock wurde erfolgreich gestartet.\n&quot;;

		mySocket = socket( AF_INET, SOCK_STREAM, 0 );

		if( mySocket == INVALID_SOCKET ) 
			cout&lt;&lt;&quot;Fehler beim Erstellen des Sockets.\nFehlercode: &quot;&lt;&lt;WSAGetLastError()&lt;&lt;&quot;\n&quot;;

		else {

			cout&lt;&lt;&quot;Socket wurde erfolgreich erstellt.\n&quot;;

			memset( &amp;SocketAddress, 0, sizeof( SocketAddress ) );

			SocketAddress.sin_family = AF_INET;
			SocketAddress.sin_port = htons( addrPort );
			SocketAddress.sin_addr.s_addr = inet_addr( addrIP );

			SocketConnect = connect( mySocket, (SOCKADDR*)&amp;SocketAddress, sizeof( SocketAddress ) );

			if( SocketConnect != SOCKET_ERROR ) {

				cout&lt;&lt;&quot;Verbindung wurde erfolgreich hergestellt.\n&quot;;

				cout&lt;&lt;&quot;Sende Nachricht '&quot;&lt;&lt;SocketMessage&lt;&lt;&quot;' ...\n&quot;;

				SocketSend = send( mySocket, SocketMessage, strlen( SocketMessage ), 0 );

				if( SocketSend != SOCKET_ERROR ) {

					cout&lt;&lt;&quot;Nachricht wurde erfolgreich gesendet.\n&quot;&lt;&lt;SocketSend&lt;&lt;&quot; Bytes uebertragen.\n&quot;;

					cout&lt;&lt;&quot;Warten auf Nachricht ...\n&quot;;

					SocketReceive = recv( mySocket, SocketReceive_Buffer, 256, 0 );

					if( SocketReceive == SOCKET_ERROR )
						cout&lt;&lt;&quot;Fehler beim Warten auf Nachricht.\nFehlercode: &quot;&lt;&lt;WSAGetLastError()&lt;&lt;&quot;\n&quot;;

					else if ( SocketReceive == 0 )
						cout&lt;&lt;&quot;Die Gegenseite hat die Verbindung beendet.\n&quot;;

					else {

						cout&lt;&lt;&quot;Daten empfangen.\n&quot;&lt;&lt;SocketReceive&lt;&lt;&quot; Bytes wurden von der Gegenseite empfangen.\n&quot;;

						cout&lt;&lt;&quot;Daten: &quot;&lt;&lt;SocketReceive_Buffer&lt;&lt;&quot;\n&quot;;

					}

				} else
					cout&lt;&lt;&quot;Fehler beim Senden der Nachricht.\nFehlercode: &quot;&lt;&lt;WSAGetLastError()&lt;&lt;&quot;\n&quot;;

			} else
				cout&lt;&lt;&quot;Fehler beim Verbinden zu Adresse &quot;&lt;&lt;addrIP&lt;&lt;&quot;:&quot;&lt;&lt;addrPort&lt;&lt;&quot;.\nFehlercode: &quot;&lt;&lt;WSAGetLastError()&lt;&lt;&quot;\n&quot;;

		}

	} else
		cout&lt;&lt;&quot;Fehler beim Starten von Winsock.\nFehlercode: &quot;&lt;&lt;getWinsock&lt;&lt;&quot;\n&quot;;

	cout&lt;&lt;&quot;\n&quot;;
	system( &quot;pause&quot; );

	return 0;

}

int startWinsock( void ) {

  WSADATA wsa;

  return WSAStartup( MAKEWORD( 2, 0 ), &amp;wsa );

}
</code></pre>
<p>Evtl. hat ja jemand die passende Lösung für mich.</p>
<p>Würde mich auf jeden Fall schon mal riesig über ne Antwort freuen.</p>
<p>Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/221534/socket-und-co</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Apr 2026 02:20:36 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/221534.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 31 Aug 2008 00:06:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Socket und Co on Sun, 31 Aug 2008 00:10:07 GMT]]></title><description><![CDATA[<p>Hiho,</p>
<p>hab mich mal an die Socketprogrammierung in Cpp gewagt.</p>
<p>So weit alles kein Ding. Versteh die ganzen Zusammenhänge, Funktionen, Bla!</p>
<p>Komme mit allem klar. Außer mit einem.</p>
<p>Ich dachte mir mal: so... connecten wa den neuen Client mal auf <a href="http://google.de:80" rel="nofollow">google.de:80</a></p>
<p>.. kurz <a href="http://google.de" rel="nofollow">google.de</a> gepingt, IP in den Client gehackt und den Port auf 80 gestellt.</p>
<p>Zu Anfang vergaß ich, dass zuerst der Client an Port 80 schicken muss, ehe der ganze HTML Quatsch kommt.</p>
<p>Anschließend hab ich ne Sendmessage eingebunden.</p>
<p>Inhalt:</p>
<pre><code>GET ./index.html HTTP1.1
</code></pre>
<p>Vorher natürlich mit telnet getestet. Klappte dort prima.</p>
<p>Jedoch bekam ich nach dem erfolgreichen der Nachricht kein Reply.</p>
<p>Habs anschließend &quot;für Dumme&quot; auf</p>
<pre><code>GET ./ HTTP1.1
</code></pre>
<p>abgeändert, jedoch schickt er genau so wie beim ersten Versuch nur die Message erfolgreich raus.</p>
<p>Anschließend wartet er vergeblich auf eine Antwort. D.h. die Antwort ist nichtmal nichts, sondern es existiert keine.</p>
<p>Hier mal mein Codegedöhns ...</p>
<pre><code class="language-cpp">#include &quot;stdafx.h&quot;

#include &lt;iostream&gt;
#include &lt;string&gt;

#include &lt;winsock2.h&gt;
#include &lt;windows.h&gt;

using namespace std;

int startWinsock( void );

int main( void ) {

	long getWinsock;
	SOCKET mySocket;
	int SocketConnect;
	int SocketSend;
	int SocketReceive;
	SOCKADDR_IN SocketAddress;
	char SocketMessage[] = &quot;GET ./ HTTP1.1&quot;;
	char SocketReceive_Buffer[256];

	unsigned short addrPort = 80;
	char *addrIP = &quot;217.72.195.42&quot;;

	getWinsock = startWinsock();

	if( getWinsock == 0 ) {

		cout&lt;&lt;&quot;Winsock wurde erfolgreich gestartet.\n&quot;;

		mySocket = socket( AF_INET, SOCK_STREAM, 0 );

		if( mySocket == INVALID_SOCKET ) 
			cout&lt;&lt;&quot;Fehler beim Erstellen des Sockets.\nFehlercode: &quot;&lt;&lt;WSAGetLastError()&lt;&lt;&quot;\n&quot;;

		else {

			cout&lt;&lt;&quot;Socket wurde erfolgreich erstellt.\n&quot;;

			memset( &amp;SocketAddress, 0, sizeof( SocketAddress ) );

			SocketAddress.sin_family = AF_INET;
			SocketAddress.sin_port = htons( addrPort );
			SocketAddress.sin_addr.s_addr = inet_addr( addrIP );

			SocketConnect = connect( mySocket, (SOCKADDR*)&amp;SocketAddress, sizeof( SocketAddress ) );

			if( SocketConnect != SOCKET_ERROR ) {

				cout&lt;&lt;&quot;Verbindung wurde erfolgreich hergestellt.\n&quot;;

				cout&lt;&lt;&quot;Sende Nachricht '&quot;&lt;&lt;SocketMessage&lt;&lt;&quot;' ...\n&quot;;

				SocketSend = send( mySocket, SocketMessage, strlen( SocketMessage ), 0 );

				if( SocketSend != SOCKET_ERROR ) {

					cout&lt;&lt;&quot;Nachricht wurde erfolgreich gesendet.\n&quot;&lt;&lt;SocketSend&lt;&lt;&quot; Bytes uebertragen.\n&quot;;

					cout&lt;&lt;&quot;Warten auf Nachricht ...\n&quot;;

					SocketReceive = recv( mySocket, SocketReceive_Buffer, 256, 0 );

					if( SocketReceive == SOCKET_ERROR )
						cout&lt;&lt;&quot;Fehler beim Warten auf Nachricht.\nFehlercode: &quot;&lt;&lt;WSAGetLastError()&lt;&lt;&quot;\n&quot;;

					else if ( SocketReceive == 0 )
						cout&lt;&lt;&quot;Die Gegenseite hat die Verbindung beendet.\n&quot;;

					else {

						cout&lt;&lt;&quot;Daten empfangen.\n&quot;&lt;&lt;SocketReceive&lt;&lt;&quot; Bytes wurden von der Gegenseite empfangen.\n&quot;;

						cout&lt;&lt;&quot;Daten: &quot;&lt;&lt;SocketReceive_Buffer&lt;&lt;&quot;\n&quot;;

					}

				} else
					cout&lt;&lt;&quot;Fehler beim Senden der Nachricht.\nFehlercode: &quot;&lt;&lt;WSAGetLastError()&lt;&lt;&quot;\n&quot;;

			} else
				cout&lt;&lt;&quot;Fehler beim Verbinden zu Adresse &quot;&lt;&lt;addrIP&lt;&lt;&quot;:&quot;&lt;&lt;addrPort&lt;&lt;&quot;.\nFehlercode: &quot;&lt;&lt;WSAGetLastError()&lt;&lt;&quot;\n&quot;;

		}

	} else
		cout&lt;&lt;&quot;Fehler beim Starten von Winsock.\nFehlercode: &quot;&lt;&lt;getWinsock&lt;&lt;&quot;\n&quot;;

	cout&lt;&lt;&quot;\n&quot;;
	system( &quot;pause&quot; );

	return 0;

}

int startWinsock( void ) {

  WSADATA wsa;

  return WSAStartup( MAKEWORD( 2, 0 ), &amp;wsa );

}
</code></pre>
<p>Evtl. hat ja jemand die passende Lösung für mich.</p>
<p>Würde mich auf jeden Fall schon mal riesig über ne Antwort freuen.</p>
<p>Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1574118</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1574118</guid><dc:creator><![CDATA[unlimieD.paSe]]></dc:creator><pubDate>Sun, 31 Aug 2008 00:10:07 GMT</pubDate></item><item><title><![CDATA[Reply to Socket und Co on Sun, 31 Aug 2008 02:54:35 GMT]]></title><description><![CDATA[<p>1. Fehler, falsches Forum. Ich weiss echt nicht, wie ihr immer WINsock mit C++ in Verbindung bringen könnt und nicht mit WinAPI.</p>
<p>2. Fehler, das HTTP Protokoll verlangt ein anderes Format!</p>
<p>unlimieD.paSe schrieb:</p>
<blockquote>
<pre><code>GET ./ HTTP1.1
</code></pre>
</blockquote>
<p>Das ist keine gültige HTTP/1.1 Anfrage. Richtig wäre:</p>
<pre><code>GET / HTTP/1.1
Host: www.google.de
</code></pre>
<p>Oder als C++ String: <code>&quot;GET / HTTP/1.1\r\nHost: www.google.de\r\n\r\n&quot;</code><br />
Davon ist alles wichtig, auch die ganzen neuen Zeilen sind entscheidend!</p>
<p>3. Fehler, Angabe der TCP Verbindung vergessen:<br />
<code>mySocket = socket( AF_INET, SOCK_STREAM, 0 );</code><br />
Sollte lauten:<br />
<code>mySocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );</code></p>
<p>4. Fehler, die IP Adresse. Die kann extrem varieren und sollte deshalb immer neu aufgelöst werden. Am besten mit dieser Funktion:<br />
<a href="http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx" rel="nofollow">getaddrinfo</a><br />
Auch darauf schauen, dass die IP und der angegebene Host im HTTP-Get übereinstimmen! Google hat sogar unterschiedliche IPs für <a href="http://www.google.de" rel="nofollow">www.google.de</a> und <a href="http://google.de" rel="nofollow">google.de</a>!</p>
<p>5. Warnung, du benutzt teilweise deprecated Funktionen. Winsocket Funktionen:<br />
<a href="http://msdn.microsoft.com/en-us/library/ms741394(VS.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms741394(VS.85).aspx</a></p>
<p>Wenn du das alles einhälst, dann funktioniert es. Bei mir kam jedenfalls ein OK zurück mit Inhalt.<br />
Ich möchte aber noch darauf hinweisen, dass Google chunked überträgt. Also Google sendet mehrere &quot;Pakete&quot; an Daten. Man erfährt die Menge, welche übermittelt wird, durch eine Hexadezimalzahl, welche auch übermittelt wird. Ich empfehle dazu das RFC 2616 zu lesen. Die Sache mit dem Chunked steht im Kapitel 3.6, bzw. 3.6.1. Es empfiehlt sich aber durchaus auch ca. die ersten 40 Seiten zu lesen. Der Rest ist mehr Nachschlagewerk.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1574129</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1574129</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Sun, 31 Aug 2008 02:54:35 GMT</pubDate></item><item><title><![CDATA[Reply to Socket und Co on Sun, 31 Aug 2008 05:54:17 GMT]]></title><description><![CDATA[<p>SocketReceive_Buffer ist außerdem nicht null-terminiert.<br />
Das gibt bei cout wohl nen seltsamen output.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1574135</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1574135</guid><dc:creator><![CDATA[null-terminiert]]></dc:creator><pubDate>Sun, 31 Aug 2008 05:54:17 GMT</pubDate></item><item><title><![CDATA[Reply to Socket und Co on Sun, 31 Aug 2008 08:25:20 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-10455.html" rel="nofollow">evilissimo</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-15.html" rel="nofollow">C++</a> in das Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-4.html" rel="nofollow">WinAPI</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/1574159</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1574159</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Sun, 31 Aug 2008 08:25:20 GMT</pubDate></item></channel></rss>