Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1457 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  [C/Reseau]resolution d'IP => compil... mais pkoi? pliz c pour demain:/

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[C/Reseau]resolution d'IP => compil... mais pkoi? pliz c pour demain:/

n°235294
Tetedeienc​h
Head Of God
Posté le 26-10-2002 à 23:10:53  profilanswer
 

Bonjour,
 
J'ai un probleme en C.
 
je dois faire une appli reseau ou je dois recuperer une IP sous forme de chaine.
 
Arguments : en 1) le nom a resoudre.
 
Je declare donc une structure de type hostent :
 
struct hostent *he;
 
puis je l'initialise :
 
  if ((he=gethostbyname(argv[1])) == NULL) {  // get the host info
    perror("gethostbyname" );
    exit(1);
  }
 
Rien de bien fulgurant.
 
Ensuite je l'affiche via :
 
printf("dest IP %s, port %d\n", inet_ntoa(*((struct in_addr *)he->h_addr)), MYPORT);  
 
Encore une fois, rien de fulgurant.
 
Quelqu'un peux alors m'expliquer pourquoi, quoique je mette la dedans, il m'affiche l'IP de ma becane, et pas l'IP liee au dns de la becane passe en parametre ???
 
merci...


Message édité par Tetedeiench le 28-10-2002 à 18:55:27

---------------
L'ingénieur chipset nortiaux : Une iFricandelle svp ! "Spa du pâté, hin!" ©®Janfynette | "La plus grosse collec vivante de bans abusifs sur pattes" | OCCT v12 OUT !
mood
Publicité
Posté le 26-10-2002 à 23:10:53  profilanswer
 

n°235295
Tetedeienc​h
Head Of God
Posté le 26-10-2002 à 23:17:45  profilanswer
 

style :
 
./resolv www.hardware.fr  
IP 129.15.xx.1xx  
 
=> La mienne.


---------------
L'ingénieur chipset nortiaux : Une iFricandelle svp ! "Spa du pâté, hin!" ©®Janfynette | "La plus grosse collec vivante de bans abusifs sur pattes" | OCCT v12 OUT !
n°235308
Tetedeienc​h
Head Of God
Posté le 26-10-2002 à 23:34:25  profilanswer
 

Voila l'integralite du programme :
 

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <netdb.h>
  11. #include "mysocket.h"
  12. #define MYPORT 7898    // the port users will be connecting to
  13. int main(int argc, char* argv[])
  14. {
  15.   int sockfd;
  16.   struct sockaddr_in their_addr; // connector's address information
  17.   struct hostent *he;
  18.   int numbytes;
  19.   packet pack;
  20.   struct hostent *me;
  21.   char myhostname[255];
  22.   if (argc != 3) {
  23.     fprintf(stderr,"usage: talker hostname message\n" );
  24.     exit(1);
  25.   }
  26.    
  27.   sockfd = openChannel(MYPORT); 
  28.   if ((he=gethostbyname(argv[1])) == NULL) {  // get the host info
  29.     perror("gethostbyname" );
  30.     exit(1);
  31.   }
  32.    
  33.   mySend(sockfd, inet_ntoa(*((struct in_addr *)he->h_addr)), MYPORT, argv[2]);
  34.   printf("dest IP %s, port %d\n", inet_ntoa(*((struct in_addr *)he->h_addr)), MYPORT);
  35.   printf("packet contains \"%s\"\n",pack.data);
  36.  
  37.   close(sockfd);
  38.  
  39.   return 0;
  40. }


 
il envoie une chaine en plus dans le packet, et utilise une lib.
 
Je suis vraiment curieux de savoir le pourquoi du comment...


---------------
L'ingénieur chipset nortiaux : Une iFricandelle svp ! "Spa du pâté, hin!" ©®Janfynette | "La plus grosse collec vivante de bans abusifs sur pattes" | OCCT v12 OUT !
n°235314
lorill
Posté le 26-10-2002 à 23:37:43  profilanswer
 

www.chezmoicamarche.com
 
t'as du foirer la config de ton dns...

n°235324
Tetedeienc​h
Head Of God
Posté le 26-10-2002 à 23:40:48  profilanswer
 

lorill a écrit a écrit :

www.chezmoicamarche.com
 
t'as du foirer la config de ton dns...




 
le probleme se rencontre :
 
-Sur les machines de la fac ( essaye 3 machines sun solaris differentes).
 
-Sur ma passerelle linux debian
 
C pour ca que je capte pas.
 
Par contre, le code surce provenant de la bas ( que j'ai fait qu'adapter a des fins de test ) :
 
http://www.ecst.csuchico.edu/~beej [...] erver.html
 
(le dernier, en bas)  
 
marche parfaitement.
 
Comprends pas.
 


---------------
L'ingénieur chipset nortiaux : Une iFricandelle svp ! "Spa du pâté, hin!" ©®Janfynette | "La plus grosse collec vivante de bans abusifs sur pattes" | OCCT v12 OUT !
n°235327
lorill
Posté le 26-10-2002 à 23:41:45  profilanswer
 

Tetedeiench a écrit a écrit :

 




 
ben j'ai copié collé ton code, viré tout ce que tu as défini dans mysocket.h, et c'est tout.

n°235331
Tetedeienc​h
Head Of God
Posté le 26-10-2002 à 23:43:03  profilanswer
 

lorill a écrit a écrit :

 
 
ben j'ai copié collé ton code, viré tout ce que tu as défini dans mysocket.h, et c'est tout.




 
Ben merde :o
 
je vire aussi et je teste...


---------------
L'ingénieur chipset nortiaux : Une iFricandelle svp ! "Spa du pâté, hin!" ©®Janfynette | "La plus grosse collec vivante de bans abusifs sur pattes" | OCCT v12 OUT !
n°235334
Tetedeienc​h
Head Of God
Posté le 26-10-2002 à 23:45:03  profilanswer
 

OK ca doit venir de ma facon de compiler alors, car moi aussi ca marche en compilant direct le fichier...
 
Je fais :
 
gcc -c mysocket.c
gcc -o talker talker.c mysocket.o -lsocket -lnsl
 
...
 
stoo :o


---------------
L'ingénieur chipset nortiaux : Une iFricandelle svp ! "Spa du pâté, hin!" ©®Janfynette | "La plus grosse collec vivante de bans abusifs sur pattes" | OCCT v12 OUT !
n°235338
Tetedeienc​h
Head Of God
Posté le 26-10-2002 à 23:48:21  profilanswer
 

oups, vla reellement ce que je fais :
 
g++ -c mysocket.c  
g++ -o talker talker.c mysocket.o -lsocket -lnsl  
 
