<?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 und Client]]></title><description><![CDATA[<p>Hallo Member!</p>
<p>Ich habe mir heute einen Server und den dazugehörigen Client zusammen gebastelt. Ein par Tutorials haben mir dabei geholfen, deswegen sind manche Benennungen auf Deutsch, andere in Englisch.</p>
<p>Ich habe aber ein kleines Problem: Ich möchte, dass der Server empfangen und senden kann (also Daten senden) und ich möchte, dass der Client das selbe kann. Aber ich bekomm das einfach nicht hin! es wäre nett, wenn mir da jemand von euch helfen kann!</p>
<p>Vielen Dank im Vorraus</p>
<p>Hier mal der Code vom Server:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;winsock.h&gt;
#include &lt;stdio.h&gt;

#define NETWORK_ERROR -1
#define NETWORK_OK     0

void ReportError(int, const char*);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
{

  WORD sockVersion;
  WSADATA wsaData;
  int nret;

  sockVersion = MAKEWORD(1, 1);

  WSAStartup(sockVersion, &amp;wsaData);
  SOCKET listeningSocket;

  listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

  if(listeningSocket == INVALID_SOCKET)
  {
  nret = WSAGetLastError();
  ReportError(nret, &quot;socket()&quot;);

  WSACleanup();
  return NETWORK_ERROR;
  }

  SOCKADDR_IN serverInfo;
  serverInfo.sin_family = AF_INET;
  serverInfo.sin_addr.s_addr = INADDR_ANY;
  serverInfo.sin_port = htons(8888);

  nret = bind(listeningSocket, (LPSOCKADDR)&amp;serverInfo, sizeof(struct sockaddr));

  if(nret == SOCKET_ERROR) 
  {
  nret = WSAGetLastError();
  ReportError(nret, &quot;bind()&quot;);

  WSACleanup();
  return NETWORK_ERROR;
  }

  nret = listen(listeningSocket, 10);

  if (nret == SOCKET_ERROR)
  {

  nret = WSAGetLastError();
  ReportError(nret, &quot;listen()&quot;);

  WSACleanup();
  return NETWORK_ERROR;
  }

         SOCKET theClient;

  theClient = accept(listeningSocket, NULL, NULL);

  if (theClient == INVALID_SOCKET)
  {

  nret = WSAGetLastError();
  ReportError(nret, &quot;accept()&quot;);
  WSACleanup();
  return NETWORK_ERROR;
  }

  else
 {
 printf(&quot;Connection accepted\n\n&quot;);
 int willkommen();
 system(&quot;PAUSE&quot;);
 }

  closesocket(theClient);
  closesocket(listeningSocket);

  WSACleanup();
  return NETWORK_OK;
  }

  int willkommen(int s)
  {

  int bytes;
  char buffer[] = &quot;Willkommen auf dem Server!\r\n&quot;;
  bytes = send(s, buffer, strlen(buffer), 0);
  if (bytes == 1)
    {
      printf(&quot;send() failed!&quot;);
      return -1;
      }

  }  

  void ReportError(int errorCode, const char *whichFunc)
  {
  char errorMsg[92];
  ZeroMemory(errorMsg, 92);

  sprintf(errorMsg, &quot;Call to %s returned error %d&quot;, (char *)whichFunc, errorCode);
  MessageBox(NULL, errorMsg, &quot;socketIndication&quot;, MB_OK);
  }
</code></pre>
<p>Und hier mal der Client:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;winsock.h&gt;
#include &lt;stdio.h&gt;

#define NETWORK_ERROR -1
#define NETWORK_OK     0
#define BUFFER_SIZE 1024

void ReportError(int, const char *);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
{

WORD sockVersion;
WSADATA wsaData;
int nret;

sockVersion = MAKEWORD(1, 1);
WSAStartup(sockVersion, &amp;wsaData);

LPHOSTENT hostEntry;
in_addr iaHost;
iaHost.s_addr = inet_addr(&quot;127.0.0.1&quot;);
hostEntry = gethostbyaddr((const char *)&amp;iaHost, sizeof(struct in_addr), AF_INET);

if(!hostEntry)
{

nret = WSAGetLastError();
ReportError(nret, &quot;gethostbyaddr()&quot;);

WSACleanup();
return NETWORK_ERROR;

}

SOCKET theSocket;
theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if(theSocket == INVALID_SOCKET)
{

nret = WSAGetLastError();
ReportError(nret, &quot;socket()&quot;);
WSACleanup();
return NETWORK_ERROR;
}

SOCKADDR_IN serverInfo;

serverInfo.sin_family = AF_INET;

serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry-&gt;h_addr_list);
serverInfo.sin_port = htons(8888);

nret = connect (theSocket, (LPSOCKADDR)&amp;serverInfo, sizeof(struct sockaddr));

if (nret == SOCKET_ERROR)
{
nret = WSAGetLastError();
ReportError(nret, &quot;connect()&quot;);

WSACleanup();
return NETWORK_ERROR;
}

else
 {
 printf(&quot;Successfully connected.\n\n&quot;);
 int empfangen();

 }

closesocket(theSocket);
WSACleanup();
return NETWORK_OK;
}

int empfangen(int s)
{

  char buffer[BUFFER_SIZE];
  int bytes;

  bytes = recv(s, buffer, sizeof(buffer) -1, 0);

  if (bytes == 1)
    {

      printf(&quot;recv() failed!&quot;);
      return -1;

buffer[bytes] = '\0';
printf(&quot;Server: %s&quot;, buffer);

}

return 0;

}    

void ReportError(int errorCode, const char *whichFunc)
{
char errorMsg[92];

ZeroMemory(errorMsg, 92);

sprintf(errorMsg, &quot;Call to %s returned error %d!&quot;, (char *)whichFunc, errorCode);

MessageBox(NULL, errorMsg, &quot;socketIndication&quot;, MB_OK);

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/75027/problem-mit-server-und-client</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 01:08:00 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/75027.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 26 May 2004 17:46:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit Server und Client on Wed, 26 May 2004 17:46:11 GMT]]></title><description><![CDATA[<p>Hallo Member!</p>
<p>Ich habe mir heute einen Server und den dazugehörigen Client zusammen gebastelt. Ein par Tutorials haben mir dabei geholfen, deswegen sind manche Benennungen auf Deutsch, andere in Englisch.</p>
<p>Ich habe aber ein kleines Problem: Ich möchte, dass der Server empfangen und senden kann (also Daten senden) und ich möchte, dass der Client das selbe kann. Aber ich bekomm das einfach nicht hin! es wäre nett, wenn mir da jemand von euch helfen kann!</p>
<p>Vielen Dank im Vorraus</p>
<p>Hier mal der Code vom Server:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;winsock.h&gt;
#include &lt;stdio.h&gt;

#define NETWORK_ERROR -1
#define NETWORK_OK     0

void ReportError(int, const char*);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
{

  WORD sockVersion;
  WSADATA wsaData;
  int nret;

  sockVersion = MAKEWORD(1, 1);

  WSAStartup(sockVersion, &amp;wsaData);
  SOCKET listeningSocket;

  listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

  if(listeningSocket == INVALID_SOCKET)
  {
  nret = WSAGetLastError();
  ReportError(nret, &quot;socket()&quot;);

  WSACleanup();
  return NETWORK_ERROR;
  }

  SOCKADDR_IN serverInfo;
  serverInfo.sin_family = AF_INET;
  serverInfo.sin_addr.s_addr = INADDR_ANY;
  serverInfo.sin_port = htons(8888);

  nret = bind(listeningSocket, (LPSOCKADDR)&amp;serverInfo, sizeof(struct sockaddr));

  if(nret == SOCKET_ERROR) 
  {
  nret = WSAGetLastError();
  ReportError(nret, &quot;bind()&quot;);

  WSACleanup();
  return NETWORK_ERROR;
  }

  nret = listen(listeningSocket, 10);

  if (nret == SOCKET_ERROR)
  {

  nret = WSAGetLastError();
  ReportError(nret, &quot;listen()&quot;);

  WSACleanup();
  return NETWORK_ERROR;
  }

         SOCKET theClient;

  theClient = accept(listeningSocket, NULL, NULL);

  if (theClient == INVALID_SOCKET)
  {

  nret = WSAGetLastError();
  ReportError(nret, &quot;accept()&quot;);
  WSACleanup();
  return NETWORK_ERROR;
  }

  else
 {
 printf(&quot;Connection accepted\n\n&quot;);
 int willkommen();
 system(&quot;PAUSE&quot;);
 }

  closesocket(theClient);
  closesocket(listeningSocket);

  WSACleanup();
  return NETWORK_OK;
  }

  int willkommen(int s)
  {

  int bytes;
  char buffer[] = &quot;Willkommen auf dem Server!\r\n&quot;;
  bytes = send(s, buffer, strlen(buffer), 0);
  if (bytes == 1)
    {
      printf(&quot;send() failed!&quot;);
      return -1;
      }

  }  

  void ReportError(int errorCode, const char *whichFunc)
  {
  char errorMsg[92];
  ZeroMemory(errorMsg, 92);

  sprintf(errorMsg, &quot;Call to %s returned error %d&quot;, (char *)whichFunc, errorCode);
  MessageBox(NULL, errorMsg, &quot;socketIndication&quot;, MB_OK);
  }
</code></pre>
<p>Und hier mal der Client:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;winsock.h&gt;
#include &lt;stdio.h&gt;

#define NETWORK_ERROR -1
#define NETWORK_OK     0
#define BUFFER_SIZE 1024

void ReportError(int, const char *);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
{

WORD sockVersion;
WSADATA wsaData;
int nret;

sockVersion = MAKEWORD(1, 1);
WSAStartup(sockVersion, &amp;wsaData);

LPHOSTENT hostEntry;
in_addr iaHost;
iaHost.s_addr = inet_addr(&quot;127.0.0.1&quot;);
hostEntry = gethostbyaddr((const char *)&amp;iaHost, sizeof(struct in_addr), AF_INET);

if(!hostEntry)
{

nret = WSAGetLastError();
ReportError(nret, &quot;gethostbyaddr()&quot;);

WSACleanup();
return NETWORK_ERROR;

}

SOCKET theSocket;
theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if(theSocket == INVALID_SOCKET)
{

nret = WSAGetLastError();
ReportError(nret, &quot;socket()&quot;);
WSACleanup();
return NETWORK_ERROR;
}

SOCKADDR_IN serverInfo;

serverInfo.sin_family = AF_INET;

serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry-&gt;h_addr_list);
serverInfo.sin_port = htons(8888);

nret = connect (theSocket, (LPSOCKADDR)&amp;serverInfo, sizeof(struct sockaddr));

if (nret == SOCKET_ERROR)
{
nret = WSAGetLastError();
ReportError(nret, &quot;connect()&quot;);

WSACleanup();
return NETWORK_ERROR;
}

else
 {
 printf(&quot;Successfully connected.\n\n&quot;);
 int empfangen();

 }

closesocket(theSocket);
WSACleanup();
return NETWORK_OK;
}

int empfangen(int s)
{

  char buffer[BUFFER_SIZE];
  int bytes;

  bytes = recv(s, buffer, sizeof(buffer) -1, 0);

  if (bytes == 1)
    {

      printf(&quot;recv() failed!&quot;);
      return -1;

buffer[bytes] = '\0';
printf(&quot;Server: %s&quot;, buffer);

}

return 0;

}    

void ReportError(int errorCode, const char *whichFunc)
{
char errorMsg[92];

ZeroMemory(errorMsg, 92);

sprintf(errorMsg, &quot;Call to %s returned error %d!&quot;, (char *)whichFunc, errorCode);

MessageBox(NULL, errorMsg, &quot;socketIndication&quot;, MB_OK);

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/527859</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/527859</guid><dc:creator><![CDATA[cf.ru]]></dc:creator><pubDate>Wed, 26 May 2004 17:46:11 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server und Client on Wed, 26 May 2004 17:57:40 GMT]]></title><description><![CDATA[<p>Ich würde dir raten ein Tutorial über die _Grundlagen_ zu lesen. Und erst wenn du die Grundlagen beherrscht, dich mit Socketprogrammierung zu beschäftigen.</p>
<p>Und bedenke: Copy&amp;Paste bringt dich nicht weiter</p>
]]></description><link>https://www.c-plusplus.net/forum/post/527869</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/527869</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Wed, 26 May 2004 17:57:40 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server und Client on Wed, 26 May 2004 18:04:59 GMT]]></title><description><![CDATA[<p>also ich habe mir das ganze nicht einfach rauskopiert, sondern ich ahbe mir die tutorials angeschaut und das teil dann geschrieben - und nicht copy and paste verwendet.<br />
und..<br />
1. ich habe mir ein par tutorials zum thema angeschaut und kann bererits ein bisschen was<br />
und<br />
2. kein copy and paste.....</p>
<p>und<br />
3. bringt mich deine antwort auch nicht viel weiter.</p>
<p>Ich programmiere schon länger in C++ und weiß schon, dass mich copy und paste nicht weiterbringt, dass brauchst du einem erfahrenen php programmierer nicht mehr zu sagen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/527871</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/527871</guid><dc:creator><![CDATA[cf.ru]]></dc:creator><pubDate>Wed, 26 May 2004 18:04:59 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server und Client on Thu, 27 May 2004 07:36:14 GMT]]></title><description><![CDATA[<p>Abgesehen davon gehört das nach WinAPI</p>
]]></description><link>https://www.c-plusplus.net/forum/post/528057</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/528057</guid><dc:creator><![CDATA[Unix-Tom]]></dc:creator><pubDate>Thu, 27 May 2004 07:36:14 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server und Client on Thu, 27 May 2004 11:31:49 GMT]]></title><description><![CDATA[<p>sorry, hab ich verplant!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/528179</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/528179</guid><dc:creator><![CDATA[cf.ru]]></dc:creator><pubDate>Thu, 27 May 2004 11:31:49 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server und Client on Thu, 27 May 2004 12:49:50 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/profile.php?mode=viewprofile&amp;u=5724" rel="nofollow">flenders</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/viewforum.php?f=31" rel="nofollow">Webzeugs</a> in das Forum <a href="http://www.c-plusplus.net/forum/viewforum.php?f=4" rel="nofollow">WinAPI</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic.php?t=39405" 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/528250</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/528250</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Thu, 27 May 2004 12:49:50 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server und Client on Thu, 27 May 2004 12:58:45 GMT]]></title><description><![CDATA[<p>gutu tut's für deine sammlung<br />
<a href="http://www.c-worker.ch/" rel="nofollow">http://www.c-worker.ch/</a></p>
<p>funktionen ruft man ohne rückgabewerte auf.</p>
<p><a href="http://cf.ru" rel="nofollow">cf.ru</a> schrieb:</p>
<blockquote>
<pre><code class="language-cpp">willkommen();
</code></pre>
</blockquote>
<p>parameter ?</p>
<p><a href="http://cf.ru" rel="nofollow">cf.ru</a> schrieb:</p>
<blockquote>
<pre><code class="language-cpp">empfangen();
</code></pre>
</blockquote>
<p>parameter ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/528260</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/528260</guid><dc:creator><![CDATA[miller_m]]></dc:creator><pubDate>Thu, 27 May 2004 12:58:45 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server und Client on Thu, 27 May 2004 13:00:31 GMT]]></title><description><![CDATA[<p>Was fällt denn dann noch sofort bei betrachten der Funktion &quot;empfangen()&quot; auf?</p>
<pre><code class="language-cpp">int empfangen(int s) 
{ 
    char buffer[BUFFER_SIZE]; 
    int bytes; 
    bytes = recv(s, buffer, sizeof(buffer) -1, 0); 
    if (bytes == 1) 
    { 
        printf(&quot;recv() failed!&quot;); 
        return -1; 
        buffer[bytes] = '\0'; 
        printf(&quot;Server: %s&quot;, buffer); 
    } 
    return 0; 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/528263</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/528263</guid><dc:creator><![CDATA[Hepi]]></dc:creator><pubDate>Thu, 27 May 2004 13:00:31 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server und Client on Thu, 27 May 2004 13:11:49 GMT]]></title><description><![CDATA[<p>Hepi schrieb:</p>
<blockquote>
<p>Was fällt denn dann noch sofort bei betrachten der Funktion &quot;empfangen()&quot; auf?</p>
</blockquote>
<p>ich weis es, ich weis es *freuwiekleineskind* <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
<p><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/recv_2.asp" rel="nofollow">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/recv_2.asp</a></p>
<p>so siehst es schon besser aus</p>
<pre><code class="language-cpp">bytes = recv(s, buffer, sizeof(buffer), 0);
    if(bytes == 0)
    {
      printf(&quot;Server hat die Verbindung getrennt..\n&quot;);
      return -1; 
    }
    if(bytes == SOCKET_ERROR)
    {
      printf(&quot;Fehler: recv, fehler code: %d\n&quot;,WSAGetLastError());
      return -1; 
    }
    buffer[bytes]='\0';
    printf(&quot;\nServer antwortet: %s\n&quot;,buffer);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/528271</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/528271</guid><dc:creator><![CDATA[miller_m]]></dc:creator><pubDate>Thu, 27 May 2004 13:11:49 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server und Client on Thu, 27 May 2004 14:25:20 GMT]]></title><description><![CDATA[<p>ahhh, vielen dank für eure antworten! werde gleich mal versuchen, dass bei mir zum laufen zu bekommen!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/528352</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/528352</guid><dc:creator><![CDATA[cf.ru]]></dc:creator><pubDate>Thu, 27 May 2004 14:25:20 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server und Client on Sat, 29 May 2004 17:10:10 GMT]]></title><description><![CDATA[<p>geht doch nicht! habe es ausprobiert und es läuft nicht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/529476</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/529476</guid><dc:creator><![CDATA[cf_ru]]></dc:creator><pubDate>Sat, 29 May 2004 17:10:10 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server und Client on Mon, 31 May 2004 12:35:20 GMT]]></title><description><![CDATA[<p>was geht nicht ? wo hängt es ? rückgabewerte ? getlasterror ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/530232</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/530232</guid><dc:creator><![CDATA[miller_m]]></dc:creator><pubDate>Mon, 31 May 2004 12:35:20 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Server und Client on Mon, 31 May 2004 14:17:30 GMT]]></title><description><![CDATA[<p>gibt es überhaupt eine verbindung???</p>
<p>und gib mal in willkommen() als parameter den listeningSocket ein</p>
<p>und bei empfangen() als parameter theSocket</p>
]]></description><link>https://www.c-plusplus.net/forum/post/530314</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/530314</guid><dc:creator><![CDATA[gargamel]]></dc:creator><pubDate>Mon, 31 May 2004 14:17:30 GMT</pubDate></item></channel></rss>