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
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 #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); } }
Mais il y a beaucoup d'erreurs ; qui se ressemblent plus ou moins; et j'ai beaucoup de mal à les corriger.
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; }
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
Merci d'avance pour vos reponse;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;
^
Je suis ouvert à tte les critiques quelles qu'elles soient !
Best regards,
beau_g
Partager