IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Linux Discussion :

[socket][client-serveur] Erreur de type "Bad address" lors de l'appel d'accept()


Sujet :

Linux

  1. #1
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2005
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2005
    Messages : 417
    Points : 448
    Points
    448
    Par défaut [socket][client-serveur] Erreur de type "Bad address" lors de l'appel d'accept()
    Salut à tous,

    J'ai un projet à faire pour mes études, et je bloque sur une erreur. Les profs ne sont d'aucun secours et google non plus.

    Voici mon code pour le serveur :
    Code c : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
     
    int main()
    {
    	//initialisation des variables
    	errno=0;
     
    	//déclaration de la socket
    	int SocketConnexion, SocketCommunication;
    	struct sockaddr_in SocketConnexionParam, SocketCommunicationParam;
     
    	//déclaration des chaines reception et émission
    	char *Reception, Charactere;
     
    	//création de la socket
    	SocketConnexion = socket(PF_INET, SOCK_STREAM, 0);
    	if(SocketConnexion == -1)
    	{
    		printf("erreur sur socket : %s\n", strerror(errno));
    		return(1);
    	}
     
    	//initialisation de l'adresse
    	bzero(&SocketConnexionParam, sizeof(SocketConnexionParam));
    	SocketConnexionParam.sin_family = PF_INET;
    	SocketConnexionParam.sin_port = htons(9100);
    	SocketConnexionParam.sin_addr.s_addr = inet_addr("127.0.0.1");
     
    	//liaison entre le nom et la socket
    	if(bind(SocketConnexion, (struct sockaddr *) &SocketConnexionParam, sizeof(SocketConnexionParam)) != 0)
    	{
    		printf("erreur sur bind : %s\n", strerror(errno));
    		close(SocketConnexion);
    		return(1);
    	}
     
    	//mise en écoute
    	if(listen(SocketConnexion, 10) != 0)
    	{
    		printf("erreur sur listen : %s\n", strerror(errno));
    		close(SocketConnexion);
    		return(1);
    	}
     
    	while(1)
    	{
    		//accepte les connexions entrantes
    		SocketCommunication = accept(SocketConnexion, (struct sockaddr *) &SocketCommunicationParam, (socklen_t *) sizeof(SocketCommunicationParam));
     
    		if(SocketCommunication != -1)
    		{
    			while(1)
    			{
    				while(1)
    				{
    					if (read(SocketCommunication, &Charactere, 1) == 1)
    					{printf("TEST\n");
    							*Reception++ = Charactere;
    						if (Charactere == '\0')
    							break;
    					}
    				}
     
    				if(strcmp(Reception, "PING") == 0)
    				{
    					write(SocketCommunication, "PONG\0", 5);
    				}
     
    				if(strcmp(Reception, "DING") == 0)
    				{
    					write(SocketCommunication, "DONG\0", 5);
    				}
     
    				if(strcmp(Reception, "FERMETURE") == 0)
    				{
    					close(SocketCommunication);
    					break;
    				}
     
    				if(strcmp(Reception, "ARRET") == 0)
    				{
    					close(SocketCommunication);
    					close(SocketConnexion);
    					return(0);
    				}
    			}//end while(1)
    		}
    		else
    		{
    			printf("erreur sur accept : %s\n", strerror(errno));
    			close(SocketConnexion);
    			return(1);
    		}
    	}//end while(1)
    }

    et le code d'un des clients :
    Code c : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
     
    int main()
    {
    	//initialisation des variables
    	errno=0;
     
    	//déclaration de la socket
    	int SocketCommunication;
    	struct sockaddr_in SocketParam;
     
    	//déclaration des chaines reception et émission
    	char *Reception, Charactere;
     
    	//création de la socket
    	SocketCommunication = socket(PF_INET, SOCK_STREAM, 0);
    	if(SocketCommunication == -1)
    	{
    		printf("erreur sur socket : %s\n", strerror(errno));
    		return(1);
    	}
     
    	//initialisation de l'adresse
    	bzero(&SocketParam, sizeof(SocketParam));
    	SocketParam.sin_family = PF_INET;
    	SocketParam.sin_port = htons(9100);
    	SocketParam.sin_addr.s_addr = inet_addr("127.0.0.1");
     
    	//Connexion
    	if(connect(SocketCommunication, (struct sockaddr *) &SocketParam, sizeof(SocketParam)) == 0)
    	{
    		write(SocketCommunication, "PING\0", 5);
     
    		printf("PING\n");
     
    		while(1)
    		{
    			if (read(SocketCommunication, &Charactere, 1) == 1)
    			{printf("TEST\n");
    				*Reception++ = Charactere;
    				if (Charactere == '\0')
    					break;
    			}
    		}
     
    		printf("%s\n", Reception);
     
    		if(strcmp(Reception, "PONG") == 0)
    		{printf("fermeture");
    			write(SocketCommunication, "FERMETURE\0", 10);
    		}
     
    		close(SocketCommunication);
    	}
    	else
    	{
    		printf("erreur sur connect : %s\n", strerror(errno));
    		return(1);
    	}
     
    	return(0);
    }

    Lors du lancement du serveur, il n'y a aucune erreur. Mais lorsque le client est lancé et tente de se connecter au serveur, celui ci renvois une erreur lors de l'accept() :
    [toto@tata]$ ./Serveur
    erreur sur accept : Bad address
    [toto@tata]$
    Pourtant le client ne renvois aucune erreur sur le connect().

    Est-ce que quelqu'un aurait une idée du pourquoi du comment que ca marche pas ?

  2. #2
    Membre émérite Avatar de nicolas.sitbon
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2 015
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2 015
    Points : 2 280
    Points
    2 280
    Par défaut
    SocketCommunication = accept(SocketConnexion, (struct sockaddr *) &SocketCommunicationParam, (socklen_t *) sizeof(SocketCommunicationParam));
    "The quieter you become, the more you are able to hear"
    "Plus vous êtes silencieux, plus vous êtes capable d'entendre"

  3. #3
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2005
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2005
    Messages : 417
    Points : 448
    Points
    448
    Par défaut
    Vous pourriez etre plus explicite ?

    Je suis grand débutant et j'ai pas tout compris là.

  4. #4
    Responsable technique

    Avatar de Anomaly
    Homme Profil pro
    Directeur
    Inscrit en
    Juin 2003
    Messages
    10 338
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Somme (Picardie)

    Informations professionnelles :
    Activité : Directeur
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 338
    Points : 130 373
    Points
    130 373
    Billets dans le blog
    1
    Par défaut
    Tu dois passer en dernier paramètre de accept un pointeur vers une variable qui va recevoir la taille de la structure sockaddr.

    Code c : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    socklen_t tailleaddr = sizeof(SocketCommunicationParam);
    SocketCommunication = accept(SocketConnexion, (struct sockaddr *) &SocketCommunicationParam, &tailleaddr);
    Responsable technique forum & site

    Si ce message (ou un autre) vous a aidé et/ou vous semble pertinent, votez pour lui avec

  5. #5
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2005
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2005
    Messages : 417
    Points : 448
    Points
    448
    Par défaut
    Je test, merci ano :awe:

  6. #6
    Membre émérite Avatar de nicolas.sitbon
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2 015
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2 015
    Points : 2 280
    Points
    2 280
    Par défaut
    Dans tous les cas, ce genre d'expression produit un comportement indéterminé:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     (socklen_t *) sizeof(SocketCommunicationParam)
    et n'a aucun sens.
    "The quieter you become, the more you are able to hear"
    "Plus vous êtes silencieux, plus vous êtes capable d'entendre"

  7. #7
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2005
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2005
    Messages : 417
    Points : 448
    Points
    448
    Par défaut
    Cool, ben fallait le dire au lieu de juste surligner.

    Parce que là, comme c'est mon prof de tp qui m'a dit de faire ça, j'ai considéré comme juste.

    La prochaine fois, un petit commentaire ferait pas de mal :jap:

    Edith me dit que grâce à ano, ca marche. merci encore.

  8. #8
    Membre émérite Avatar de nicolas.sitbon
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2 015
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2 015
    Points : 2 280
    Points
    2 280
    Par défaut
    Non la prochaine fois, réflêchir un peu ne ferait pas de mal.
    "The quieter you become, the more you are able to hear"
    "Plus vous êtes silencieux, plus vous êtes capable d'entendre"

  9. #9
    Membre émérite Avatar de nicolas.sitbon
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2 015
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2 015
    Points : 2 280
    Points
    2 280
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    #include <stdbool.h>
    #include <stdlib.h>
     
    int main(void)
    {
       int ret = 0;
       int SocketConnexion = socket(PF_INET, SOCK_STREAM, 0);
     
       if(SocketConnexion == -1)
       {
          perror("socket");
          ret = EXIT_FAILURE;
       }
       else
       {
          struct sockaddr_in SocketConnexionParam =
          {
             .sin_family      = AF_INET,
             .sin_port        = htons(9100),
             .sin_addr.s_addr = inet_addr("127.0.0.1")
          };
     
          if(bind(SocketConnexion, (struct sockaddr *) & SocketConnexionParam, sizeof SocketConnexionParam) == -1)
          {
             perror("bind");
             ret = EXIT_FAILURE;
          }
          else if(listen(SocketConnexion, SOMAXCONN) == -1)
          {
             perror("listen");
             ret = EXIT_FAILURE;
          }
          else
          {
             bool main_loop = true;
     
             while(main_loop)
             {
                struct sockaddr_in SocketCommunicationParam = {0};
                int SocketCommunication = accept(SocketConnexion, (struct sockaddr *) & SocketCommunicationParam, &(socklen_t) {sizeof SocketCommunicationParam});
     
                if(SocketCommunication != -1)
                {
                   bool inner_loop = true;
     
                   while(inner_loop)
                   {
                      char Reception[BUFSIZ];
                      char Charactere = 0;
                      ssize_t read_byte = 0;
                      size_t pos = 0;
     
                      do
                      {                       
                         read_byte = read(SocketCommunication, &Charactere, 1);
     
                         if(read_byte == 1)
                         {
                            puts("TEST");
                            Reception[pos++] = Charactere;
                         }
                      }
                      while (read_byte == 1  &&  Charactere != '\0'  &&  pos < sizeof Reception - 1);
     
                      Reception[pos] = 0;
     
                      if(strcmp(Reception, "PING") == 0)
                      {
                         write(SocketCommunication, "PONG\0", 5);
                      }
                      else if(strcmp(Reception, "DING") == 0)
                      {
                         write(SocketCommunication, "DONG\0", 5);
                      }
                      else if(strcmp(Reception, "FERMETURE") == 0)
                      {
                         inner_loop = false;
                      }
                      else if(strcmp(Reception, "ARRET") == 0)
                      {
                         main_loop = inner_loop = false;
                      }
                      else
                      {
                         /* WHAT TO DO??? */
                      }
                   } /* ending inner_loop */
     
                   close(SocketCommunication), SocketCommunication = -1;
                }
                else
                {
                   perror ("accept");
                   ret = EXIT_FAILURE;
                   main_loop = false;
                }
             } /* ending main_loop */
          }
     
          close(SocketConnexion), SocketConnexion = -1;
       }
     
       return ret;
    }
    "The quieter you become, the more you are able to hear"
    "Plus vous êtes silencieux, plus vous êtes capable d'entendre"

  10. #10
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2005
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2005
    Messages : 417
    Points : 448
    Points
    448
    Par défaut
    Merci. Je comprends déjà plus ce qui n'allait pas.

  11. #11
    Membre averti
    Homme Profil pro
    DevOps AWS
    Inscrit en
    Juillet 2009
    Messages
    120
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : DevOps AWS
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2009
    Messages : 120
    Points : 334
    Points
    334
    Par défaut
    Je me permet de remonter ce thread un peu vieu mais qui vient de me sortir de l'impasse.

    J'aimerais comprendre la différence entre

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    //accepte les connexions entrantes
    		SocketCommunication = accept(SocketConnexion, (struct sockaddr *) &SocketCommunicationParam, (socklen_t *) sizeof(SocketCommunicationParam));
    et ça

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    socklen_t tailleaddr = sizeof(SocketCommunicationParam);
    SocketCommunication = accept(SocketConnexion, (struct sockaddr *) &SocketCommunicationParam, &tailleaddr);
    Je viens de faire exactement la même errreur mais je ne saisis pas la subtilité entre les deux (et c'est en train de froisser mon égo surdimentionné de dev ).
    De plus ce point me semble important et pourtant tout les tuto sur les socket en C font l'impasse dessus et c'est fort dommageable.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [socket] client/serveur multi-thred
    Par iag_1980 dans le forum Entrée/Sortie
    Réponses: 5
    Dernier message: 03/03/2007, 15h38
  2. socket client serveur
    Par idino dans le forum Réseau
    Réponses: 3
    Dernier message: 29/01/2007, 14h45
  3. Indy client & serveur erreurs
    Par uriotcea dans le forum C++Builder
    Réponses: 4
    Dernier message: 13/08/2006, 02h02
  4. Socket client serveur
    Par rhdjml dans le forum Langage
    Réponses: 2
    Dernier message: 08/03/2006, 12h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo