<?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[3. pararmeter von recv angeben]]></title><description><![CDATA[<p>Hallo, ich habe (mit Hilfe <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> eine Server Client Anwendung geschrieben, wobei<br />
unterschiedliche &quot;Nachrichten&quot; verschickt werden können. Es gibt ein<br />
struct &quot;Nachricht&quot; der nur den NachrichtenTyp speichern kann und 2 weitere Nachríchten, die Nutzinformationen enthalten und von &quot;Nachricht&quot; erben.</p>
<p>Nun muß ich bei recv( , , ,) im 3. pararmeter angeben, wieviel Daten im meine nachricht übertragen werden sollen. Nun habe ich das problem, dass ich ja erst bestimmen kann wie groß die Nachricht tatsächlich ist, wenn ich sie geladen habe. was muß ich also dort eintragen?</p>
<p>Ich Poste mal den Server und den Client. Aber das Problem liegt zur Zeit nur beim Client, da nur er Daten vom Typ nachricht empfängt.</p>
<p>Server:</p>
<pre><code class="language-cpp">#include &lt;winsock2.h&gt;
#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;

struct Nachricht {					//Eine allgemeine nachricht, von der jede Andere erbt
	int MsgType;  
	Nachricht():MsgType(0){};
}

struct NachrichtTyp1: Nachricht 
{ 

   int x; 
   int y; 

  NachrichtTyp1(int x_, int y_)  { x = x_; y = y_;MsgType=1;}; 
  NachrichtTyp1()  {}; 
}; 

struct NachrichtTyp2: Nachricht 
{ 

  float x; 
  float y; 
  float z;

  NachrichtTyp2(float x_, float y_,float z_)  { x = x_; y = y_;z=z_;MsgType=2;}; 
   NachrichtTyp2()  {}; 
}; 

//////////////SERVER/////////////////
int main()
{

  long rc;
  SOCKET acceptSocket;
  SOCKET connectedSocket;
  SOCKADDR_IN addr;
  char buf[256];

  // Winsock starten
  rc=startWinsock();

  // Socket erstellen
  acceptSocket=socket(AF_INET,SOCK_STREAM,0);

  // Socket binden
  memset(&amp;addr,0,sizeof(SOCKADDR_IN));
  addr.sin_family=AF_INET;
  addr.sin_port=htons(12345);
  addr.sin_addr.s_addr=ADDR_ANY;
  rc=bind(acceptSocket,(SOCKADDR*)&amp;addr,sizeof(SOCKADDR_IN));

  // In den listen Modus
  rc=listen(acceptSocket,10);

  // Verbindung annehmen
  connectedSocket=accept(acceptSocket,NULL,NULL);

  // Daten austauschen
  while(rc!=SOCKET_ERROR)
  {

	// Diese 3 Zeilen sind eigentlich unnötig und geben nur das gesenete vom Client aus
    rc=recv(connectedSocket,buf,256,0);
    buf[rc]='\0';
    printf(&quot;Client sendet: %s\n&quot;,buf);

	NachrichtTyp2 *i = new NachrichtTyp2(8,2,5);
	rc=send(connectedSocket,(const char*)i, sizeof(NachrichtTyp2),0); 
	delete (i);

  }
  closesocket(acceptSocket);
  closesocket(connectedSocket);
  WSACleanup();
  return 0;
}

int startWinsock(void)
{
  WSADATA wsa;
  return WSAStartup(MAKEWORD(2,0),&amp;wsa);
}
</code></pre>
<p>Client:</p>
<pre><code class="language-cpp">#include &lt;winsock2.h&gt;
#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;

//Prototypen
int startWinsock(void);

struct Nachricht {					//Eine allgemeine nachricht, von der jede Andere erbt
	int MsgType;  
	Nachricht():MsgType(0){};
};

struct NachrichtTyp1: Nachricht		//Nachricht Typ 1, erbet von Nachricht
{ 

   int x; 
   int y; 

   NachrichtTyp1(int x_, int y_)  { x = x_; y = y_;MsgType=1;}; 
   NachrichtTyp1()  {}; 
}; 

struct NachrichtTyp2: Nachricht		//Nachricht Typ 2, erbet von Nachricht
{ 

  float x; 
  float y; 
  float z;

   NachrichtTyp2(float x_, float y_,float z_)  { x = x_; y = y_;z = z_ ;MsgType=2;}; 
   NachrichtTyp2()  {}; 
}; 

////////////Client

int main()
{
  long rc;
  SOCKET s;
  SOCKADDR_IN addr;
  char buf[256];

  // Winsock starten
  rc=startWinsock();

  // Socket erstellen
  s=socket(AF_INET,SOCK_STREAM,0);

  // Verbinden
  memset(&amp;addr,0,sizeof(SOCKADDR_IN)); // zuerst alles auf 0 setzten
  addr.sin_family=AF_INET;
  addr.sin_port=htons(12345); // wir verwenden mal port 12345
  addr.sin_addr.s_addr=inet_addr(&quot;127.0.0.1&quot;); // zielrechner ist unser eigener

  rc=connect(s,(SOCKADDR*)&amp;addr,sizeof(SOCKADDR));

  // Daten austauschen
  while(rc!=SOCKET_ERROR)
  {

    gets(buf);  // nur um auf eine Eingabe zu warten...
    send(s,buf,strlen(buf),0); //Server sendet eine Nachricht vom Typ NachrichtTyp1 oder NachrichtTyp2 wenn er irgentetwas empängt.

	Nachricht*i = new Nachricht(); 
	int Was_muss_hier_stehen=100000;				///Welchen Wert muß ich als 3. Pararmeter angeben?
	recv (s,( char*)i,Was_muss_hier_stehen,0);

	if (i-&gt;MsgType==1) {
	NachrichtTyp1 *info=(NachrichtTyp1*)i;
	printf( &quot;%d\n&quot;, info-&gt;x ); 
    printf( &quot;%d\n&quot;, info-&gt;y );
	printf( &quot;%d\n&quot;, info-&gt;MsgType);
	}

	if (i-&gt;MsgType==2) {
	NachrichtTyp2 *info=(NachrichtTyp2*)i;
	printf( &quot;%f\n&quot;, info-&gt;x ); 
    printf( &quot;%f\n&quot;, info-&gt;y );
	printf( &quot;%f\n&quot;, info-&gt;z );
	printf( &quot;%d\n&quot;, info-&gt;MsgType);
	}

  }

  closesocket(s);
  WSACleanup();
  return 0;
}

int startWinsock(void)
{
  WSADATA wsa;
  return WSAStartup(MAKEWORD(2,0),&amp;wsa);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/140252/3-pararmeter-von-recv-angeben</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Jul 2026 03:48:25 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/140252.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 13 Mar 2006 14:29:32 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to 3. pararmeter von recv angeben on Mon, 13 Mar 2006 14:29:32 GMT]]></title><description><![CDATA[<p>Hallo, ich habe (mit Hilfe <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> eine Server Client Anwendung geschrieben, wobei<br />
unterschiedliche &quot;Nachrichten&quot; verschickt werden können. Es gibt ein<br />
struct &quot;Nachricht&quot; der nur den NachrichtenTyp speichern kann und 2 weitere Nachríchten, die Nutzinformationen enthalten und von &quot;Nachricht&quot; erben.</p>
<p>Nun muß ich bei recv( , , ,) im 3. pararmeter angeben, wieviel Daten im meine nachricht übertragen werden sollen. Nun habe ich das problem, dass ich ja erst bestimmen kann wie groß die Nachricht tatsächlich ist, wenn ich sie geladen habe. was muß ich also dort eintragen?</p>
<p>Ich Poste mal den Server und den Client. Aber das Problem liegt zur Zeit nur beim Client, da nur er Daten vom Typ nachricht empfängt.</p>
<p>Server:</p>
<pre><code class="language-cpp">#include &lt;winsock2.h&gt;
#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;

struct Nachricht {					//Eine allgemeine nachricht, von der jede Andere erbt
	int MsgType;  
	Nachricht():MsgType(0){};
}

struct NachrichtTyp1: Nachricht 
{ 

   int x; 
   int y; 

  NachrichtTyp1(int x_, int y_)  { x = x_; y = y_;MsgType=1;}; 
  NachrichtTyp1()  {}; 
}; 

struct NachrichtTyp2: Nachricht 
{ 

  float x; 
  float y; 
  float z;

  NachrichtTyp2(float x_, float y_,float z_)  { x = x_; y = y_;z=z_;MsgType=2;}; 
   NachrichtTyp2()  {}; 
}; 

//////////////SERVER/////////////////
int main()
{

  long rc;
  SOCKET acceptSocket;
  SOCKET connectedSocket;
  SOCKADDR_IN addr;
  char buf[256];

  // Winsock starten
  rc=startWinsock();

  // Socket erstellen
  acceptSocket=socket(AF_INET,SOCK_STREAM,0);

  // Socket binden
  memset(&amp;addr,0,sizeof(SOCKADDR_IN));
  addr.sin_family=AF_INET;
  addr.sin_port=htons(12345);
  addr.sin_addr.s_addr=ADDR_ANY;
  rc=bind(acceptSocket,(SOCKADDR*)&amp;addr,sizeof(SOCKADDR_IN));

  // In den listen Modus
  rc=listen(acceptSocket,10);

  // Verbindung annehmen
  connectedSocket=accept(acceptSocket,NULL,NULL);

  // Daten austauschen
  while(rc!=SOCKET_ERROR)
  {

	// Diese 3 Zeilen sind eigentlich unnötig und geben nur das gesenete vom Client aus
    rc=recv(connectedSocket,buf,256,0);
    buf[rc]='\0';
    printf(&quot;Client sendet: %s\n&quot;,buf);

	NachrichtTyp2 *i = new NachrichtTyp2(8,2,5);
	rc=send(connectedSocket,(const char*)i, sizeof(NachrichtTyp2),0); 
	delete (i);

  }
  closesocket(acceptSocket);
  closesocket(connectedSocket);
  WSACleanup();
  return 0;
}

int startWinsock(void)
{
  WSADATA wsa;
  return WSAStartup(MAKEWORD(2,0),&amp;wsa);
}
</code></pre>
<p>Client:</p>
<pre><code class="language-cpp">#include &lt;winsock2.h&gt;
#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;

//Prototypen
int startWinsock(void);

struct Nachricht {					//Eine allgemeine nachricht, von der jede Andere erbt
	int MsgType;  
	Nachricht():MsgType(0){};
};

struct NachrichtTyp1: Nachricht		//Nachricht Typ 1, erbet von Nachricht
{ 

   int x; 
   int y; 

   NachrichtTyp1(int x_, int y_)  { x = x_; y = y_;MsgType=1;}; 
   NachrichtTyp1()  {}; 
}; 

struct NachrichtTyp2: Nachricht		//Nachricht Typ 2, erbet von Nachricht
{ 

  float x; 
  float y; 
  float z;

   NachrichtTyp2(float x_, float y_,float z_)  { x = x_; y = y_;z = z_ ;MsgType=2;}; 
   NachrichtTyp2()  {}; 
}; 

////////////Client

int main()
{
  long rc;
  SOCKET s;
  SOCKADDR_IN addr;
  char buf[256];

  // Winsock starten
  rc=startWinsock();

  // Socket erstellen
  s=socket(AF_INET,SOCK_STREAM,0);

  // Verbinden
  memset(&amp;addr,0,sizeof(SOCKADDR_IN)); // zuerst alles auf 0 setzten
  addr.sin_family=AF_INET;
  addr.sin_port=htons(12345); // wir verwenden mal port 12345
  addr.sin_addr.s_addr=inet_addr(&quot;127.0.0.1&quot;); // zielrechner ist unser eigener

  rc=connect(s,(SOCKADDR*)&amp;addr,sizeof(SOCKADDR));

  // Daten austauschen
  while(rc!=SOCKET_ERROR)
  {

    gets(buf);  // nur um auf eine Eingabe zu warten...
    send(s,buf,strlen(buf),0); //Server sendet eine Nachricht vom Typ NachrichtTyp1 oder NachrichtTyp2 wenn er irgentetwas empängt.

	Nachricht*i = new Nachricht(); 
	int Was_muss_hier_stehen=100000;				///Welchen Wert muß ich als 3. Pararmeter angeben?
	recv (s,( char*)i,Was_muss_hier_stehen,0);

	if (i-&gt;MsgType==1) {
	NachrichtTyp1 *info=(NachrichtTyp1*)i;
	printf( &quot;%d\n&quot;, info-&gt;x ); 
    printf( &quot;%d\n&quot;, info-&gt;y );
	printf( &quot;%d\n&quot;, info-&gt;MsgType);
	}

	if (i-&gt;MsgType==2) {
	NachrichtTyp2 *info=(NachrichtTyp2*)i;
	printf( &quot;%f\n&quot;, info-&gt;x ); 
    printf( &quot;%f\n&quot;, info-&gt;y );
	printf( &quot;%f\n&quot;, info-&gt;z );
	printf( &quot;%d\n&quot;, info-&gt;MsgType);
	}

  }

  closesocket(s);
  WSACleanup();
  return 0;
}

int startWinsock(void)
{
  WSADATA wsa;
  return WSAStartup(MAKEWORD(2,0),&amp;wsa);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1015381</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1015381</guid><dc:creator><![CDATA[Andreas XXL]]></dc:creator><pubDate>Mon, 13 Mar 2006 14:29:32 GMT</pubDate></item><item><title><![CDATA[Reply to 3. pararmeter von recv angeben on Mon, 13 Mar 2006 14:36:08 GMT]]></title><description><![CDATA[<p>Andreas XXL schrieb:</p>
<blockquote>
<p>Nun habe ich das problem, dass ich ja erst bestimmen kann wie groß die Nachricht tatsächlich ist, wenn ich sie geladen habe.</p>
</blockquote>
<p>Dann hast Du Dein Protokoll falsch definiert.<br />
Es gibt i.d.R. zwei verschiedene Ansätze:<br />
1. Du schickst zuerst mit, wie lange die folgenden Daten sind (also z.B. 4-byte, welche die Länge der nächsten Daten angibt)<br />
2. Du definierst ein eindeutiges Ende-Zeichen (z.B. CR); das setzt natürlich voraus, dass dieses Zeichen sonst niergens auftreten darf.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1015391</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1015391</guid><dc:creator><![CDATA[Jochen Kalmbach]]></dc:creator><pubDate>Mon, 13 Mar 2006 14:36:08 GMT</pubDate></item><item><title><![CDATA[Reply to 3. pararmeter von recv angeben on Mon, 13 Mar 2006 14:56:23 GMT]]></title><description><![CDATA[<p>du musst auch den rückgabewert von recv überprüfen. du kannst nämlich auch nur 1 byte bekommen auch wenn du dort 12 angibst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1015409</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1015409</guid><dc:creator><![CDATA[ß]]></dc:creator><pubDate>Mon, 13 Mar 2006 14:56:23 GMT</pubDate></item><item><title><![CDATA[Reply to 3. pararmeter von recv angeben on Mon, 13 Mar 2006 15:58:58 GMT]]></title><description><![CDATA[<p>ß schrieb:</p>
<blockquote>
<p>du musst auch den rückgabewert von recv überprüfen. du kannst nämlich auch nur 1 byte bekommen auch wenn du dort 12 angibst.</p>
</blockquote>
<p>Ok. Der Rückabewert gibt doch an, wie viele Daten übertragen wurden.<br />
Aber was mache ich wenn es zu wenig sind? Dann sind doch zu wenig Daten in Mein &quot;i&quot; geladen worden. Wie kann ich denn dann die folgenden Daten in das Objekt i hinzufügen, so das nicht die die schon drin sind überschrieben werden?<br />
kann man irgendwie abfragen, wie viele daten im Empfangspuffer sind ohne diese dort rauszunehmen? Weil ich ja erst recv aufrufen möchte, wenn soviele Daten da sind, wie mein Objekt das ich &quot;befüllen&quot; möchte groß ist! Oder wie macht man das dann?</p>
<p>recv (s,( char*)i,100,0);</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1015481</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1015481</guid><dc:creator><![CDATA[Andreas XXL]]></dc:creator><pubDate>Mon, 13 Mar 2006 15:58:58 GMT</pubDate></item><item><title><![CDATA[Reply to 3. pararmeter von recv angeben on Mon, 13 Mar 2006 16:14:47 GMT]]></title><description><![CDATA[<p>Benutz halt ne Schleife bis du alles zusammenhast.</p>
<p>Bei recv kannst du dann als Puffer i + bytesReceived und als Länge Strukturgroesse - bytesReceived angeben, dann wird auch nichts überschrieben.</p>
<p>Warum legst du das Nachricht-Objekt überhaupt auf dem Heap an? Sieht ziemlich dämlich aus wenn du es danach sofort wieder freigibst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1015494</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1015494</guid><dc:creator><![CDATA[ß]]></dc:creator><pubDate>Mon, 13 Mar 2006 16:14:47 GMT</pubDate></item><item><title><![CDATA[Reply to 3. pararmeter von recv angeben on Mon, 13 Mar 2006 17:15:40 GMT]]></title><description><![CDATA[<p>Danke für die Hinweise!</p>
<p>Ich habe jetzt die Nchrichten und das empfangen modifiziert und glaube, dass es jetzt so funktionieren müsste:</p>
<pre><code class="language-cpp">#include &lt;winsock2.h&gt;
#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;

//Prototypen
int startWinsock(void);

struct Nachricht {//Eine allgemeine nachricht, von der jede Andere erbt
	int groesse;
	int MsgType;  
	Nachricht():MsgType(0){groesse=sizeof(Nachricht);};
};

struct NachrichtTyp1: Nachricht		//Nachricht Typ 1
{ 

   int x; 
   int y; 

   NachrichtTyp1(int x_, int y_)  { x = x_; y = y_;MsgType=1;groesse=sizeof(NachrichtTyp1);}; 
   NachrichtTyp1()  {}; 
}; 

struct NachrichtTyp2: Nachricht		//Nachricht Typ 2 erbet von Nachricht
{ 

  float x; 
  float y; 
  float z;

   NachrichtTyp2(float x_, float y_,float z_)  { x = x_; y = y_;z = z_ ;MsgType=2;groesse=sizeof(NachrichtTyp2);}; 
   NachrichtTyp2()  {}; 
}; 

////////////Client

int main()
{
  long rc;
  SOCKET s;
  SOCKADDR_IN addr;
  char buf[256];

  // Winsock starten
  rc=startWinsock();
  if(rc!=0)
  {
    printf(&quot;Fehler: startWinsock, fehler code: %d\n&quot;,rc);
    return 1;
  }
  else
  {
    printf(&quot;Winsock gestartet!\n&quot;);
  }

  // Socket erstellen
  s=socket(AF_INET,SOCK_STREAM,0);
  if(s==INVALID_SOCKET)
  {
    printf(&quot;Fehler: Der Socket konnte nicht erstellt werden, fehler code: %d\n&quot;,WSAGetLastError());
    return 1;
  }
  else
  {
    printf(&quot;Socket erstellt!\n&quot;);
  }

  // Verbinden
  memset(&amp;addr,0,sizeof(SOCKADDR_IN)); // zuerst alles auf 0 setzten
  addr.sin_family=AF_INET;
  addr.sin_port=htons(12345); // wir verwenden mal port 12345
  addr.sin_addr.s_addr=inet_addr(&quot;127.0.0.1&quot;); // zielrechner ist unser eigener

  rc=connect(s,(SOCKADDR*)&amp;addr,sizeof(SOCKADDR));
  if(rc==SOCKET_ERROR)
  {
    printf(&quot;Fehler: connect gescheitert, fehler code: %d\n&quot;,WSAGetLastError());
    return 1;
  }
  else
  {
    printf(&quot;Verbunden mit 127.0.0.1..\n&quot;);
  }

  // Daten austauschen
  while(rc!=SOCKET_ERROR)
  {

    gets(buf);  // nur um auf eine Eingabe zu warten...
    send(s,buf,strlen(buf),0);
	Nachricht*i = new Nachricht(); 

/// Zuerst die ersten 4 Byte lesen, somit habe ich jetzt die groesse (steht dann in i unter i-&gt;groesse)
int wert=0;
while (wert&lt;4) 
{
		wert=wert+recv (s,( char*)i+wert,4-wert,0);

}
wert=0;

//Die restlichen Werte lesen (sind jetzt groesse-4)
while (wert&lt;i-&gt;groesse-4) 
{
		wert=wert+recv (s,( char*)i+wert+4,i-&gt;groesse-(wert+4),0);

}

	if (i-&gt;MsgType==1) {
	NachrichtTyp1 *info=(NachrichtTyp1*)i;
	printf( &quot;%d\n&quot;, info-&gt;x ); 
    printf( &quot;%d\n&quot;, info-&gt;y );
	printf( &quot;%d\n&quot;, info-&gt;MsgType);
	}

	if (i-&gt;MsgType==2) {
	NachrichtTyp2 *info=(NachrichtTyp2*)i;
	printf( &quot;%d\n&quot;, info-&gt;groesse);
	printf( &quot;%f\n&quot;, info-&gt;x ); 
    printf( &quot;%f\n&quot;, info-&gt;y );
	printf( &quot;%f\n&quot;, info-&gt;z );
	printf( &quot;%d\n&quot;, info-&gt;MsgType);
	}
  }

  closesocket(s);
  WSACleanup();
  return 0;
}

int startWinsock(void)
{
  WSADATA wsa;
  return WSAStartup(MAKEWORD(2,0),&amp;wsa);
}
</code></pre>
<p>Sind da jetzt noch Fehler vorhanden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1015554</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1015554</guid><dc:creator><![CDATA[Andreas XXL]]></dc:creator><pubDate>Mon, 13 Mar 2006 17:15:40 GMT</pubDate></item><item><title><![CDATA[Reply to 3. pararmeter von recv angeben on Mon, 13 Mar 2006 17:49:34 GMT]]></title><description><![CDATA[<p>Vehlerbehandlung wäre nicht schlecht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1015581</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1015581</guid><dc:creator><![CDATA[ß]]></dc:creator><pubDate>Mon, 13 Mar 2006 17:49:34 GMT</pubDate></item></channel></rss>