Ping en C
Résolu/Fermé
jfs
anasre - 28 déc. 2008 à 17:39
- Messages postés
- 21
- Date d'inscription
- mardi 24 octobre 2000
- Statut
- Membre
- Dernière intervention
- 8 juin 2005
anasre - 28 déc. 2008 à 17:39
15 réponses
jfs
1 mai 2005 à 23:22
- Messages postés
- 21
- Date d'inscription
- mardi 24 octobre 2000
- Statut
- Membre
- Dernière intervention
- 8 juin 2005
1 mai 2005 à 23:22
et comment faire cela ? Y a t il unmoyen simple ?
Merci.
Merci.
Luffy =)
1 mai 2005 à 23:39
- Messages postés
- 365
- Date d'inscription
- mercredi 20 avril 2005
- Statut
- Membre
- Dernière intervention
- 19 mai 2006
1 mai 2005 à 23:39
bon en fait je viens de tester la fonction system(), et elle renvoie une valeur correspondant à la réussite ou non de la commande passée en paramètre. Exemple avec du code :
là je ping une mauvaise adresse, et le retour est 1.
là le retour = 0 donc ça a marché.
donc dans ton code, tu inclues un test sur le retour de system()
if (ret==0)
{
//tu écris dans ton fichier
}
++
int _tmain(int argc, _TCHAR* argv[]) { printf("Luffy's Ping Test\n"); int ret=system("ping 127.0.0.0"); printf("\nResultat : %d\n\n",ret); system("pause"); return 0; }
là je ping une mauvaise adresse, et le retour est 1.
int _tmain(int argc, _TCHAR* argv[]) { printf("Luffy's Ping Test\n"); int ret=system("ping 127.0.0.1"); printf("\nResultat : %d\n\n",ret); system("pause"); return 0; }
là le retour = 0 donc ça a marché.
donc dans ton code, tu inclues un test sur le retour de system()
if (ret==0)
{
//tu écris dans ton fichier
}
++
Un tout grand merci à toi. Finalement j'y suis arrivé (du moins 1 partie). Voici le code :
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <conio.h>
#define p 512
int main()
{
FILE *fichier;
FILE *fichier2;
char n;
char commande [p] ;
fichier = fopen ("fichier.txt","r");
fichier2 = fopen ("fichier2.txt", "w");
fscanf(fichier, "%s\n",&n);
int atoi(char *n);
strcpy(commande, "ping ");
strcat(commande, &n);
int ret = system (commande);
if (ret==1)
{
fprintf (fichier2, "%s\n", &n);
}
system ("pause");
return 0;
Maintenant c'est au moment ou je lui dis de lire les 2 adr qu'il merde :
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <conio.h>
#define p 512
int main()
{
FILE *fichier;
FILE *fichier2;
char n, v;
char commande [p] ;
fichier = fopen ("fichier.txt","r");
fichier2 = fopen ("fichier2.txt", "w");
fscanf(fichier, "%s\n%s\n",&n,&v);
int atoi(char *n);
int atoi(char *v);
strcpy(commande, "ping ");
strcat(commande, &n);
int ret = system (commande);
if (ret==1)
{
fprintf (fichier2, "%s\n", &n);
}
strcpy(commande, "ping ");
strcat(commande, &v);
int ret = system (commande);
if (ret==1)
{
fprintf (fichier2, "%s\n", &v);
}
system ("pause");
return 0;
}
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <conio.h>
#define p 512
int main()
{
FILE *fichier;
FILE *fichier2;
char n;
char commande [p] ;
fichier = fopen ("fichier.txt","r");
fichier2 = fopen ("fichier2.txt", "w");
fscanf(fichier, "%s\n",&n);
int atoi(char *n);
strcpy(commande, "ping ");
strcat(commande, &n);
int ret = system (commande);
if (ret==1)
{
fprintf (fichier2, "%s\n", &n);
}
system ("pause");
return 0;
Maintenant c'est au moment ou je lui dis de lire les 2 adr qu'il merde :
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <conio.h>
#define p 512
int main()
{
FILE *fichier;
FILE *fichier2;
char n, v;
char commande [p] ;
fichier = fopen ("fichier.txt","r");
fichier2 = fopen ("fichier2.txt", "w");
fscanf(fichier, "%s\n%s\n",&n,&v);
int atoi(char *n);
int atoi(char *v);
strcpy(commande, "ping ");
strcat(commande, &n);
int ret = system (commande);
if (ret==1)
{
fprintf (fichier2, "%s\n", &n);
}
strcpy(commande, "ping ");
strcat(commande, &v);
int ret = system (commande);
if (ret==1)
{
fprintf (fichier2, "%s\n", &v);
}
system ("pause");
return 0;
}
faire des ping avec system(ping) je trouve ca tres bof !!
ne vaudrait-il mieu pas monter une socket icmp et faire un connect() ??
un peu comme ça :
[code]
int chknet(char * ip)
{
struct sockaddr_in name;
struct hostent * hent;
int sock;
int retour = 0;
/* creaion de la socket en icmp */
if(!(sock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP))) return (-1);
memset(&name,0,sizeof(struct sockaddr_in));
name.sin_family = AF_INET;
name.sin_addr.s_addr = inet_addr(ip);
if(connect(sock,(struct sockaddr *) &name,sizeof(struct sockaddr))==0) retour = 1;
else retour = 0;
shutdown(sock,SHUT_RDWR);
close(sock);
return(retour);
[/code]
ne vaudrait-il mieu pas monter une socket icmp et faire un connect() ??
un peu comme ça :
[code]
int chknet(char * ip)
{
struct sockaddr_in name;
struct hostent * hent;
int sock;
int retour = 0;
/* creaion de la socket en icmp */
if(!(sock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP))) return (-1);
memset(&name,0,sizeof(struct sockaddr_in));
name.sin_family = AF_INET;
name.sin_addr.s_addr = inet_addr(ip);
if(connect(sock,(struct sockaddr *) &name,sizeof(struct sockaddr))==0) retour = 1;
else retour = 0;
shutdown(sock,SHUT_RDWR);
close(sock);
return(retour);
[/code]
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
Luffy =)
1 mai 2005 à 21:35
- Messages postés
- 365
- Date d'inscription
- mercredi 20 avril 2005
- Statut
- Membre
- Dernière intervention
- 19 mai 2006
1 mai 2005 à 21:35
Ben le problème avec la fonction system() c'est que tu n'as pas de retour à propement dit de ce que tu as lancé, que ce soit un ping ou autre.
Donc en gros, si tu es motivé, tu crées carrément une socket, tu fais ton ping toi-même et tu répères ce qui revient sur la socket.
Donc en gros, si tu es motivé, tu crées carrément une socket, tu fais ton ping toi-même et tu répères ce qui revient sur la socket.
Sorry mais j'ai encore un petit probl :
quand j'essaie de compiler, il me dit qu'il y a une erreur au niveau du int _tmain(int argc, _TCHAR* argv[])
Encore merci.
quand j'essaie de compiler, il me dit qu'il y a une erreur au niveau du int _tmain(int argc, _TCHAR* argv[])
Encore merci.
Luffy =)
2 mai 2005 à 09:17
- Messages postés
- 365
- Date d'inscription
- mercredi 20 avril 2005
- Statut
- Membre
- Dernière intervention
- 19 mai 2006
2 mai 2005 à 09:17
Mets simplement un :
sans arguments.
int main()
sans arguments.
je suis vraiment désolé mais il me dit à la compil "cannot convert int to char*" Je suis vraiment desole mais je débute ...Voici le code au cas où :
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define p 512
#define ping "ping"
int main()
{
FILE *fichier;
FILE *fichier2;
char n[250];
char commande [p] ;
fichier = fopen ("fichier.txt","r");
fscanf(fichier, "%s\n",&n);
int atoi(char *n);
strcpy(commande, "ping ");
strcat(commande, n);
system(commande);
if (n==1)
{
fprintf(fichier2, "%s\n",&n);
}
fclose (fichier);
fclose (fichier2);
exit (0);
}
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define p 512
#define ping "ping"
int main()
{
FILE *fichier;
FILE *fichier2;
char n[250];
char commande [p] ;
fichier = fopen ("fichier.txt","r");
fscanf(fichier, "%s\n",&n);
int atoi(char *n);
strcpy(commande, "ping ");
strcat(commande, n);
system(commande);
if (n==1)
{
fprintf(fichier2, "%s\n",&n);
}
fclose (fichier);
fclose (fichier2);
exit (0);
}
Luffy =)
2 mai 2005 à 11:25
- Messages postés
- 365
- Date d'inscription
- mercredi 20 avril 2005
- Statut
- Membre
- Dernière intervention
- 19 mai 2006
2 mai 2005 à 11:25
il te le met à quelle ligne ?
Luffy =)
2 mai 2005 à 12:33
- Messages postés
- 365
- Date d'inscription
- mercredi 20 avril 2005
- Statut
- Membre
- Dernière intervention
- 19 mai 2006
2 mai 2005 à 12:33
Alors plusieurs petites remarques :
tu n'as pas mis de nom variables, donc il ne doit pas compilé. De plus en C, il faut savoir que les variables sont toujours à déclarer au tout début de la fonction. Les nouveaux compilo acceptent, mais dans la norme, c'est mieux de les déclarer et de les itinitialiser avant toute chose.
Ensuite tu déclares 2 fois la variable "int ret", donc po possible, pas 2 variables dans une fonction qui portent le même nom.
sinon je trouve ton code très lisible et propre ! j'aime bien le
int atoi(char *n);
tu n'as pas mis de nom variables, donc il ne doit pas compilé. De plus en C, il faut savoir que les variables sont toujours à déclarer au tout début de la fonction. Les nouveaux compilo acceptent, mais dans la norme, c'est mieux de les déclarer et de les itinitialiser avant toute chose.
Ensuite tu déclares 2 fois la variable "int ret", donc po possible, pas 2 variables dans une fonction qui portent le même nom.
sinon je trouve ton code très lisible et propre ! j'aime bien le
#define p 512 char commande [p] ;
Rebonjour,
Merci pour ton compliment ;-)) Je t'avoue que c'est mon 1er progr ...d'où toutes mes questions.
Je te remercie encore pour tout ce que tu m'as indiqué.
Si je te suis bien, je ne peux pas utiliser 2 fois la variable int ret. Je peux te demander de regarder encore ceci stp ? :
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <conio.h>
#define p 512
int main()
{
FILE *fichier;
FILE *fichier2;
char n, v;
char commande [p] ;
fichier = fopen ("fichier.txt","r");
fichier2 = fopen ("fichier2.txt", "w");
fscanf(fichier, "%s\n%s\n",&n, &v);
int atoi(char *n);
int atoi(char *v);
strcpy(commande, "ping ");
strcat(commande, &n);
int ret = system (commande);
if (ret==1)
{
fprintf (fichier2, "%s\n", &n);
}
strcpy(commande, "ping ");
strcat(commande, &v);
int ret2 = system (commande);
if (ret2==1)
{
fprintf (fichier2, "%s\n", &v);
}
system ("pause");
return 0;
}
Merci pour ton compliment ;-)) Je t'avoue que c'est mon 1er progr ...d'où toutes mes questions.
Je te remercie encore pour tout ce que tu m'as indiqué.
Si je te suis bien, je ne peux pas utiliser 2 fois la variable int ret. Je peux te demander de regarder encore ceci stp ? :
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <conio.h>
#define p 512
int main()
{
FILE *fichier;
FILE *fichier2;
char n, v;
char commande [p] ;
fichier = fopen ("fichier.txt","r");
fichier2 = fopen ("fichier2.txt", "w");
fscanf(fichier, "%s\n%s\n",&n, &v);
int atoi(char *n);
int atoi(char *v);
strcpy(commande, "ping ");
strcat(commande, &n);
int ret = system (commande);
if (ret==1)
{
fprintf (fichier2, "%s\n", &n);
}
strcpy(commande, "ping ");
strcat(commande, &v);
int ret2 = system (commande);
if (ret2==1)
{
fprintf (fichier2, "%s\n", &v);
}
system ("pause");
return 0;
}
Luffy =)
2 mai 2005 à 13:59
- Messages postés
- 365
- Date d'inscription
- mercredi 20 avril 2005
- Statut
- Membre
- Dernière intervention
- 19 mai 2006
2 mai 2005 à 13:59
Tu n'as pas écouté tout ce que je t'ai dit !!! lol ;-)
Tu peux utiliser la même variable 2 fois, mais il faut la déclarer qu'une seule fois :
quand tu écris "int ret" on dit que tu déclares ta variable. De la mémoire est allouée pour pouvoir contenir une donnée. donc quand tu refais ret=1; le processeur met la valeur 1 dans la case mémoire réservée pour ret.
et il faut que tu déclares tes variables au tout début de ta fonction, et en leur mettant une valeur nulle pour éviter des erreurs :
et une question, à quoi te sert :
?
Tu peux utiliser la même variable 2 fois, mais il faut la déclarer qu'une seule fois :
int ret=0; ret=1;
quand tu écris "int ret" on dit que tu déclares ta variable. De la mémoire est allouée pour pouvoir contenir une donnée. donc quand tu refais ret=1; le processeur met la valeur 1 dans la case mémoire réservée pour ret.
et il faut que tu déclares tes variables au tout début de ta fonction, et en leur mettant une valeur nulle pour éviter des erreurs :
#include <winsock.h> #include <stdio.h> #include <stdlib.h> #include <stdio.h> #include <signal.h> #include <conio.h> #define p 512 int main() { FILE *fichier=NULL; FILE *fichier2=NULL; char n, v; char commande [p]=NULL ; int ret=0; fichier = fopen ("fichier.txt","r"); fichier2 = fopen ("fichier2.txt", "w"); fscanf(fichier, "%s\n%s\n",&n, &v); int atoi(char *n); int atoi(char *v); strcpy(commande, "ping "); strcat(commande, &n); ret = system (commande); if (ret==1) { fprintf (fichier2, "%s\n", &n); } strcpy(commande, "ping "); strcat(commande, &v); ret = system (commande); if (ret==1) { fprintf (fichier2, "%s\n", &v); } system ("pause"); return 0; }
et une question, à quoi te sert :
int atoi(char *n); int atoi(char *v);
?
Merci :-)
En fait, voici commetn j'ai fait :
#include <stdio.h>
#include <winsock.h>
#include <stdlib.h>
#include <stdio.h>
#define p 512
int main()
{
FILE *fichier;
FILE *fichier2;
char n[250], v[250];
char commande [p] ;
fichier = fopen ("fichier.txt","rt");
fichier2 = fopen ("fichier2.txt", "wt");
printf("Test Ping ResULB\n");
fscanf(fichier, "%s\n%s\n",&n, &v);
int atoi(char *n);
int atoi(char *v);
strcpy(commande, "ping ");
strcat(commande, n);
int ret = system (commande);
if (ret==1)
{
fprintf (fichier2, "%s\n", &n);
}
strcpy(commande, "ping ");
strcat(commande, v);
int ret2 = system (commande);
if (ret2==1)
{
fprintf (fichier2, "%s\n", &v);
}
fclose (fichier);
fclose (fichier2);
system ("pause");
return 0;
}
En fait, voici commetn j'ai fait :
#include <stdio.h>
#include <winsock.h>
#include <stdlib.h>
#include <stdio.h>
#define p 512
int main()
{
FILE *fichier;
FILE *fichier2;
char n[250], v[250];
char commande [p] ;
fichier = fopen ("fichier.txt","rt");
fichier2 = fopen ("fichier2.txt", "wt");
printf("Test Ping ResULB\n");
fscanf(fichier, "%s\n%s\n",&n, &v);
int atoi(char *n);
int atoi(char *v);
strcpy(commande, "ping ");
strcat(commande, n);
int ret = system (commande);
if (ret==1)
{
fprintf (fichier2, "%s\n", &n);
}
strcpy(commande, "ping ");
strcat(commande, v);
int ret2 = system (commande);
if (ret2==1)
{
fprintf (fichier2, "%s\n", &v);
}
fclose (fichier);
fclose (fichier2);
system ("pause");
return 0;
}
Bonjour à tous,
je viens de tomer sur votre discussion et j'ai le même problème que jfs sauf que je dévellope sur un microprocesseur, il faut donc que je crée ma commande ping en entier avec des commandes ICMP.
Savez vous comment fonctionne la commande ping ? J'ai vu que l'ICMP pouvait envoyer des instruction avec des requêtes et des commandes mais concrètement quand on ping une adresse, quel info est envoyée et qu'est-ce qui est répondu ?
Merci d'avance
je viens de tomer sur votre discussion et j'ai le même problème que jfs sauf que je dévellope sur un microprocesseur, il faut donc que je crée ma commande ping en entier avec des commandes ICMP.
Savez vous comment fonctionne la commande ping ? J'ai vu que l'ICMP pouvait envoyer des instruction avec des requêtes et des commandes mais concrètement quand on ping une adresse, quel info est envoyée et qu'est-ce qui est répondu ?
Merci d'avance