<?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[Socketprogrammierung]]></title><description><![CDATA[<p>Ich habe ein kleines Prgramm nach einem tutorial verfasst, welches auf dem socketprinzip Daten zwischen Server und client verschickt.<br />
läuft alles ganz gut mit einer Ausnahme, an der ich seit tagen schwitze. Nach dem 1. Schleifendurchlauf der do-while schleife vergleicht er nicht mehr die Städte sondern durchläuft diese Schleife immer wieder &quot;blind&quot;. Außerdem häält der server nur bei jedem 2. mal bei der funktion read und wartet auf den client. hat jemand ne idee, wie ich das beheben kann?</p>
<p>Server</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt; 
#include &lt;sys/socket.h&gt; //needed for socket
#include &lt;netinet/in.h&gt; //for internet domain address
#include &lt;iostream&gt;

int StringCompare (const char *s1, const char *s2);

void error(const char *msg)	//function called, when error occour
{
    perror(msg);	
    exit(1);
}
using namespace std;
int main(int argc, char *argv[])
{

     int sockfd, newsockfd;   //file descriptors (store values returned by socket sytem call and accept system call
     int portno, ret;   //portno = portnumber on which server accepts connections ; ret = stores value of StringCompare 
     socklen_t clilen;
     char buffer[256];	//server reads characters from socket connection into the buffer
     char leipzig[256] = &quot;leipzig&quot;;     
     struct sockaddr_in serv_addr, cli_addr; // sockaddr_in = struct containing internet adress. defined in netinet/in.h
     int n, nausg;
   //  string switchv;

     if (argc &lt; 2) {	// displays an error, when user don't pass the port number
         fprintf(stderr,&quot;ERROR, no port provided\n&quot;);
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);	//creates new socket (AF_INET = (unix command) internet domain; SOCK_STREAM = stream socket (characters are read in continuous stream; protocol (in case of stream socket we got a TCP protocol
     if (sockfd &lt; 0) //check if we got a proper socket
        error(&quot;ERROR opening socket&quot;);
     bzero((char *) &amp;serv_addr, sizeof(serv_addr)); //sets all values in a buffer to zero (first argument is a pointer ti the buffer, the second is the buffersize. serv_addr is initialized to zeros
     portno = atoi(argv[1]); // function atoi converts the string of digits to an interger
     serv_addr.sin_family = AF_INET; //Adress family (internet domain)
     serv_addr.sin_addr.s_addr = INADDR_ANY; //INADDR_ANY = symbolic constant which gets the host ip adress
     serv_addr.sin_port = htons(portno); //htons convert portno (host byte order) to network byte order
     if (bind(sockfd, (struct sockaddr *) &amp;serv_addr, //binds the socket to adress (1.Argument: socketfiledescriptor 2.Argument: address to which is bound 3.Argument: size of address to which is bound
              sizeof(serv_addr)) &lt; 0) 
              error(&quot;ERROR on binding&quot;);

     listen(sockfd,5);  //system call to listen on the socket connetion (1.Argument: socketfiledescriptor 2.Argumen: size of backlog queue
     clilen = sizeof(cli_addr);

     newsockfd = accept(sockfd,(struct sockaddr *) &amp;cli_addr,&amp;clilen);  //accept block the process until a client connects, returns new filedescriptor for the communication (1Argument:socketfiledescriptor;2.Argument: reference pointer to the adress of the client 3.Argument: size of this structure)
     if (newsockfd &lt; 0) 
          error(&quot;ERROR on accept&quot;);
do {	
     bzero(buffer,256);
		cout &lt;&lt; &quot;Server: Buffer nach bzero :&quot; &lt;&lt; buffer &lt;&lt; endl;
     n = read(newsockfd,buffer,255);	//read block until there is something to read in the socket.
	cout &lt;&lt; &quot;Server: Buffer :&quot; &lt;&lt; buffer &lt;&lt; endl;
     if (n &lt; 0) error(&quot;ERROR reading from socket&quot;);
     // cout &lt;&lt; &quot;Here is the message: &quot; &lt;&lt; buffer &lt;&lt; endl;

		ret = StringCompare(buffer,&quot;Leipzig&quot;);     
		if (ret==true)
		{
			//fputs (&quot;Leipzig: 22 Grad&quot;,buffer);
			 n = write(newsockfd, &quot;Leipzig 22 Grad&quot;, sizeof(buffer));

		}
		ret = StringCompare(buffer,&quot;Stuttgart&quot;);     
		if (ret==true)
		{
			 n = write(newsockfd, &quot;Stuttgart sonnig, 23 Grad&quot;, sizeof(buffer));
			close(newsockfd);
		}
		ret = StringCompare(buffer,&quot;Hamburg&quot;);     
		if (ret==true)
		{
			 n = write(newsockfd, &quot;Hamburg: windig, 18 Grad&quot;, sizeof(buffer));

		}
		ret = StringCompare(buffer,&quot;Erlangen&quot;);     
		if (ret==true)
		{
			 n = write(newsockfd, &quot;Erlangen sonnig, 23 Grad&quot;, sizeof(buffer));

		}
		ret = StringCompare(buffer,&quot;Konstanz&quot;);
		if (ret==true)
		{
			 n = write(newsockfd, &quot;Konstanz neblig, 24 Grad&quot;, sizeof(buffer));

		}	
		if (n &lt; 0) error(&quot;ERROR reading from socket&quot;);

	else {
		n = write(newsockfd, &quot;ungueltige Eingabe: &quot;, 20 );
		//nausg = write(newsockfd, buffer, sizeof(buffer));
	     }
		cout &lt;&lt; &quot;Server: Buffer am ende: &quot; &lt;&lt; buffer &lt;&lt; endl;
		cout &lt;&lt; &quot;\n\n&quot;;

	}
	while ( true);
	//nausg = write(newsockfd, &quot;exit&quot;, sizeof(buffer));
     //n = write(newsockfd,&quot;ende vom spass&quot;,18);
     if (n &lt; 0) error(&quot;ERROR writing to socket&quot;);
     close(newsockfd);
     close(sockfd);
     return 0; 
}

int StringCompare (const char *s1, const char *s2)  //function to compare the character arrays
{
	int ret;
	if (strcmp(s1,s2)) ret = 0;
	else ret = 1;
	return ret;
}
</code></pre>
<p>Client</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;netdb.h&gt;	//define a structure hostent
#include &lt;iostream&gt;

using namespace std;

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;	//server is a pointer to a structure and will defines a host computer on the interet

    char buffer[256] ,exitv;
    if (argc &lt; 3) {
       fprintf(stderr,&quot;usage %s hostname port\n&quot;, argv[0]);
	// cout &gt;&gt; stderr 
       exit(0);
    }
    portno = atoi(argv[2]);  //server.cpp
    sockfd = socket(AF_INET, SOCK_STREAM, 0); //server.cpp
    if (sockfd &lt; 0) 
        error(&quot;ERROR opening socket&quot;);
    server = gethostbyname(argv[1]);  //takes hostname from argv and returns a pointer to a hostent containing inforamtion about that host
    if (server == NULL) {
        fprintf(stderr,&quot;ERROR, no such host\n&quot;);
        exit(0);
    }
    bzero((char *) &amp;serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server-&gt;h_addr, //copy byte by byte from source to destination because server-&gt;h_addr is a character String (arguments: source,destination,length)
         (char *)&amp;serv_addr.sin_addr.s_addr,
         server-&gt;h_length);
    serv_addr.sin_port = htons(portno); //server.cpp

    if (connect(sockfd,(struct sockaddr *) &amp;serv_addr,sizeof(serv_addr)) &lt; 0) //connect establish a connection
        error(&quot;ERROR connecting&quot;);

do {

    cout &lt;&lt; &quot;Buffer am Beginn : &quot; &lt;&lt; buffer &lt;&lt; endl; 
    cout &lt;&lt; &quot;Stadt eingeben: &quot;;
    bzero(buffer,256);
	     cout &lt;&lt; &quot;\nBuffer nach bzero : &quot; &lt;&lt; buffer &lt;&lt; endl; 
    cin &gt;&gt; buffer;
     cout &lt;&lt; &quot;Buffer nach einlesen : &quot; &lt;&lt; buffer &lt;&lt; endl; 
    //fgets(buffer,255,stdin);  fgets geht nich, weill Null-character \0 an arrayende gesetzt wird. 
    //cout &lt;&lt; &quot;Buffer:  |&quot; &lt;&lt; buffer &lt;&lt;&quot;|&quot; ; nur zum debuggen
    n = write(sockfd,buffer,sizeof(buffer));
    if (n &lt; 0) 
         error(&quot;ERROR writing to socket&quot;);
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n &lt; 0) 
         error(&quot;ERROR reading from socket&quot;);
    cout &lt;&lt; &quot;Buffer am Ende: &quot; &lt;&lt; buffer &lt;&lt; endl; 
	bzero(buffer,256);
   }

while  (true);
	close(sockfd);
    return 0;
}
</code></pre>
<p>wie ich das gerade verfolgt hab bricht der Server das ganze sogar mit der Meldung: ERROR reading from socket: Bad file descriptor<br />
ab.<br />
Brauche also wirklich dringend hilfe <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/297957/socketprogrammierung</link><generator>RSS for Node</generator><lastBuildDate>Fri, 14 Aug 2026 04:17:45 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/297957.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 10 Jan 2012 15:11:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Socketprogrammierung on Tue, 10 Jan 2012 15:32:48 GMT]]></title><description><![CDATA[<p>Ich habe ein kleines Prgramm nach einem tutorial verfasst, welches auf dem socketprinzip Daten zwischen Server und client verschickt.<br />
läuft alles ganz gut mit einer Ausnahme, an der ich seit tagen schwitze. Nach dem 1. Schleifendurchlauf der do-while schleife vergleicht er nicht mehr die Städte sondern durchläuft diese Schleife immer wieder &quot;blind&quot;. Außerdem häält der server nur bei jedem 2. mal bei der funktion read und wartet auf den client. hat jemand ne idee, wie ich das beheben kann?</p>
<p>Server</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt; 
#include &lt;sys/socket.h&gt; //needed for socket
#include &lt;netinet/in.h&gt; //for internet domain address
#include &lt;iostream&gt;

int StringCompare (const char *s1, const char *s2);

void error(const char *msg)	//function called, when error occour
{
    perror(msg);	
    exit(1);
}
using namespace std;
int main(int argc, char *argv[])
{

     int sockfd, newsockfd;   //file descriptors (store values returned by socket sytem call and accept system call
     int portno, ret;   //portno = portnumber on which server accepts connections ; ret = stores value of StringCompare 
     socklen_t clilen;
     char buffer[256];	//server reads characters from socket connection into the buffer
     char leipzig[256] = &quot;leipzig&quot;;     
     struct sockaddr_in serv_addr, cli_addr; // sockaddr_in = struct containing internet adress. defined in netinet/in.h
     int n, nausg;
   //  string switchv;

     if (argc &lt; 2) {	// displays an error, when user don't pass the port number
         fprintf(stderr,&quot;ERROR, no port provided\n&quot;);
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);	//creates new socket (AF_INET = (unix command) internet domain; SOCK_STREAM = stream socket (characters are read in continuous stream; protocol (in case of stream socket we got a TCP protocol
     if (sockfd &lt; 0) //check if we got a proper socket
        error(&quot;ERROR opening socket&quot;);
     bzero((char *) &amp;serv_addr, sizeof(serv_addr)); //sets all values in a buffer to zero (first argument is a pointer ti the buffer, the second is the buffersize. serv_addr is initialized to zeros
     portno = atoi(argv[1]); // function atoi converts the string of digits to an interger
     serv_addr.sin_family = AF_INET; //Adress family (internet domain)
     serv_addr.sin_addr.s_addr = INADDR_ANY; //INADDR_ANY = symbolic constant which gets the host ip adress
     serv_addr.sin_port = htons(portno); //htons convert portno (host byte order) to network byte order
     if (bind(sockfd, (struct sockaddr *) &amp;serv_addr, //binds the socket to adress (1.Argument: socketfiledescriptor 2.Argument: address to which is bound 3.Argument: size of address to which is bound
              sizeof(serv_addr)) &lt; 0) 
              error(&quot;ERROR on binding&quot;);

     listen(sockfd,5);  //system call to listen on the socket connetion (1.Argument: socketfiledescriptor 2.Argumen: size of backlog queue
     clilen = sizeof(cli_addr);

     newsockfd = accept(sockfd,(struct sockaddr *) &amp;cli_addr,&amp;clilen);  //accept block the process until a client connects, returns new filedescriptor for the communication (1Argument:socketfiledescriptor;2.Argument: reference pointer to the adress of the client 3.Argument: size of this structure)
     if (newsockfd &lt; 0) 
          error(&quot;ERROR on accept&quot;);
do {	
     bzero(buffer,256);
		cout &lt;&lt; &quot;Server: Buffer nach bzero :&quot; &lt;&lt; buffer &lt;&lt; endl;
     n = read(newsockfd,buffer,255);	//read block until there is something to read in the socket.
	cout &lt;&lt; &quot;Server: Buffer :&quot; &lt;&lt; buffer &lt;&lt; endl;
     if (n &lt; 0) error(&quot;ERROR reading from socket&quot;);
     // cout &lt;&lt; &quot;Here is the message: &quot; &lt;&lt; buffer &lt;&lt; endl;

		ret = StringCompare(buffer,&quot;Leipzig&quot;);     
		if (ret==true)
		{
			//fputs (&quot;Leipzig: 22 Grad&quot;,buffer);
			 n = write(newsockfd, &quot;Leipzig 22 Grad&quot;, sizeof(buffer));

		}
		ret = StringCompare(buffer,&quot;Stuttgart&quot;);     
		if (ret==true)
		{
			 n = write(newsockfd, &quot;Stuttgart sonnig, 23 Grad&quot;, sizeof(buffer));
			close(newsockfd);
		}
		ret = StringCompare(buffer,&quot;Hamburg&quot;);     
		if (ret==true)
		{
			 n = write(newsockfd, &quot;Hamburg: windig, 18 Grad&quot;, sizeof(buffer));

		}
		ret = StringCompare(buffer,&quot;Erlangen&quot;);     
		if (ret==true)
		{
			 n = write(newsockfd, &quot;Erlangen sonnig, 23 Grad&quot;, sizeof(buffer));

		}
		ret = StringCompare(buffer,&quot;Konstanz&quot;);
		if (ret==true)
		{
			 n = write(newsockfd, &quot;Konstanz neblig, 24 Grad&quot;, sizeof(buffer));

		}	
		if (n &lt; 0) error(&quot;ERROR reading from socket&quot;);

	else {
		n = write(newsockfd, &quot;ungueltige Eingabe: &quot;, 20 );
		//nausg = write(newsockfd, buffer, sizeof(buffer));
	     }
		cout &lt;&lt; &quot;Server: Buffer am ende: &quot; &lt;&lt; buffer &lt;&lt; endl;
		cout &lt;&lt; &quot;\n\n&quot;;

	}
	while ( true);
	//nausg = write(newsockfd, &quot;exit&quot;, sizeof(buffer));
     //n = write(newsockfd,&quot;ende vom spass&quot;,18);
     if (n &lt; 0) error(&quot;ERROR writing to socket&quot;);
     close(newsockfd);
     close(sockfd);
     return 0; 
}

int StringCompare (const char *s1, const char *s2)  //function to compare the character arrays
{
	int ret;
	if (strcmp(s1,s2)) ret = 0;
	else ret = 1;
	return ret;
}
</code></pre>
<p>Client</p>
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;netdb.h&gt;	//define a structure hostent
#include &lt;iostream&gt;

using namespace std;

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;	//server is a pointer to a structure and will defines a host computer on the interet

    char buffer[256] ,exitv;
    if (argc &lt; 3) {
       fprintf(stderr,&quot;usage %s hostname port\n&quot;, argv[0]);
	// cout &gt;&gt; stderr 
       exit(0);
    }
    portno = atoi(argv[2]);  //server.cpp
    sockfd = socket(AF_INET, SOCK_STREAM, 0); //server.cpp
    if (sockfd &lt; 0) 
        error(&quot;ERROR opening socket&quot;);
    server = gethostbyname(argv[1]);  //takes hostname from argv and returns a pointer to a hostent containing inforamtion about that host
    if (server == NULL) {
        fprintf(stderr,&quot;ERROR, no such host\n&quot;);
        exit(0);
    }
    bzero((char *) &amp;serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server-&gt;h_addr, //copy byte by byte from source to destination because server-&gt;h_addr is a character String (arguments: source,destination,length)
         (char *)&amp;serv_addr.sin_addr.s_addr,
         server-&gt;h_length);
    serv_addr.sin_port = htons(portno); //server.cpp

    if (connect(sockfd,(struct sockaddr *) &amp;serv_addr,sizeof(serv_addr)) &lt; 0) //connect establish a connection
        error(&quot;ERROR connecting&quot;);

do {

    cout &lt;&lt; &quot;Buffer am Beginn : &quot; &lt;&lt; buffer &lt;&lt; endl; 
    cout &lt;&lt; &quot;Stadt eingeben: &quot;;
    bzero(buffer,256);
	     cout &lt;&lt; &quot;\nBuffer nach bzero : &quot; &lt;&lt; buffer &lt;&lt; endl; 
    cin &gt;&gt; buffer;
     cout &lt;&lt; &quot;Buffer nach einlesen : &quot; &lt;&lt; buffer &lt;&lt; endl; 
    //fgets(buffer,255,stdin);  fgets geht nich, weill Null-character \0 an arrayende gesetzt wird. 
    //cout &lt;&lt; &quot;Buffer:  |&quot; &lt;&lt; buffer &lt;&lt;&quot;|&quot; ; nur zum debuggen
    n = write(sockfd,buffer,sizeof(buffer));
    if (n &lt; 0) 
         error(&quot;ERROR writing to socket&quot;);
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n &lt; 0) 
         error(&quot;ERROR reading from socket&quot;);
    cout &lt;&lt; &quot;Buffer am Ende: &quot; &lt;&lt; buffer &lt;&lt; endl; 
	bzero(buffer,256);
   }

while  (true);
	close(sockfd);
    return 0;
}
</code></pre>
<p>wie ich das gerade verfolgt hab bricht der Server das ganze sogar mit der Meldung: ERROR reading from socket: Bad file descriptor<br />
ab.<br />
Brauche also wirklich dringend hilfe <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2166375</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2166375</guid><dc:creator><![CDATA[Royalts]]></dc:creator><pubDate>Tue, 10 Jan 2012 15:32:48 GMT</pubDate></item><item><title><![CDATA[Reply to Socketprogrammierung on Tue, 10 Jan 2012 15:31:31 GMT]]></title><description><![CDATA[<p>1. Bitte richtig einrücken.<br />
2. read() gibt es nicht auf Windows, recv() schon. Würde ich bevorzugen. (Gleiches Gilt bei write() und send())<br />
3. Du solltest nicht nur auf &lt; 0 sondern auch auf == 0 prüfen. Das passiert nämlich, wenn jemand die Verbindung ordentlich schließt.<br />
PS: Bis auf cout ist das C und kein C++. <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="😉"
    /><br />
PPS: Nur weil dein Client alles auf einmal schickt, heißt das nicht, dass auch alles in einem Paket ankommt. Du solltest dir Trennzeichen überlegen um ein vollständiges Paket identifizieren zu können. z.B. '\n'.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2166385</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2166385</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Tue, 10 Jan 2012 15:31:31 GMT</pubDate></item><item><title><![CDATA[Reply to Socketprogrammierung on Tue, 10 Jan 2012 15:46:17 GMT]]></title><description><![CDATA[<p>bin auf unix unterwegs... deshalb weis ich das auch nich wirklich.<br />
sind die funktionen für c++ und c (also accept/bind/listen) nicht gleich? Dem widme ich mich, wenn alles so einigermaßen läuft.<br />
Das mit dem Trennzeichen meinst du aus kosmetischen gründen?! weil sich client und server ja immer nen character array von 255 zeichen hin und her schicken und nat. steht da am ende immer nen null-character. macht ja read() und write() von selbst?!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2166393</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2166393</guid><dc:creator><![CDATA[Royalts]]></dc:creator><pubDate>Tue, 10 Jan 2012 15:46:17 GMT</pubDate></item><item><title><![CDATA[Reply to Socketprogrammierung on Tue, 10 Jan 2012 16:04:53 GMT]]></title><description><![CDATA[<p>Die Sache ist einfach die, dass das was du absendest nicht auch so ankommen musst. Es kan zum Beispiel sein, dass du 256 Byte sendest und zunächst nur 20 Byte versendet werden. Anschließend dann 200 Byte und dann die restlichen Bytes.</p>
<p>Sprich: Die Segmentierung ist nicht festgelegt und du kannst nicht davon ausgehen, dass alles &quot;in einem Rutsch&quot; gesendet wird.</p>
<p>Dem kann man zum Beispiel entgegenwirken, indem man sagt, dass ein Leerzeichen eine Nachricht trennt.</p>
<p>Empfängst du dann z.B. Stuttgart\nBerlin\K</p>
<p>Dann weißt du, dass die Nachrichten Stuttgart und Berlin vollständig sind (Trennzeichen vorhanden). Bei der Nachricht K fehlt aber noch etwas, da das Trennzeichen noch nicht gelesen wurde. Kann also sein, dass beim nächsten Lesevorgang folgendes gelesen wird:<br />
öln\nAmsterdam\n</p>
<p>Eine sehr einfache aber ziemlich ineffiziente Lösung wäre es, wenn du immer nur ein Zeichen einliest. Wenn dann dein Trennzeichen ankommt, dann verarbeitest du die Nachricht und liest danach weiter ein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2166406</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2166406</guid><dc:creator><![CDATA[fgddfg]]></dc:creator><pubDate>Tue, 10 Jan 2012 16:04:53 GMT</pubDate></item><item><title><![CDATA[Reply to Socketprogrammierung on Tue, 10 Jan 2012 16:13:26 GMT]]></title><description><![CDATA[<p>read und write machen da nix selbst ...</p>
<p>Null-Char als &quot;trennzeichen&quot; geht tedentiell, das wird dir aber, wenn er erst mal anfaengt deine Packete zu fragmentieren( d.h. er bestimmt selbst was wann geschickt wird) probleme mit den string funktionen machen ... sprich du kannst dann nimmer mit denen draufrumrutschen (die beenden alle ihre taetigkeiten, wenn die ein NULL zeichen bekommen). Sondern du musst selber die packete zusammenbauen mit binaeren funktionen.<br />
Ausserdem sieht /n schoener aus, wenn deine communication mit nem terminal überwachst / emulierst <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="🙂"
    /></p>
<p>244 als standardgroesse geht technisch auch, aber macht eher sinn bei UDP (SOCK_DGRAM)<br />
Der vorteil von TCP ist eigentlich, das alles in richtiger reihenfolge durchkommt. Der nachteil: es ist nicht bestimmt wann.<br />
also wenn du 2 mal 512 byte schickst, kann am ande z.b. Packete als 300,300,300,124 ankommen. also du wurdest mit nem aufruf 300 bytes lesen, dann wieder ...</p>
<p>besser ist fuer TCP ein nicht durch längen bestimmtes Flussprotokoll.<br />
Und gut ist auch ein textBasierrendes (texte = ascii/utf8 = binaerspezifiziert, also kann nix drehen durch systeme, ausserdem kann man das besser loggen und emulieren )</p>
<blockquote>
<p>sind die funktionen für c++ und c (also accept/bind/listen) nicht gleich? Dem widme ich mich, wenn alles so einigermaßen läuft.</p>
</blockquote>
<p>c ist als &quot;Teil&quot; in c++ integriert, mit paar kleinen besonderheiten.<br />
Also kannst du von C++ immer auf c wechseln ....</p>
<p>C++ zu C ist eher ne Style-Frage.<br />
was du schreibst ist reiner C-Style.<br />
Was cooky451 meint ist, das die verwendung von std::cout nicht zu deinem Stil passt <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="🙂"
    /> mit printf statt std::cout koennte dein code auch sogar nen reiner c-compiler fressen <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="🙂"
    /></p>
<p>Ciao ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2166411</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2166411</guid><dc:creator><![CDATA[RHBaum]]></dc:creator><pubDate>Tue, 10 Jan 2012 16:13:26 GMT</pubDate></item><item><title><![CDATA[Reply to Socketprogrammierung on Tue, 10 Jan 2012 17:43:03 GMT]]></title><description><![CDATA[<p>Die richtige Konstante für die Funktion socket() heißt übrigens PF_INET, nicht AF_INET, auch wenn auf vielen Plattformen beide den selben Wert haben.</p>
<p>AF steht nämlich für address family, PF für protocol family.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2166447</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2166447</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Tue, 10 Jan 2012 17:43:03 GMT</pubDate></item></channel></rss>