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 :

Socket dans une thread


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 Socket dans une thread
    Bonjour a tous,

    Dans le cadre d'un projet et etant debutant en C, je viens à vous pour un petit coup de pouce.
    Je cherche à envoyer une message vers un serveur à l'aide d'une socket; et en utilisant les threads.

    J'ai donc créer la fonction suivante qui me permet normalement d'envoyer un message de type char à une addresse et à un port donnés en unicast

    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
     
    #include "unicast_socket_sendto.h"
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/time.h> /* gettimeofday */
     
    void unicast_socket_sendto(int *sockfd, int* port, char* address, char* data) {
     
    	    struct hostent *he; /* server address info */
    	    struct sockaddr_in their_addr;
    	    int bytesReceived;
    	    socklen_t addr_len = (socklen_t)sizeof (their_addr);
    	    int sock = *sockfd;
     
     
    	    /* resolve server host name or IP address */
    	    if ((he = gethostbyname(address)) == NULL) {
    	    	perror("Server gethostbyname");
    	    	exit(1);
    	    }
     
    	    printf("Setting up socket Port %d: ", *port);
    	    /* Setup the socket */
    	    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    	        perror("Talker socket");
    	        exit(1);
    	    }
    	    printf("OK\n");
     
     
    	    memset(&their_addr, 0, sizeof (their_addr)); /* zero struct */
    	    /* Server details */
    	    their_addr.sin_family = AF_INET; /* host byte order .. */
    	    their_addr.sin_port = htons(*port); /* .. short, netwk byte order */
    	    their_addr.sin_addr = *((struct in_addr *) he->h_addr);
     
     
           /* Sends the data to the server. */
           if ((bytesReceived = sendto(sock, &data, sizeof (data), 0,
                (struct sockaddr *)&their_addr, addr_len)) == -1) {
            perror("Server Send Error");
            exit(1);
            }
     
    }
    Je souhaite maintenant inserer cette fonction dans un thread qui me permettra d'envoyer cette d'envoyer un message pendant un certains temps; jusqu'a l'envoie du message complet par exemple; j'ai donc essayé d'écrire un code qui ressemble a ca:

    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
    #include "thread.h"
    #include "unicast_socket_sendto.h"
     
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pthread.h>
     
    #define  UNI_ADDR      "2.5.6.7"       /* Arg: IP Uni Address */
    #define  UNI_PORT        1234       /* Arg: Server port */
    #define  MESSAGE        "Hello"
     
    /* this function is run by the second thread */
    void *send_unicast(void *void_msg)
    {
    	/* increment x to 100*/
    	char msg = (char *)void_msg;
    	int sockfd;
     
    	char uni_add = UNI_ADDR;
    	int uni_port = UNI_PORT;
     
    	while (1) {
    		unicast_socket_sendto(sockfd, &uni_port, &uni_add, &msg);
    	    }
     
    	printf("sending finished\n");
    	close(sockfd);
    	/* the function must return something - NULL will do */
    	return NULL;
    }
     
    int main()
    {
    	char msg = MESSAGE;
     
    	/* this variable is our reference to the second thread */
     
    	pthread_t inc_x_thread;
     
    	/* create a second thread which executes send_unicast(&msg) */
     
    	if(pthread_create(&inc_x_thread, NULL, send_unicast, &msg)) {
    		fprintf(stderr, "Error creating thread\n");
    		return 1;
     
    	}
     
    	return 0;
     
    }
    Mais il y a beaucoup d'erreurs ; qui se ressemblent plus ou moins; et j'ai beaucoup de mal à les corriger.

    Pouvez vous m'expliquer l'origine de ses erreurs et m'aider si possible à les supprimer.

    De plus, mon utilisation des threads n'etant pas encore au point; j'aurai besoin de petits conseils pour les utiliser dans ce contexte ; il doit me manquer quelques elements importants

    13:19:37 **** Incremental Build of configuration Debug for project test2 ****
    Info: Internal Builder is used for build
    gcc -O0 -g3 -Wall -c -fmessage-length=0 -o thread.o "..\\thread.c"
    In file included from c:\mingw\include\windows.h:42:0,
    from c:\mingw\include\winsock2.h:22,
    from c:\mingw\include\ws2tcpip.h:19,
    from ..\unicast_socket_sendto.h:14,
    from ..\thread.c:9:
    c:\mingw\include\windef.h:66:9: erreur: les noms de macro doivent être des identificateurs
    #define '\0' ((void*)0)
    ^
    ..\thread.c: Dans la fonction 'send_unicast':
    ..\thread.c:25:13: attention : initialization makes integer from pointer without a cast [-Wint-conversion]
    char msg = (char *)void_msg;
    ^
    ..\thread.c:17:22: attention : initialization makes integer from pointer without a cast [-Wint-conversion]
    #define UNI_ADDR "2.5.6.7" /* Arg: IP Uni Address */
    ^
    ..\thread.c:28:17: note: in expansion of macro 'UNI_ADDR'
    char uni_add = UNI_ADDR;
    ^
    ..\thread.c:32:33: attention : passing argument 2 of 'unicast_socket_sendto' makes integer from pointer without a cast [-Wint-conversion]
    unicast_socket_sendto(sockfd, &uni_port, &uni_add, &msg);
    ^
    In file included from ..\thread.c:9:0:
    ..\unicast_socket_sendto.h:30:6: note: expected 'int' but argument is of type 'int *'
    void unicast_socket_sendto(int sockfd, int port, char address, char data);
    ^
    ..\thread.c:32:44: attention : passing argument 3 of 'unicast_socket_sendto' makes integer from pointer without a cast [-Wint-conversion]
    unicast_socket_sendto(sockfd, &uni_port, &uni_add, &msg);
    ^
    In file included from ..\thread.c:9:0:
    ..\unicast_socket_sendto.h:30:6: note: expected 'char' but argument is of type 'char *'
    void unicast_socket_sendto(int sockfd, int port, char address, char data);
    ^
    ..\thread.c:32:54: attention : passing argument 4 of 'unicast_socket_sendto' makes integer from pointer without a cast [-Wint-conversion]
    unicast_socket_sendto(sockfd, &uni_port, &uni_add, &msg);
    ^
    In file included from ..\thread.c:9:0:
    ..\unicast_socket_sendto.h:30:6: note: expected 'char' but argument is of type 'char *'
    void unicast_socket_sendto(int sockfd, int port, char address, char data);
    ^
    ..\thread.c: Dans la fonction 'main':
    ..\thread.c:19:25: attention : initialization makes integer from pointer without a cast [-Wint-conversion]
    #define MESSAGE "Hello"
    ^
    ..\thread.c:43:13: note: in expansion of macro 'MESSAGE'
    char msg = MESSAGE;
    ^
    Merci d'avance pour vos reponse;
    Je suis ouvert à tte les critiques quelles qu'elles soient !

    Best regards,

    beau_g

  2. #2
    Expert éminent

    Femme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 202
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (Île de France)

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

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 202
    Par défaut
    Le premier message est une violente erreur de compilation: un #define avec '\0' comme nom de macro.
    Fais voir tes en-têtes.

    Ce type d'erreur provoque souvent une cascade de problèmes.
    Par la suite, tu as des erreurs de conversion entre pointeurs et entiers, correspondant en général à du "j'ai codé à l'aveugle et je me suis loupé".

  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
    Qu'est ce que tu veux dire par en-tête ?
    Oui je dois avouer que je suis pas tres fier de moi :/ il me manque pas mal de pratique en codage

  4. #4
    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
    J'ai resolu quelques erreurs avec quelques modifications, mais il me reste toujours la 1ere :

    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
    /*
     * thread.c
     *
     *  Created on: 21 juil. 2017
     *      Author: begau
     */
     
    #include "thread.h"
    #include "unicast_socket_sendto.h"
     
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pthread.h>
     
     
    /* this function is run by the second thread */
    void *send_unicast(void *x_void_ptr)
    {
    	/* increment x to 100 */
    	int *x_ptr = (int *)x_void_ptr;
     
    	int sockfd;
    	char addr_uni = "2.5.6.7";
    	int uni_port = 1234;
    	char message = "Hello";
     
    	while(++(*x_ptr) < 100);
    	while (1) {
    		unicast_socket_sendto(sockfd, uni_port, addr_uni, message);
    	    }
     
    	printf("sending finished\n");
    	close(sockfd);
    	/* the function must return something - NULL will do */
    	return NULL;
    }
     
    int main()
    {
    	int x = 0;
     
    	/* this variable is our reference to the second thread */
     
    	pthread_t inc_x_thread;
     
    	/* create a second thread which executes send_unicast(&msg) */
     
    	if(pthread_create(&inc_x_thread, NULL, send_unicast, &x)) {
    		fprintf(stderr, "Error creating thread\n");
    		return 1;
     
    	}
     
    	return 0;
     
    }
    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
    #include "unicast_socket_sendto.h"
     
     
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/time.h> /* gettimeofday */
    #include "app_config.h"
     
     
     
    void unicast_socket_sendto(int sockfd, int port, char address, char data) {
     
    	    struct hostent *he; /* server address info */
    	    struct sockaddr_in their_addr;
    	    int bytesReceived;
    	    socklen_t addr_len = (socklen_t)sizeof (their_addr);
    	    int sock = sockfd;
    	    int uni_port = port;
     
    	    /* resolve server host name or IP address */
    	    if ((he = gethostbyname(&address)) == NULL) {
    	    	perror("Server gethostbyname");
    	    	exit(1);
    	    }
     
    	    printf("Setting up socket Port %d: ", uni_port);
    	    /* Setup the socket */
    	    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    	        perror("Talker socket");
    	        exit(1);
    	    }
    	    printf("OK\n");
     
     
    	    memset(&their_addr, 0, sizeof (their_addr)); /* zero struct */
    	    /* Server details */
    	    their_addr.sin_family = AF_INET; /* host byte order .. */
    	    their_addr.sin_port = htons(uni_port); /* .. short, netwk byte order */
    	    their_addr.sin_addr = *((struct in_addr *) he->h_addr);
     
     
           /* Sends the data to the server. */
           if ((bytesReceived = sendto(sock, &data, sizeof (data), 0,
                (struct sockaddr *)&their_addr, addr_len)) == -1) {
            perror("Server Send Error");
            exit(1);
            }
     
    }
    ERREUR:
    15:51:21 **** Incremental Build of configuration Debug for project test2 ****
    Info: Internal Builder is used for build
    gcc -O0 -g3 -Wall -c -fmessage-length=0 -o thread.o "..\\thread.c"
    In file included from c:\mingw\include\windows.h:42:0,
    from c:\mingw\include\winsock2.h:22,
    from c:\mingw\include\ws2tcpip.h:19,
    from ..\unicast_socket_sendto.h:14,
    from ..\thread.c:9:
    c:\mingw\include\windef.h:66:9: erreur: les noms de macro doivent être des identificateurs
    #define '\0' ((void*)0)
    ^
    ..\thread.c: Dans la fonction 'send_unicast':
    ..\thread.c:25:18: attention : initialization makes integer from pointer without a cast [-Wint-conversion]
    char addr_uni = "2.5.6.7";
    ^
    ..\thread.c:27:17: attention : initialization makes integer from pointer without a cast [-Wint-conversion]
    char message = "Hello";
    ^

    15:51:21 Build Finished (took 343ms)

  5. #5
    Expert éminent

    Femme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 202
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (Île de France)

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

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 202
    Par défaut
    tes fichiers .h
    h est une extension de nom de fichier, choisie pour le mot header ou en-tête en français.

  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
    Ils sont deja affiché il me semble non ?

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

Discussions similaires

  1. fermeture socket dans un thread
    Par adrien1 dans le forum Débuter
    Réponses: 0
    Dernier message: 20/04/2010, 14h45
  2. Probleme de socket dans un thread
    Par nikus dans le forum Threads & Processus
    Réponses: 4
    Dernier message: 25/06/2008, 03h27
  3. problème de socket dans une application win32
    Par deck_bsd dans le forum Windows
    Réponses: 5
    Dernier message: 20/01/2007, 18h32
  4. pb de socket dans une appli MFC
    Par Nelmo dans le forum MFC
    Réponses: 1
    Dernier message: 04/06/2006, 19h02
  5. [MFC] CArchive dans une thread
    Par Kaori dans le forum MFC
    Réponses: 12
    Dernier message: 11/04/2005, 15h26

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