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

C Discussion :

Problème de réception de message simple avec recvfrom() (socket)


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti Avatar de beau_g
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2017
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Nièvre (Bourgogne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2017
    Messages : 26
    Par défaut Problème de réception de message simple avec recvfrom() (socket)
    Boujour à tous,

    Dans le cadre d'un projet, j'ai besoin de faire communiquer un "client" et un "serveur" à l'aide d'envoie de socket. Sans vous expliquez en détail ce projet, j'essaie dans un premier temps d'envoyer un simple "hello!" en unicast à une addresse IP local. J'ai crée pour cela une thread (ci dessous):

    main:

    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
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pthread.h>
    #include "unicast_socket_sendto.h"
     
    /*Returns 0 if OK, a negative value if EOF.*/
    int fpurge(FILE *f)
    {
    	int c;
    	while((c=fgetc(f))!=EOF && c!='\n')
    	{ }
    	return (c==EOF ? -1 : 0);
    }
     
    void *send_uni(void *arg)
    {
    	char *dest_ip = "100.0.0.0";
    	char *message = "hello";
    	int port = 7;
     
     
    	while (1)
    	{
     
    		printf("Push a letter S or Q:\r\n ");
    		char c = getchar();
    		fpurge(stdin);
     
    		if ((c=='s')||(c=='S'))
    		{
    			printf("Message sent: %s\n", message);
    			unicast_socket_sendto(port, dest_ip, message);
    			system("BREAK");
    			return EXIT_SUCCESS;
    		}
    		else if ((c=='q')||(c=='Q'))
    		{
    			printf("QUIT");
    			break;
    		}
    		else
    		{
    			printf("Invalid key, tape 'S' to send a message, 'Q' to exit.");
    		}
    	}
     
    	return NULL;
    }
     
     
    int main(void)
    {
    	/* this variable is our reference to the second thread */
    	pthread_t thread;
     
    	/* create a second thread which executes send_uni(void *arg) */
     
    	if(pthread_create(&thread, NULL, send_uni, NULL)) {
    		fprintf(stderr, "Error creating thread\n");
    		return 1;
    	}
     
    	/* wait for the second thread to finish */
    	if(pthread_join(thread, NULL)) {
    		fprintf(stderr, "Error joining thread\n");
    		return 2;
    	}
    	return 0;
    }
    classe "unicast_socket_sendto.c" :

    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
     
    #include "unicast_socket_sendto.h"
     
    #include <errno.h>  /* errno */
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/time.h>
     
    /* gettimeofday */
     
    int unicast_socket_sendto( int port, char * dest_ip, char * message) {
     
    	struct sockaddr_in sDest;
    	int socket_id;
    	WSADATA wsad; //Structure required to activate sockets
    	int iResult = 0;  // variable to perform tests
        char msg_rcv[1024]; //message received
     
    	if (WSAStartup(2,&wsad) == 0)
    	{
    		printf("Sockets are now usable...\n");
    	}
    	else
    	{
    		printf("Error n°%d : Error during initialization.\n", errno);
    		//exit(errno);
    		return errno;
    	}
     
    	//Initialization of the UDP socket
    	socket_id = socket(AF_INET, SOCK_DGRAM,0);
    	if(socket_id != -1)
    	{
    		printf("Initialization of the UDP socket successful.\n");
    	}
    	else
    	{
    		printf("Erreur n%d : Unable to initialize socket.\n",errno);
    		//exit(errno);
    		return errno;
    	}
     
    	// initialization of the destination structure
    	sDest.sin_family = AF_INET;
    	sDest.sin_port = htons(port);
    	sDest.sin_addr.s_addr = inet_addr(dest_ip);
     
    	int sizeSdest = sizeof(sDest); //Size of the receiving structure
     
    	// Sending the message
    	printf("Sending of the socket ...\n");
    	iResult = sendto(socket_id,message,strlen(message)+1, 0, (struct sockaddr *)&sDest, sizeof(sDest));
    	if (iResult != -1)
    	{
    		printf("Successful message sending.\n");
    	}
    	else
    	{
    		printf("Error n%d : Unable to send message.\n",errno);
    		//exit(errno);
    		return errno;
    	}
     
    	// Receiving the message
    	iResult = recvfrom(socket_id, msg_rcv, sizeof(msg_rcv), 0, (struct sockaddr *)&sDest, &sizeSdest);
    	msg_rcv[iResult]='\0'; // recvfrom renvoie le nombre d'octet lu!
     
    	if (iResult != -1)
    	{
    		printf("successful message reception.\n");
    	}
    	else
    	{
    		printf("Error n%d : Unable to receive message.\n",errno);
    		//exit(errno);
    		return errno;
    	}
    	// Display of the message without interference
    	printf("The message is : %s\n", msg_rcv);
     
    	//Closing the socket
    	if (closesocket(socket_id) == 0)
    	{
    		printf("Socket closing.\n");
    	}
    	else
    	{
    		printf("Error closing the socket.\n");
    		//exit(errno);
    		return errno;
    	}
     
    	return 0;
    }
    J'ai rajouté la fonction "recvfrom" afin de savoir si le message était bien réceptionné mais apparemment non^^
    Je pense que c'est pas rapport au choix de l’adresse IP de réception. Doit elle être la même que celle de l'ordinateur ? Le choix du port aussi est il important ?

    Voici le message que le prgramme m'affiche:

    Push a letter S or Q:
    S
    Message sent: hello
    Sockets are now usable...
    Initialization of the UDP socket successful.
    Sending of the socket ...
    Successful message sending.
    Error n34 : Unable to receive message.

    Merci d'avance pour vos réponses,

    Cordialement,

    beaug.

  2. #2
    Modérateur

    Avatar de Bktero
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2009
    Messages
    4 493
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2009
    Messages : 4 493
    Billets dans le blog
    1
    Par défaut
    Je pense que c'est pas rapport au choix de l’adresse IP de réception. Doit elle être la même que celle de l'ordinateur ?
    Et ça donne quoi si tu mets l'adresse du PC ?

  3. #3
    Membre averti Avatar de beau_g
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2017
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Nièvre (Bourgogne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2017
    Messages : 26
    Par défaut
    Bonjour,

    Bizarrement, ça reste bloqué après l'envoie du message...

    Nom : Capture1.PNG
Affichages : 477
Taille : 5,6 Ko

  4. #4
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 395
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 395
    Par défaut
    Ne faut-il pas binder son socket à un port pour pouvoir faire des recvfrom() dessus?
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  5. #5
    Membre averti Avatar de beau_g
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2017
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Nièvre (Bourgogne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2017
    Messages : 26
    Par défaut
    je ne sais pas j'ai vu beaucoup d'exemple sans, mais j'ai quand même essayé de rajouter un bind....sans succès

    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
    109
    110
    111
    112
     
    #include "unicast_socket_sendto.h"
     
    #include <errno.h>  /* errno */
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/time.h>
     
    /* gettimeofday */
     
     
    int unicast_socket_sendto( int port, char * dest_ip, char * message) {
     
     
    	struct sockaddr_in sDest;
    	int socket_id;
    	WSADATA wsad; //Structure required to activate sockets
    	int iResult = 0;  // variable to perform tests
        char msg_rcv[1024]; //message received
     
    	if (WSAStartup(2,&wsad) == 0)
    	{
    		printf("Sockets are now usable...\n");
    	}
    	else
    	{
    		printf("Error n°%d : Error during initialization.\n", errno);
    		//exit(errno);
    		return errno;
    	}
     
    	//Initialization of the UDP socket
    	socket_id = socket(AF_INET, SOCK_DGRAM,0);
    	if(socket_id != -1)
    	{
    		printf("Initialization of the UDP socket successful.\n");
    	}
    	else
    	{
    		printf("Erreur n%d : Unable to initialize socket.\n",errno);
    		//exit(errno);
    		return errno;
    	}
     
    	// initialization of the destination structure
    	sDest.sin_family = AF_INET;
    	sDest.sin_port = htons(port);
    	sDest.sin_addr.s_addr = inet_addr(dest_ip);
     
    	int sizeSdest = sizeof(sDest); //Size of the receiving structure
     
    	// Sending the message
    	printf("Sending of the socket ...\n");
    	iResult = sendto(socket_id,message,strlen(message)+1, 0, (struct sockaddr *)&sDest, sizeof(sDest));
    	if (iResult != -1)
    	{
    		printf("Successful message sending.\n");
    	}
    	else
    	{
    		printf("Error n%d : Unable to send message.\n",errno);
    		//exit(errno);
    		return errno;
    	}
     
    	// Receiving the message
     
    	/* bind to receive address */
    	if (bind(socket_id, (struct sockaddr *)&sDest, sizeSdest) == 0) {
    		printf("bind: Ok.\n");
    	}
    	else
    	{
    		printf("Error n%d: bind.\n",errno);
    		//exit(errno);
    	    return errno;
        }
     
     
     
    	iResult = recvfrom(socket_id, msg_rcv, sizeof(msg_rcv), 0, (struct sockaddr *)&sDest, &sizeSdest);
    	msg_rcv[iResult]='\0'; // recvfrom renvoie le nombre d'octet lu!
     
    	if (iResult != -1)
    	{
    		printf("successful message reception.\n");
    	}
    	else
    	{
    		printf("Error n%d : Unable to receive message.\n",errno);
    		//exit(errno);
    		return errno;
    	}
    	// Display of the message without interference
    	printf("The message is : %s\n", msg_rcv);
     
    	//Closing the socket
    	if (closesocket(socket_id) == 0)
    	{
    		printf("Socket closing.\n");
    	}
    	else
    	{
    		printf("Error closing the socket.\n");
    		//exit(errno);
    		return errno;
    	}
     
    	return 0;
    }

    Message sent: hello
    Sockets are now usable...
    Initialization of the UDP socket successful.
    Sending of the socket ...
    Successful message sending.
    Error n34: bind.
    l'erreur s'arrête au bind;

  6. #6
    Membre averti Avatar de beau_g
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2017
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Nièvre (Bourgogne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2017
    Messages : 26
    Par défaut
    bind() failed: 10022 plus précisement; grace à WSAGetLastError()

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

Discussions similaires

  1. Réponses: 0
    Dernier message: 16/12/2010, 12h28
  2. Réponses: 4
    Dernier message: 29/04/2009, 16h01
  3. problème simple avec des "if"
    Par johnburton54 dans le forum VBA Access
    Réponses: 11
    Dernier message: 07/06/2007, 10h24
  4. Problème simple avec des checkbox
    Par nanor21 dans le forum Langage
    Réponses: 2
    Dernier message: 07/05/2006, 00h26
  5. problème pour parser un fichier xml avec XML::Simple
    Par black_code dans le forum Modules
    Réponses: 3
    Dernier message: 30/01/2006, 19h32

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