TCP-Server fehler "Connection reset by peer"



  • Hi, ich habe einen Thread der so aussieht:

    void *readThread(void)
    {
    pthread_mutex_lock(&shutdownmutex);
    		while(!shutdown)
    		{
    			pthread_mutex_unlock(&shutdownmutex);	// unlock variable "shutdown"
    			char msg[1024];
    			int status = recv(new_socket_fd, msg, sizeof(msg),0 ); // read message from client 
    
    			if(status == 0) // client closed transmission
    			{
    				//cout << "connection closed" << endl;
    				pthread_mutex_lock(&shutdownmutex);	// lock variable "shutdown"
    				shutdown = true;			// close threads properly
    				pthread_mutex_unlock(&shutdownmutex);	// lock variable "shutdown"
    			}
    			else if ( status < 0) //  some error
    			{  
         				signal_error("ERROR: error receiving from client",0xA007);
         				//cout << strerror(errno) << endl;
    			}
    			else
    			{
    				readPtr(msg,status);
    			}
    			pthread_mutex_lock(&shutdownmutex);	// lock variable "shutdown"
    		}
    		pthread_mutex_unlock(&shutdownmutex); // unlock variable "shutdown"
    
    		pthread_cond_signal(&startSending); // tell the other thread to shut down
    		listenSock();
    		return 0;
    }
    

    und einen Client:

    int main (int argc, char *argv[] )
    {
      int s, n;
      struct sockaddr_in sin; struct hostent *hptr;
      char msg[80] = "Hello World!";
    if ( argc < 3 ) {
          printf ( "%s host port\n", argv[0] );   /* input error: need host & port */
          return -1;
    }
    if ( (s = socket(AF_INET, SOCK_STREAM, 0 ) ) < 0) { /* create socket*/
      perror("socket");  /* socket error */
      return -1;
    }
    sin.sin_family = AF_INET;              /*set protocol family to Internet */
    sin.sin_port = htons(atoi(argv[2]));  /* set port no. */
    if ( (hptr =  gethostbyname(argv[1]) ) == NULL){
           fprintf(stderr, "gethostname error: %s", argv[1]);
           return  -1;
     }
    memcpy( &sin.sin_addr, hptr->h_addr, hptr->h_length);
    if (connect (s, (struct sockaddr *)&sin, sizeof(sin) ) < 0 ){
         perror("connect"); return -1;   /* connect error */
    }
    if ( send(s, msg, strlen(msg) +1,0) < 0 ) {  /* send message to server */
           perror("write");    return -1; /*  write error */
    }
    if ( ( n = recv(s, msg, sizeof(msg),0 ) ) <0) {  /* read message from server */
         perror("read"); return -1; /*  read error */
    }
    printf (" %d bytes: %s\n", n, msg);  /* print message to screen */
    /* close connection, clean up socket */
    if (close(s) < 0) { 
       perror("close");   /* close error */
       return -1;}
    return 0;
    }
    

    Aber ich erhalte immer wenn ich den Client starte nach der Empfangenen und wieder zurück gesendeten Nachricht folgende ausgabe:
    `received: Hello World! (13)

    ERROR: error receiving from client (a007)

    Connection reset by peer

    connection closed

    `
    Und ich weiß leider nicht warum da vor connection closed ein "Connection reset by peer" steht

    ICh hoffe es kann sich das mal jemand ansehen

    LG



  • Dieser Thread wurde von Moderator/in rüdiger aus dem Forum ANSI C in das Forum Linux/Unix verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.



  • recv schrieb:

    void *readThread(void)
    {
    pthread_mutex_lock(&shutdownmutex);
    		while(!shutdown)
    		{
    			pthread_mutex_unlock(&shutdownmutex);	// unlock variable "shutdown"
    			char msg[1024];
    			int status = recv(new_socket_fd, msg, sizeof(msg),0 ); // read message from client 
    			
    			if(status == 0) // client closed transmission
    			{
    				//cout << "connection closed" << endl;
    				pthread_mutex_lock(&shutdownmutex);	// lock variable "shutdown"
    				shutdown = true;			// close threads properly
    				pthread_mutex_unlock(&shutdownmutex);	// lock variable "shutdown"
    			}
    			else if ( status < 0) //  some error
    			{  
         				signal_error("ERROR: error receiving from client",0xA007);
         				//cout << strerror(errno) << endl;
    			}
    			else
    			{
    				readPtr(msg,status);
    			}
    			pthread_mutex_lock(&shutdownmutex);	// lock variable "shutdown"
    		}
    		pthread_mutex_unlock(&shutdownmutex); // unlock variable "shutdown"
    		
    		pthread_cond_signal(&startSending); // tell the other thread to shut down
    		listenSock();
    		return 0;
    }
    

    könnte es sein das du da in eine race condition der shutdown variable rein läufst? müßte man in dem fall nicht die komplette schleife mutexen also so ungefähr

    void *readThread(void)
    {
    pthread_mutex_lock(&shutdownmutex);
    		while(!shutdown)
    		{
    			char msg[1024];
    			int status = recv(new_socket_fd, msg, sizeof(msg),0 ); // read message from client 
    
    			if(status == 0) // client closed transmission
    			{
    				//cout << "connection closed" << endl;
    				shutdown = true;
    			}
    			else if ( status < 0) //  some error
    			{  
         				signal_error("ERROR: error receiving from client",0xA007);
         				//cout << strerror(errno) << endl;
    			}
    			else
    			{
    				readPtr(msg,status);
    			}
    		}
    		pthread_mutex_unlock(&shutdownmutex); // unlock variable "shutdown"
    
    		pthread_cond_signal(&startSending); // tell the other thread to shut down
    		listenSock();
    		return 0;
    }
    

    oder alternativ mit ner leicht umgestalteten if abfrage, wir wollen ja nicht immer gleich vom schlimmsten ausgehen 😉

    void *readThread(void)
    {
    		while(1)
    		{
    			char msg[1024];
    			int status = recv(new_socket_fd, msg, sizeof(msg),0 ); // read message from client 
    
    			if(status > 0) // client closed transmission
    			{
    				readPtr(msg,status);
    			}
    			else if ( status == 0)
    			{  
    				//cout << "connection closed" << endl;
    				break;
    			}
    			else //status < 0
    			{
         				signal_error("ERROR: error receiving from client",0xA007);
         				//cout << strerror(errno) << endl;
    			}
    		}
    		return 0;
    }
    

    lg lolo



  • na also geht doch auch mit nem goto 😉

    void *readThread(void)
    {
      char msg[1024];
      int status;
    
      loop:
    //while(1){
        status = recv(new_socket_fd, msg, sizeof(msg),0 );
        if(status > 0){
          readPtr(msg,status);
          //continue;
          goto loop;
        }else if( status == 0){
          //cout << "connection closed" << endl;
        }else{//status < 0
          signal_error("ERROR: error receiving from client",0xA007);
          //cout << strerror(errno) << endl;
        }
        //break;
    //}
      return 0;
    }
    

    lg lolo[/quote]



  • recv schrieb:

    Aber ich erhalte immer wenn ich den Client starte nach der Empfangenen und wieder zurück gesendeten Nachricht folgende ausgabe:
    `received: Hello World! (13)

    ERROR: error receiving from client (a007)

    Connection reset by peer

    connection closed

    `
    Und ich weiß leider nicht warum da vor connection closed ein "Connection reset by peer" steht

    Das liegt daran, dass die Gegenstelle die Verbindung killt und nicht schliesst. close() oder closesocket() statt shutdown(), vorzeitiges Beenden des Programms könnten Ursachen dafür sein.



  • noobLolo schrieb:

    na also geht doch auch mit nem goto 😉

    Gibt aber Abzüge in der B-Note. 😉



  • EOP schrieb:

    noobLolo schrieb:

    na also geht doch auch mit nem goto 😉

    Gibt aber Abzüge in der B-Note. 😉

    Was ist ein Abzug in der B-Note schrieb:

    Das ist eine Wertung beim Eiskunstlauf oder beim standard- und Lateintanzen. Die A-Note ist die technische Ausführung einer Kür oder eines Tanzes. Die B-Note ist die Grazie, die Musikalität, die künstlerische Darbietung. Diese Wertungen sind sehr umstritten, weil sie halt Geschmacksabhängig sind, und nicht nach festlegbaren Kriterien stattfinden. Deshalb ist Tanzen auch noch nicht olympisch.

    😃

    lg lolo



  • Eigentlich wollt ich mit dem Post auch nur meinen Fehler in dem Post "07:47:56 20.02.2010" in dem Codeblock unter "oder alternativ mit ner leicht umgestalteten if abfrage, wir wollen ja nicht immer gleich vom schlimmsten ausgehen" vertuschen, wo die Schleife bei einem Fehler nie verlassen wird daher eher ein abzug in der A-Note 👍



  • Danke, ich hab das ganze schon gelöst, da ich aber keine Antwort (abgesehen vom Forums-Bot 🙂 ) dachte ich es interessiert keinen wie es gelöst wurde 😉



  • recv schrieb:

    Danke, ich hab das ganze schon gelöst, da ich aber keine Antwort (abgesehen vom Forums-Bot 🙂 ) dachte ich es interessiert keinen wie es gelöst wurde 😉

    Natürlich! Immer her damit, stell dir vor das googelt jemand, und dann hat er nur das Problem ohne Lösung == total wertlos

    googel doch mal "c tcp server fehler" bist direkt auf Platz 1 also Bitte Lösungen auch posten 🙂

    lg lolo



  • Ich weiß nicht mehr ganz genau wies war, aber in doListen() wurde der readThread erneut aufgerufen bevor der alte geschlossen war, bzw. bevor accept() aufgerufen wurde.

    LG


Anmelden zum Antworten