<?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 SERVER]]></title><description><![CDATA[<p>/*********************************************************************</p>
<p>Ein sehr(!!) Simpler Socket Server.<br />
-----------------------------------</p>
<p>Beachtet das es nur ein Beispiel ist welches nicht ausprogrammiert ist.<br />
Es fehlen Anweisungen wie closesocket und WSACleanup.<br />
Ebenfalls wird hier auch nicht verhindert das mehrere Verbindungen<br />
hergestellt werden können.<br />
Aber zum demonstieren wie man eine Verbingung annimmt und Daten<br />
sendet und empfänget ist es recht geeignet.<br />
Eventuell hat es auch noch einige Fehler im Code..</p>
<pre><code class="language-cpp">#include &lt;iostream.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;windows.h&gt;
#include &lt;winsock2.h&gt;

#define  SERVER_PORT             5432
#define  RECV_BUF_MAXLEN         256
#define  SEND_BUF_MAXLEN         256

long WinsockStartup();

int main()
{
  long   rc;

  SOCKET sockListen;
  SOCKET sockConnected;
  SOCKADDR_IN addr;
  int addrlen = sizeof(addr);

  char sendBuf[SEND_BUF_MAXLEN+1];
  char recvBuf[RECV_BUF_MAXLEN+1];

  addr.sin_addr.s_addr = 0;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(SERVER_PORT);

  rc = WinsockStartup();
  if (rc == SOCKET_ERROR)
  {
    cout &lt;&lt; &quot;Error: Winsock Startup failed: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
    return 0;
  }
  sockListen=socket(AF_INET,SOCK_STREAM,NULL);
  if (sockListen == INVALID_SOCKET)
  {
    cout &lt;&lt; &quot;Error: Cannot create Socket: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
    return 0;
  }
  rc = bind(sockListen, (SOCKADDR*)&amp;addr, sizeof(addr));
  if (rc == SOCKET_ERROR)
  {
    cout &lt;&lt; &quot;Error: Cannot bind Socket: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
    return 0;
  }
  rc = listen(sockListen, NULL);
  if (rc == SOCKET_ERROR)
  {
    cout &lt;&lt; &quot;Error: Cannot listen: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
    return 0;
  }
  cout &lt;&lt; &quot;Waiting for a connection...&quot; &lt;&lt; endl;
  sockConnected=accept(sockListen, (SOCKADDR*)&amp;addr, &amp;addrlen);
  if (sockConnected == INVALID_SOCKET)
  {
    cout &lt;&lt; &quot;Error: Cannot accept connection: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
    return 0;
  }
  rc = send(sockConnected,&quot;Hallo\0&quot;,6,NULL);
  if (rc == SOCKET_ERROR)
  {
    cout &lt;&lt; &quot;Error: Cannot send Data: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
    return 0;
  }
  cout &lt;&lt; rc &lt;&lt; &quot; Bytes sent !&quot; &lt;&lt; endl;
  while (1)
  {
    rc = recv(sockConnected,recvBuf,RECV_BUF_MAXLEN,NULL);
    if (rc == SOCKET_ERROR)
    {
      cout &lt;&lt; &quot;Error: Cannot recv Data: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
      return 0;
    }
    if (rc == 0)
    {
      cout &lt;&lt; &quot;Other Host has closed the connection !&quot; &lt;&lt; endl;
      return 0;
    }
    recvBuf[rc] = '\0';
    cout &lt;&lt; rc &lt;&lt; &quot; Bytes received: &quot; &lt;&lt; recvBuf &lt;&lt; endl;
    rc = send(sockConnected,&quot;Ganz meine Meinung\0&quot;,19,NULL);
    if (rc == SOCKET_ERROR)
    {
      cout &lt;&lt; &quot;Error: Cannot send Data: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
      return 0;
    }
    cout &lt;&lt; rc &lt;&lt; &quot; Bytes sent !&quot; &lt;&lt; endl;
  }
}

long WinsockStartup()
{
  long rc;

  WORD wVersionRequested;
  WSADATA wsaData;
  wVersionRequested = MAKEWORD(2, 1);

  rc = WSAStartup( wVersionRequested, &amp;wsaData );
  return rc;
}
</code></pre>
<p>Testen könnt ihr ihn indem ihn auf '127.0.0.1 5432' eine telnet Verbindung<br />
herstellt (telnet 127.0.0.1 5432). Er wird euch erst &quot;Hallo&quot; sagen,<br />
und dann alle weiteren Daten welche ihr sendet mit &quot;Ganz meine Meinung&quot;<br />
beantworten. Simpel halt.</p>
<p>MEINE FRAGE:<br />
Wo wird hier die IP 127.0.0.1 übergeben?<br />
Bin noch Anfänger</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/79439/socket-server</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 21:57:50 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/79439.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 11 Jul 2004 13:18:29 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to SOCKET SERVER on Sun, 11 Jul 2004 13:18:29 GMT]]></title><description><![CDATA[<p>/*********************************************************************</p>
<p>Ein sehr(!!) Simpler Socket Server.<br />
-----------------------------------</p>
<p>Beachtet das es nur ein Beispiel ist welches nicht ausprogrammiert ist.<br />
Es fehlen Anweisungen wie closesocket und WSACleanup.<br />
Ebenfalls wird hier auch nicht verhindert das mehrere Verbindungen<br />
hergestellt werden können.<br />
Aber zum demonstieren wie man eine Verbingung annimmt und Daten<br />
sendet und empfänget ist es recht geeignet.<br />
Eventuell hat es auch noch einige Fehler im Code..</p>
<pre><code class="language-cpp">#include &lt;iostream.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;windows.h&gt;
#include &lt;winsock2.h&gt;

#define  SERVER_PORT             5432
#define  RECV_BUF_MAXLEN         256
#define  SEND_BUF_MAXLEN         256

long WinsockStartup();

int main()
{
  long   rc;

  SOCKET sockListen;
  SOCKET sockConnected;
  SOCKADDR_IN addr;
  int addrlen = sizeof(addr);

  char sendBuf[SEND_BUF_MAXLEN+1];
  char recvBuf[RECV_BUF_MAXLEN+1];

  addr.sin_addr.s_addr = 0;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(SERVER_PORT);

  rc = WinsockStartup();
  if (rc == SOCKET_ERROR)
  {
    cout &lt;&lt; &quot;Error: Winsock Startup failed: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
    return 0;
  }
  sockListen=socket(AF_INET,SOCK_STREAM,NULL);
  if (sockListen == INVALID_SOCKET)
  {
    cout &lt;&lt; &quot;Error: Cannot create Socket: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
    return 0;
  }
  rc = bind(sockListen, (SOCKADDR*)&amp;addr, sizeof(addr));
  if (rc == SOCKET_ERROR)
  {
    cout &lt;&lt; &quot;Error: Cannot bind Socket: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
    return 0;
  }
  rc = listen(sockListen, NULL);
  if (rc == SOCKET_ERROR)
  {
    cout &lt;&lt; &quot;Error: Cannot listen: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
    return 0;
  }
  cout &lt;&lt; &quot;Waiting for a connection...&quot; &lt;&lt; endl;
  sockConnected=accept(sockListen, (SOCKADDR*)&amp;addr, &amp;addrlen);
  if (sockConnected == INVALID_SOCKET)
  {
    cout &lt;&lt; &quot;Error: Cannot accept connection: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
    return 0;
  }
  rc = send(sockConnected,&quot;Hallo\0&quot;,6,NULL);
  if (rc == SOCKET_ERROR)
  {
    cout &lt;&lt; &quot;Error: Cannot send Data: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
    return 0;
  }
  cout &lt;&lt; rc &lt;&lt; &quot; Bytes sent !&quot; &lt;&lt; endl;
  while (1)
  {
    rc = recv(sockConnected,recvBuf,RECV_BUF_MAXLEN,NULL);
    if (rc == SOCKET_ERROR)
    {
      cout &lt;&lt; &quot;Error: Cannot recv Data: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
      return 0;
    }
    if (rc == 0)
    {
      cout &lt;&lt; &quot;Other Host has closed the connection !&quot; &lt;&lt; endl;
      return 0;
    }
    recvBuf[rc] = '\0';
    cout &lt;&lt; rc &lt;&lt; &quot; Bytes received: &quot; &lt;&lt; recvBuf &lt;&lt; endl;
    rc = send(sockConnected,&quot;Ganz meine Meinung\0&quot;,19,NULL);
    if (rc == SOCKET_ERROR)
    {
      cout &lt;&lt; &quot;Error: Cannot send Data: &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
      return 0;
    }
    cout &lt;&lt; rc &lt;&lt; &quot; Bytes sent !&quot; &lt;&lt; endl;
  }
}

long WinsockStartup()
{
  long rc;

  WORD wVersionRequested;
  WSADATA wsaData;
  wVersionRequested = MAKEWORD(2, 1);

  rc = WSAStartup( wVersionRequested, &amp;wsaData );
  return rc;
}
</code></pre>
<p>Testen könnt ihr ihn indem ihn auf '127.0.0.1 5432' eine telnet Verbindung<br />
herstellt (telnet 127.0.0.1 5432). Er wird euch erst &quot;Hallo&quot; sagen,<br />
und dann alle weiteren Daten welche ihr sendet mit &quot;Ganz meine Meinung&quot;<br />
beantworten. Simpel halt.</p>
<p>MEINE FRAGE:<br />
Wo wird hier die IP 127.0.0.1 übergeben?<br />
Bin noch Anfänger</p>
]]></description><link>https://www.c-plusplus.net/forum/post/558063</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/558063</guid><dc:creator><![CDATA[hermes]]></dc:creator><pubDate>Sun, 11 Jul 2004 13:18:29 GMT</pubDate></item><item><title><![CDATA[Reply to SOCKET SERVER on Sun, 11 Jul 2004 13:25:30 GMT]]></title><description><![CDATA[<p>hermes schrieb:</p>
<blockquote>
<p>Es fehlen Anweisungen wie closesocket und WSACleanup.<br />
Ebenfalls wird hier auch nicht verhindert das mehrere Verbindungen<br />
hergestellt werden können.<br />
Aber zum demonstieren wie man eine Verbingung annimmt und Daten<br />
sendet und empfänget ist es recht geeignet.</p>
</blockquote>
<p>Dafür ist es eben nicht so gut geeignet, wenn Aufrufe von closesocket und WSACleanup fehlen. <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="🙄"
    /><br />
So ein Beispiel sollte immer so kurz wie möglich, aber so lang wie nötig sein.</p>
<blockquote>
<p>MEINE FRAGE:<br />
Wo wird hier die IP 127.0.0.1 übergeben?</p>
</blockquote>
<p>Es gibt da die Variable addr. Das sieht doch stark nach der Adresse aus. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/558069</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/558069</guid><dc:creator><![CDATA[Christoph]]></dc:creator><pubDate>Sun, 11 Jul 2004 13:25:30 GMT</pubDate></item><item><title><![CDATA[Reply to SOCKET SERVER on Sun, 11 Jul 2004 13:56:40 GMT]]></title><description><![CDATA[<p>Ich würde gerne Daten mit einem anderen PC im LAN oder WAN austauschen,<br />
ist diese Methode überhaupt die richtige dafür?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/558088</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/558088</guid><dc:creator><![CDATA[hermes]]></dc:creator><pubDate>Sun, 11 Jul 2004 13:56:40 GMT</pubDate></item><item><title><![CDATA[Reply to SOCKET SERVER on Sun, 11 Jul 2004 16:07:22 GMT]]></title><description><![CDATA[<p>Lern erst mal die Grundlagen.</p>
<p>So wie ich dich einschätze wird ohne ein Buch zu WINSOCK nichts klappen.<br />
Hol dir ein Buch.</p>
<p>MfG, Tolga.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/558168</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/558168</guid><dc:creator><![CDATA[Tolga]]></dc:creator><pubDate>Sun, 11 Jul 2004 16:07:22 GMT</pubDate></item><item><title><![CDATA[Reply to SOCKET SERVER on Mon, 12 Jul 2004 13:37:37 GMT]]></title><description><![CDATA[<p>es gibt zu winsock keine aktuelle [edit] deutschen [/edit] bücher (zumindest nicht bei amazon oder bol) aber wenn du die tutorials auf der seite liest wo du den sourcecode herhast (böse böse keine quellen anzugeben <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_down"
      title=":-1:"
      alt="👎"
    /> ) wirst du das wohl hinbekommen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/558175</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/558175</guid><dc:creator><![CDATA[muhkuhmasta]]></dc:creator><pubDate>Mon, 12 Jul 2004 13:37:37 GMT</pubDate></item><item><title><![CDATA[Reply to SOCKET SERVER on Sun, 11 Jul 2004 18:18:25 GMT]]></title><description><![CDATA[<p>muhkuhmasta schrieb:</p>
<blockquote>
<p>es gibt zu winsock keine aktuellen bücher</p>
</blockquote>
<p>Da würde ich an deiner Stelle hoffen, dass das keiner mitbekommt.. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/558303</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/558303</guid><dc:creator><![CDATA[Tolga]]></dc:creator><pubDate>Sun, 11 Jul 2004 18:18:25 GMT</pubDate></item><item><title><![CDATA[Reply to SOCKET SERVER on Sun, 11 Jul 2004 18:44:27 GMT]]></title><description><![CDATA[<p><a href="https://duckduckgo.com/?q=isbn+0735615799&amp;cppnetbooks" rel="nofollow">Network Programming for Microsoft Windows | ISBN: 0735615799</a> <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/558320</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/558320</guid><dc:creator><![CDATA[Hepi]]></dc:creator><pubDate>Sun, 11 Jul 2004 18:44:27 GMT</pubDate></item></channel></rss>