<?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[Problem mit Server [gelöst]]]></title><description><![CDATA[<p>Hallo. Also mich plagt ein Problem, dem ich nicht auf die Schliche kommen konnte. Mein Server hat eine Endlosschleife, die aber ihren Dienst nicht tut. Woran liegt das? (Das C im C++ Code sei bitte außer Acht gelassen)</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;winsock.h&gt;

#define BUFFER_SIZE 1024

using namespace std;

int handling(int c)
{
char buffer[BUFFER_SIZE], name[BUFFER_SIZE];
int bytes;
strcpy(buffer, &quot;My name is: &quot;);
bytes = send(c, buffer, strlen(buffer), 0);
if (bytes == -1)
return -1;
bytes = recv(c, name, sizeof(name), 0);
if (bytes == -1)
return -1;
name[bytes] = '\0';
sprintf(buffer, &quot;Hello %s, nice to meet you!\r\n&quot;, name);
bytes = send(c, buffer, strlen(buffer), 0);
if (bytes == -1)
return -1;
return 0;
}
int main(int argc, char *argv[])
{
		    /* initialize windows sockets */
    WSADATA wsa;
    if (WSAStartup(MAKEWORD(1, 1), &amp;wsa))
    {
        cout &lt;&lt; &quot;WSAStartup() failed: &quot; &lt;&lt; (unsigned long)GetLastError() &lt;&lt; endl;
        return EXIT_FAILURE;
    }
int s, c, cli_size;
struct sockaddr_in srv, cli;
if (argc != 2)
{
fprintf(stderr, &quot;usage: %s port\n&quot;, argv[0]);
return 1;
}
s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1)
{
perror(&quot;socket() failed&quot;);
return 2;
}
srv.sin_addr.s_addr = INADDR_ANY;
srv.sin_port = htons( (unsigned short int) atol(argv[1]));
srv.sin_family = AF_INET;
if (bind(s, (struct sockaddr*)&amp;srv, sizeof(srv)) == -1)
{
perror(&quot;bind() failed&quot;);
return 3;
}
if (listen(s, 3) == -1)
{
perror(&quot;listen() failed&quot;);
return 4;
}
for(;;) // &lt;--------------- DIE SCHLEIFE GEHT NICHT. DAS PROGRAMM BRICHT SOFORT AB
{
c = accept(s, (struct sockaddr*)&amp;cli, &amp;cli_size);
if (c == -1)
{
perror(&quot;accept() failed&quot;);
return 5;
}
printf(&quot;client from %s&quot;, inet_ntoa(cli.sin_addr));
if (handling(c) == -1)
fprintf(stderr, &quot;%s: handling() failed&quot;, argv[0]);
/* hier empfiehlt sich kein return mehr, weil sonst der
* ganze Server beendet wird wenn ein Client wegstirbt. Das ist
* natürlich nicht sinnvoll.
*/
closesocket(c);
}
return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/246860/problem-mit-server-gelöst</link><generator>RSS for Node</generator><lastBuildDate>Thu, 17 Sep 2026 20:45:51 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/246860.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 02 Aug 2009 22:30:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit Server [gelöst] on Tue, 04 Aug 2009 00:35:23 GMT]]></title><description><![CDATA[<p>Hallo. Also mich plagt ein Problem, dem ich nicht auf die Schliche kommen konnte. Mein Server hat eine Endlosschleife, die aber ihren Dienst nicht tut. Woran liegt das? (Das C im C++ Code sei bitte außer Acht gelassen)</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;winsock.h&gt;

#define BUFFER_SIZE 1024

using namespace std;

int handling(int c)
{
char buffer[BUFFER_SIZE], name[BUFFER_SIZE];
int bytes;
strcpy(buffer, &quot;My name is: &quot;);
bytes = send(c, buffer, strlen(buffer), 0);
if (bytes == -1)
return -1;
bytes = recv(c, name, sizeof(name), 0);
if (bytes == -1)
return -1;
name[bytes] = '\0';
sprintf(buffer, &quot;Hello %s, nice to meet you!\r\n&quot;, name);
bytes = send(c, buffer, strlen(buffer), 0);
if (bytes == -1)
return -1;
return 0;
}
int main(int argc, char *argv[])
{
		    /* initialize windows sockets */
    WSADATA wsa;
    if (WSAStartup(MAKEWORD(1, 1), &amp;wsa))
    {
        cout &lt;&lt; &quot;WSAStartup() failed: &quot; &lt;&lt; (unsigned long)GetLastError() &lt;&lt; endl;
        return EXIT_FAILURE;
    }
int s, c, cli_size;
struct sockaddr_in srv, cli;
if (argc != 2)
{
fprintf(stderr, &quot;usage: %s port\n&quot;, argv[0]);
return 1;
}
s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1)
{
perror(&quot;socket() failed&quot;);
return 2;
}
srv.sin_addr.s_addr = INADDR_ANY;
srv.sin_port = htons( (unsigned short int) atol(argv[1]));
srv.sin_family = AF_INET;
if (bind(s, (struct sockaddr*)&amp;srv, sizeof(srv)) == -1)
{
perror(&quot;bind() failed&quot;);
return 3;
}
if (listen(s, 3) == -1)
{
perror(&quot;listen() failed&quot;);
return 4;
}
for(;;) // &lt;--------------- DIE SCHLEIFE GEHT NICHT. DAS PROGRAMM BRICHT SOFORT AB
{
c = accept(s, (struct sockaddr*)&amp;cli, &amp;cli_size);
if (c == -1)
{
perror(&quot;accept() failed&quot;);
return 5;
}
printf(&quot;client from %s&quot;, inet_ntoa(cli.sin_addr));
if (handling(c) == -1)
fprintf(stderr, &quot;%s: handling() failed&quot;, argv[0]);
/* hier empfiehlt sich kein return mehr, weil sonst der
* ganze Server beendet wird wenn ein Client wegstirbt. Das ist
* natürlich nicht sinnvoll.
*/
closesocket(c);
}
return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1754288</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1754288</guid><dc:creator><![CDATA[skullyan]]></dc:creator><pubDate>Tue, 04 Aug 2009 00:35:23 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server [gelöst] on Sun, 02 Aug 2009 22:55:58 GMT]]></title><description><![CDATA[<p>Warum nicht einfach</p>
<pre><code class="language-cpp">while(true)
	{ 
		c = accept(s, (struct sockaddr*)&amp;cli, &amp;cli_size); 
		if (c == -1) 
		{ 
			perror(&quot;accept() failed&quot;); 
			return 5; 
		} 
		printf(&quot;client from %s&quot;, inet_ntoa(cli.sin_addr)); 
		if (handling(c) == -1) 
			fprintf(stderr, &quot;%s: handling() failed&quot;, argv[0]); 
		/* hier empfiehlt sich kein return mehr, weil sonst der 
		* ganze Server beendet wird wenn ein Client wegstirbt. Das ist 
		* natürlich nicht sinnvoll. 
		*/ 
		closesocket(c); 
	}
</code></pre>
<p>Gruß eXitus</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1754291</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1754291</guid><dc:creator><![CDATA[eXitus]]></dc:creator><pubDate>Sun, 02 Aug 2009 22:55:58 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server [gelöst] on Sun, 02 Aug 2009 23:05:11 GMT]]></title><description><![CDATA[<p>eXitus schrieb:</p>
<blockquote>
<p>Warum nicht einfach</p>
<pre><code class="language-cpp">while(true)
	{ 
		c = accept(s, (struct sockaddr*)&amp;cli, &amp;cli_size); 
		if (c == -1) 
		{ 
			perror(&quot;accept() failed&quot;); 
			return 5; 
		} 
		printf(&quot;client from %s&quot;, inet_ntoa(cli.sin_addr)); 
		if (handling(c) == -1) 
			fprintf(stderr, &quot;%s: handling() failed&quot;, argv[0]); 
		/* hier empfiehlt sich kein return mehr, weil sonst der 
		* ganze Server beendet wird wenn ein Client wegstirbt. Das ist 
		* natürlich nicht sinnvoll. 
		*/ 
		closesocket(c); 
	}
</code></pre>
<p>Gruß eXitus</p>
</blockquote>
<p>Die Lösung geht leider auch nicht... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1754294</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1754294</guid><dc:creator><![CDATA[skullyan]]></dc:creator><pubDate>Sun, 02 Aug 2009 23:05:11 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server [gelöst] on Mon, 03 Aug 2009 00:31:00 GMT]]></title><description><![CDATA[<p>1. Problem beschreiben<br />
2. Codestelle raussuchen<br />
3. Welche Endlosschleife?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1754307</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1754307</guid><dc:creator><![CDATA[Kóyaánasqatsi]]></dc:creator><pubDate>Mon, 03 Aug 2009 00:31:00 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server [gelöst] on Mon, 03 Aug 2009 00:43:52 GMT]]></title><description><![CDATA[<p>Du hast cli_size nicht initialisiert:</p>
<pre><code class="language-cpp">cli_size = sizeof(SOCKADDR_IN);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1754308</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1754308</guid><dc:creator><![CDATA[BBBB]]></dc:creator><pubDate>Mon, 03 Aug 2009 00:43:52 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server [gelöst] on Mon, 03 Aug 2009 01:37:04 GMT]]></title><description><![CDATA[<p>BBBB schrieb:</p>
<blockquote>
<p>Du hast cli_size nicht initialisiert:</p>
<pre><code class="language-cpp">cli_size = sizeof(SOCKADDR_IN);
</code></pre>
</blockquote>
<p>Stimmt, aber die Schleife bricht trotzdem sofort ab. Sowas habe ich noch nie erlebt, naja... Hier nochmal der Codeblock:</p>
<pre><code class="language-cpp">for(;;) // &lt;--------------- DIE SCHLEIFE GEHT NICHT. DAS PROGRAMM BRICHT SOFORT AB
{
c = accept(s, (struct sockaddr*)&amp;cli, &amp;cli_size);
if (c == -1)
{
perror(&quot;accept() failed&quot;);
return 5;
}
printf(&quot;client from %s&quot;, inet_ntoa(cli.sin_addr));
if (handling(c) == -1)
fprintf(stderr, &quot;%s: handling() failed&quot;, argv[0]);
closesocket(c);
}
return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1754315</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1754315</guid><dc:creator><![CDATA[skullyan]]></dc:creator><pubDate>Mon, 03 Aug 2009 01:37:04 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server [gelöst] on Mon, 03 Aug 2009 04:54:48 GMT]]></title><description><![CDATA[<p>skullyan schrieb:</p>
<blockquote>
<pre><code class="language-cpp">for(;;) // &lt;--------------- DIE SCHLEIFE GEHT NICHT. DAS PROGRAMM BRICHT SOFORT AB
</code></pre>
</blockquote>
<p>WO bricht es ab? bist du mal mit dem Debugger schrittweise durchgegangen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1754319</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1754319</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 03 Aug 2009 04:54:48 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server [gelöst] on Tue, 04 Aug 2009 00:35:01 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>skullyan schrieb:</p>
<blockquote>
<pre><code class="language-cpp">for(;;) // &lt;--------------- DIE SCHLEIFE GEHT NICHT. DAS PROGRAMM BRICHT SOFORT AB
</code></pre>
</blockquote>
<p>WO bricht es ab? bist du mal mit dem Debugger schrittweise durchgegangen?</p>
</blockquote>
<p>Ja, hier:</p>
<pre><code class="language-cpp">if (argc != 2)
{
fprintf(stderr, &quot;usage: %s port\n&quot;, argv[0]);
return 1;
}
</code></pre>
<p>Nachdem ich <code>fprintf</code> mit einer C++ Instanz ( <code>cout</code> ) ersetzt habe, trat der fehler nicht mehr auf.. Komisch... Liegt das vielleicht daran, dass ich kein <code>stdio.h</code> inkludiert hatte? Und wenn ja, wieso ließ sich das Programm überhaupt kompilieren?</p>
<p>EDIT: Wie ich eben festgestellt habe, lag es daran, dass <code>stdio.h</code> nicht inkludiert war. Mich wundert nur, dass der Compiler da nicht meckert... Gelöst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1754880</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1754880</guid><dc:creator><![CDATA[skullyan]]></dc:creator><pubDate>Tue, 04 Aug 2009 00:35:01 GMT</pubDate></item></channel></rss>