Pour la simple raison qu'on est oblige d'utiliser g++... :(


---------------
L'ingénieur chipset nortiaux : Une iFricandelle svp ! "Spa du pâté, hin!" ©®Janfynette | "La plus grosse collec vivante de bans abusifs sur pattes" | OCCT v12 OUT !
n°235482
Je@nb
Kindly give dime
Posté le 27-10-2002 à 10:42:07  profilanswer
 

ta pensé aux IPv6 dans ton prog ?  :love:

mood
Publicité
Posté le 27-10-2002 à 10:42:07  profilanswer
 

n°235933
Tetedeienc​h
Head Of God
Posté le 28-10-2002 à 15:03:56  profilanswer
 

non, mais j'aimerai bien comprendre le pourquoi du comment :cry:


---------------
L'ingénieur chipset nortiaux : Une iFricandelle svp ! "Spa du pâté, hin!" ©®Janfynette | "La plus grosse collec vivante de bans abusifs sur pattes" | OCCT v12 OUT !
n°236128
Tetedeienc​h
Head Of God
Posté le 28-10-2002 à 18:54:36  profilanswer
 

Up, j'ai vraiment besoin de la reponse :/
 
Voila le code source :
 

Code :
  1. /*
  2. ** talker.c -- a datagram "client" demo
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <arpa/inet.h>
  13. #include <netdb.h>
  14. #include "mysocket.h"
  15. #define MYPORT 7898    // the port users will be connecting to
  16. int main(int argc, char* argv[])
  17. {
  18.   int sockfd;
  19.   struct sockaddr_in their_addr; // connector's address information
  20.   struct hostent *he;
  21.   if (argc != 3) {
  22.     fprintf(stderr,"usage: talker hostname message\n" );
  23.     exit(1);
  24.   }
  25.   if ((he=gethostbyname(argv[1])) == NULL) {  // get the host info
  26.     perror("gethostbyname" );
  27.     exit(1);
  28.   }
  29.   sockfd = openChannel(MYPORT);
  30.   mySend(sockfd, inet_ntoa(*((struct in_addr *)he->h_addr)), MYPORT, argv[2]);
  31.   printf("dest IP %s, port %d\n", inet_ntoa(*((struct in_addr *)he->h_addr)), MYPORT);
  32.   printf("packet contains \"%s\"\n",pack.data);
  33.   close(sockfd);
  34.   return 0;
  35. }


 
Et la librairie :
 

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <netdb.h>
  11. #define MAXBUFLEN 8000
  12. #define TRANSPORTPORT 2222
  13. typedef  struct{
  14.   char src_IP[50];
  15.   int src_port;
  16.   char dst_IP[50];
  17.   int dst_port;
  18.   char data[5000];
  19. }packet;
  20. int openChannel(int portNumber)
  21. {
  22.   int sockfd;
  23.   struct sockaddr_in my_addr;
  24.   if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  25.     perror("socket" );
  26.     exit(1);
  27.   }
  28.   my_addr.sin_family = AF_INET;         // host byte order
  29.   my_addr.sin_port = htons(portNumber);     // short, network byte order
  30.   my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
  31.   memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
  32.   //  printf("port a binder : %d\n" + portNumber);
  33.   if (bind(sockfd, (struct sockaddr *)&my_addr,
  34.            sizeof(struct sockaddr)) == -1) {
  35.     perror("bind" );
  36.     exit(1);
  37.   }
  38.   return sockfd;
  39. }
  40. void mySend(int sockFD, char *destinationIPAddress, int portNumber, char *data)
  41. {
  42.   struct hostent *me;
  43.   struct sockaddr_in my_addr;
  44.   struct sockaddr_in their_addr;
  45.   char myhostname[255];
  46.   packet pack;
  47.   int MYPORT;
  48.   int socklength;
  49.   socklength = sizeof(struct sockaddr_in);
  50.   getsockname(sockFD,(struct sockaddr *) &my_addr, &socklength);
  51.   MYPORT = my_addr.sin_port;
  52.   gethostname(myhostname,255);
  53.   if ((me=gethostbyname(myhostname)) == NULL) {
  54.     perror("gethostbyname" );
  55.     exit(1);
  56.   }
  57.   strcpy(pack.data, data);
  58.   strcpy(pack.src_IP, inet_ntoa(*((struct in_addr *)me->h_addr)));
  59.   pack.src_port = MYPORT;
  60.   strcpy(pack.dst_IP, destinationIPAddress);
  61.   pack.dst_port = portNumber;
  62.   their_addr.sin_family = AF_INET;     // host byte order
  63.   their_addr.sin_port = htons(TRANSPORTPORT); // short, network byte order
  64.   their_addr.sin_addr = *((struct in_addr *)me->h_addr);
  65.   memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
  66.   if ((sendto(sockFD, &pack, sizeof(packet), 0,
  67.               (struct sockaddr *)&their_addr, sizeof(struct sockaddr))
  68.        ) == -1) {
  69.     perror("sendto" );
  70.     exit(1);
  71.   }
  72.   return;
  73. }
  74. void myRecv(int sockFD, char *sourceIPAddress, int portNumber, char *data)
  75. {
  76.   int addr_len;
  77.   packet pack;
  78.   struct sockaddr_in their_addr;
  79.   addr_len = sizeof(struct sockaddr);
  80.   if ((recvfrom(sockFD ,&pack , MAXBUFLEN-1, 0,
  81.                (struct sockaddr *)&their_addr, &addr_len)) == -1) {
  82.     perror("recvfrom" );
  83.     exit(1);
  84.   }
  85.   strcpy(data, pack.data);
  86.   return;
  87. }


 
Merci :cry:


---------------
L'ingénieur chipset nortiaux : Une iFricandelle svp ! "Spa du pâté, hin!" ©®Janfynette | "La plus grosse collec vivante de bans abusifs sur pattes" | OCCT v12 OUT !
n°236246
Tetedeienc​h
Head Of God
Posté le 28-10-2002 à 22:12:57  profilanswer
 

please :/


---------------
L'ingénieur chipset nortiaux : Une iFricandelle svp ! "Spa du pâté, hin!" ©®Janfynette | "La plus grosse collec vivante de bans abusifs sur pattes" | OCCT v12 OUT !
n°236275
gatorette
Posté le 28-10-2002 à 22:45:10  profilanswer
 

Bizarre...
 
Juste une idée comme ça : Est-ce que la structure hostent est bien remplie après le gethostbyname ? C'est à dire qu'il faudrait voir ce que contient la variable h_addr. Voici un petit programme pour s'assurer que ce qu'elle contient est bon :

Code :
  1. int main( int argc, char* argv[] )
  2. {
  3. hostent *he = gethostbyname( argv[1] );
  4. printf( "hostname: %s\naddress: %d.%d.%d.%d\n", argv[1], ((unsigned char *)he->h_addr)[0], ((unsigned char *)he->h_addr)[1], ((unsigned char *)he->h_addr)[2], ((unsigned char *)he->h_addr)[3] );
  5. return 0;
  6. }


---------------
each day I don't die is cheating
n°236383
Tetedeienc​h
Head Of God
Posté le 29-10-2002 à 03:45:58  profilanswer
 

gatorette a écrit a écrit :

Bizarre...
 
Juste une idée comme ça : Est-ce que la structure hostent est bien remplie après le gethostbyname ? C'est à dire qu'il faudrait voir ce que contient la variable h_addr. Voici un petit programme pour s'assurer que ce qu'elle contient est bon :

Code :
  1. int main( int argc, char* argv[] )
  2. {
  3. hostent *he = gethostbyname( argv[1] );
  4. printf( "hostname: %s\naddress: %d.%d.%d.%d\n", argv[1], ((unsigned char *)he->h_addr)[0], ((unsigned char *)he->h_addr)[1], ((unsigned char *)he->h_addr)[2], ((unsigned char *)he->h_addr)[3] );
  5. return 0;
  6. }






 
Merci, mais j'ai trouve le souci...
 
En fait, je crois que la fonction gesthostbyname assigne une structure globale, et donc renvoie tjs le meme pointeur ( un truc du genre).
 
Car avant je rpenais la struct renvoyee et je mettais mon IP dedans.
 
Puis j'appelais une seconde fois la fonction.
 
Now je le fais plus, ou du moins je copie la valeur de la struct renvoyee dans une autre struct et ca marche...
 
trop zarbi :/

n°236388
gatorette
Posté le 29-10-2002 à 08:17:30  profilanswer
 

Ok, bonne observation... C'est vrai que c'est quelque chose que j'avais jamais remarqué.
 
Une petite note de Microsoft concernant ce point (désolé je développe sous Windows) :
 

Citation :

[nom]MSDN a écrit[/nom]The application must never attempt to modify this structure or to free any of its components. Furthermore, only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other Windows Sockets function calls.

(url = http://msdn.microsoft.com/library/en-us/winsock/winsock/gethostbyname_2.asp)
 
--
Edit: correction de la citation...
Edit2: ...qui ne fonctionne toujours pas (bug ?, protection ?)


Message édité par gatorette le 29-10-2002 à 08:21:19

---------------
each day I don't die is cheating
n°236668
Tetedeienc​h
Head Of God
Posté le 29-10-2002 à 15:46:52  profilanswer
 

gatorette a écrit a écrit :

Ok, bonne observation... C'est vrai que c'est quelque chose que j'avais jamais remarqué.
 
Une petite note de Microsoft concernant ce point (désolé je développe sous Windows) :
 

Citation :

[nom]MSDN a écrit[/nom]The application must never attempt to modify this structure or to free any of its components. Furthermore, only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other Windows Sockets function calls.

(url = http://msdn.microsoft.com/library/en-us/winsock/winsock/gethostbyname_2.asp)
 
--
Edit: correction de la citation...
Edit2: ...qui ne fonctionne toujours pas (bug ?, protection ?)




 
Merci ENORMEMENT, ca explique BEAUCOUP de choses :jap:
 
Pour la citation, n'oublie pas que celle s'appelant quote utilise pas de nom, et celle nommee citation supporte la balise [nom] ;)
 
Edite mon message si tu veux comprendre ;)
 

Citation :

test


 

Test a ecrit a écrit :

 test2



Message édité par Tetedeiench le 29-10-2002 à 15:49:35

---------------
L'ingénieur chipset nortiaux : Une iFricandelle svp ! "Spa du pâté, hin!" ©®Janfynette | "La plus grosse collec vivante de bans abusifs sur pattes" | OCCT v12 OUT !
n°236890
gatorette
Posté le 29-10-2002 à 22:07:25  profilanswer
 

Merci pour l'explication sur la citation... encore quelque chose que j'avais pas remarqué !


---------------
each day I don't die is cheating

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  [C/Reseau]resolution d'IP => compil... mais pkoi? pliz c pour demain:/

 

Sujets relatifs
[C++] Erreur à la compil , help svpRendre disponible une macro sur plusieur machine d'un meme reseau
[JS] Explication du pkoi et du commentpkoi ca marche pas?
[Java] Applet, reseau et repaint() : probleme[C/Reseau] Comment savoir sur quel port tourne un socket ?
Access en réseauinterface graphique, gestion souris,gestion imprimante reseau
[C/Reseau] Petite explication niveau sockets please...[HTML][PHP]pkoi les champs <INPUT type= text>ne prend pas les espaces
Plus de sujets relatifs à : [C/Reseau]resolution d'IP => compil... mais pkoi? pliz c pour demain:/


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR