3xc4l18ur question = ( to ) ? be : ! be; | Bah voila,
Cette année j'ai un prof de merde en C, du coup je galere un peu a m'y retrouver.
Pourriez vous m'aider? Je souhaite recupère le resultat de mon thread mais ca marche pas je sais pas pourquoi...
Code :
- /* gcc -Wall -ainsi -pedantic -o scanTCP scanTCP.c -lpthread */
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <string.h>
- #include <netinet/in.h>
- #include <unistd.h>
- struct Data
- {
- int socket_service;
- struct sockaddr_in adr;
- };
- void* testConnexion(void* data)
- {
- struct Data sock = *((struct Data*) data);
- char* resultat = "";
- /* Connexion au serveur */
- if ((connect (sock.socket_service,(struct sockaddr *) &sock.adr, sizeof(struct sockaddr_in))) == -1)
- resultat = "FERME";
- else
- resultat = "OUVERT";
-
- pthread_exit((void*) resultat);
- }
- main (int argc, char** argv)
- {
- if (argc > 2)
- {
- struct Data sockData;
- struct hostent* host;
- int thread, port;
- char* mach_srv = "";
- char** reponse;
- mach_srv = argv[1];
- port = atoi(argv[2]);
-
- if ((sockData.socket_service = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
- {
- perror("Creation socket impossible\n" );
- exit(1);
- }
-
- if ((host = gethostbyname(mach_srv)) == NULL)
- {
- perror ("Nom de l'hote inconnu\n" );
- exit(1);
- }
-
- /* configuration de l'adresse */
- bcopy(host->h_addr, &sockData.adr.sin_addr, host->h_length); /* bcopy(src, dest, lg) */
- sockData.adr.sin_family = AF_INET;
- sockData.adr.sin_port = htons(port);
-
- /*
- Creation d'un nouveau thread s'excutant simultanment avec
- son pere. Le nouveau thread execute la fonction "testConnexion"
- avec l'argument sockData. La variable thread contient a present
- le descripteur du thread.
- */
- pthread_create ((pthread_t *) &thread, NULL, testConnexion, (void *) &sockData);
- /*erreur*/
- /* attente du fils du thread et recup de sa valeur retour */
- pthread_join ( thread, (void**) reponse);
-
- printf("Le port %d est %s", port, (char*) *reponse);
-
- exit(0);
- }
- else
- {
- printf ("Vous devez taper qqch du genre :\n ./scanTCP 127.0.0.1 21\n" );
- exit(-1);
- }
- }
|
Bien sur je suis pas un pro du C, alors je suis pas l'abri d'une erreur a la con.
PS: Si je créé un thread comme ca, le pere peut mourrir en sachant que son fils lui survivra ?
Code :
- /* threads */
- /* initialisation des attribut du thread */
- pthread_attr_init(&attr);
- /* detachement du fils et du pere */
- pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED );
- /* creation du thread avec ses attributs */
- nb = NB_PACKET;
- pthread_create ((pthread_t *) &thread, &attr, envoitICMP, (void *) &nb);
|
Message édité par !cricri le 25-11-2004 à 10:58:04
|