Comment puis-je ping à un port serveur à partir d'Objective C (iPhone)? ..ping vers un port de serveur à partir de l'iPhone?
Répondre
Tout d'abord vous shoud regarder cette question
How to write a simple Ping method in Cocoa/Objective-C
puis
http://en.wikipedia.org/wiki/Internet_Control_Message_Protocol
http://www.kernelthread.com/projects/hanoi/html/icmp.html
http://www.programmersheaven.com/download/2417/download.aspx
Et je trouve ce code ..
How to write a simple Ping method in Cocoa/Objective-C
* Start
* Kontakt
How to write a simple Ping method in Cocoa/Objective-C
Question: How to write a simple Ping method in Cocoa/Objective-C
I need to write a simple ping method in Cocoa/Objective-C. It also needs to work on the iPhone. I found an example that uses icmp , will this work on the iPhone? I'm leaning towards a solution using NSNetServices , is this a good idea? The method only needs to ping a few times and return the average and -1 if the host is down or unreachable.
avatar rjstelling
Autor: rjstelling
Utworzono: 28 Apr 2009
Language: question language en
Answers:
1. The code below seems to be working synchronously: const char *hostName = [@"stackoverflow.com" cStringUsingEncoding:NSASCIIStringEncoding]; SCNetworkConnectionFlags flags = 0; if (SCNetworkCheckReachabilityByName(hostName, &flags) && flags > 0) { NSLog(@"Host is reachable: %d", flags); } else { NSLog(@"Host is unreachable"); } Note: SystemConfiguration.framework is required
2. Let me try this again...this time logging in, and formatting better ;-) StreamSCNetworkCheckReachabilityByName is deprecated and NOT available for the iPhone. bool success = false; const char *host_name = [@"stackoverflow.com" cStringUsingEncoding:NSASCIIStringEncoding]; SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name); SCNetworkReachabilityFlags flags; success = SCNetworkReachabilityGetFlags(reachability, &flags); bool isAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired); if (isAvailable) { NSLog(@"Host is reachable: %d", flags); }else{ NSLog(@"Host is unreachable"); } Note: SystemConfiguration.framework is required
3. Look into CFHost and in particular CFHostGetReachability . There is sample CFHost code available, as well, which includes a routine to check host availability.
4. You are not missing anything -- "Reachability" doesn't actually test that the target domain is in fact reachable, it only assesses if there is a pathway out of the machine by which the target domain is potentially reachable. So long as you have some outbound connection (e.g., an active wirless or wired connection), and a routing configuration that leads to the target, then the site is "reachable" as far as SCNetworkReachability is concerned.
5. i tested with IP address but it not works properly: result is always YES! is it possibile that do not exist a simple echo with timeout class?
6. Hi, Please take note that there is an difference between the simulator and the actual iPhone. The simulator is not a true simulator like the one supplied by Android, it uses Mac OSX classes for most of the functions. This is particularly hell if there is a difference between the Mac OSX and iPhonew(for example the keychain).
7. The answer Gene Myers posted works using "SCNetworkReachabilityCreateWithName" for me - but only in the simulator. On my device (iPod w/OS 2.2.1) it always returns "Host is reachable" even for nonsense addresses like "zzz". Am I misunderstanding something? Thanks. Here's my code just in case: From https://stackoverflow.com/questions/798454/how-to-write-a-simple-ping-method-in-cocoa-objective-c - (IBAction) TestReachability:(id)sender { bool success = false; const char *host_name = [ipAddressText.textcStringUsingEncoding:NSASCIIStringEncoding]; NSString *imageConnectionSuccess = @"Connected.png"; NSString *imageConnectionFailed = @"NotConnected.png"; SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name); SCNetworkReachabilityFlags flags; success = SCNetworkReachabilityGetFlags(reachability, &flags); bool isAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired); if (isAvailable) { NSLog([NSString stringWithFormat: @"'%s' is reachable, flags: %x", host_name, flags]); [imageView setImage: [UIImage imageNamed:imageConnectionSuccess]]; } else { NSLog([NSString stringWithFormat: @"'%s' is not reachable", host_name]); [imageView setImage: [UIImage imageNamed:imageConnectionFailed]]; } }
8. Pinging on the iPhone works a bit different than on other platforms, due to the fact that you don't have root access. See this sample code from Apple.
réponse simple ..
Utilisez le programme SimplePing pour Mac OS ..
Remplacer le FrameWork CFNetwork pour MAC en cela à l'iPhone CFNetwork FrameWork .....
et exécuter le programme ....
Rappelez-vous, démarrer le projet que l'iPhone, puis Copier SimplePing.h .m et copier les codes de votre MAin View Controller ....
Remplacer cette principale fonction main.m (ci-dessous) (code bootom StartPinging)
enter code here
int main(int argc, char* argv[])
{
#pragma unused(argc)
#pragma unused(argv)
NSAutoreleasePool * pool;
Main * mainObj;
pool = [[NSAutoreleasePool alloc] init];
assert(pool != nil);
if (argc == 2) {
mainObj = [[[Main alloc] init] autorelease];
assert(mainObj != nil);
[mainObj runWithHostName:[NSString stringWithUTF8String:"www.google.com"]];
} else {
fprintf(stderr, "usage: %s host\n", getprogname());
}
[pool drain];
// On success we loop forever in -runWithHostName:, so the only way to
// get here is if something went wrong.
return EXIT_FAILURE;
}
-(void) startPinging
{
NSAutoreleasePool * pool;
Main * mainObj;
pool = [[NSAutoreleasePool alloc] init];
assert(pool != nil);
mainObj = [[[Main alloc] init] autorelease];
assert(mainObj != nil);
[mainObj runWithHostName:[NSString stringWithUTF8String:"www.google.com"]];
}
[pool drain];
// On success we loop forever in -runWithHostName:, so the only way to
// get here is if something went wrong.
}
Connectez cette méthode startpinging à votre bouton ..
J'ai eu ce même problème, et a fini par écrire une simple enveloppe autour SimplePing à réaliser ceci, a écrit un blog à ce sujet et il y a tellement moi le code sur github, j'espère aidera quelqu'un ici:
http://splinter.com.au/how-to-ping-a-server-in-objective-c-iphone
- 1. Ajouter un port à un ping
- 2. Redirection d'un serveur (port) vers un autre
- 3. Fonctionnalité de ping sur le port série
- 4. Puis-je faire un ping vers mon PC à partir de mon mobile et vice versa?
- 5. Réponse à Ping dans le serveur Node.JS
- 6. ping sur un serveur via un port spécifique (fsockopen) dans php
- 7. Comment effectuer un ping à partir d'un package SSIS?
- 8. Télécharger l'image vers un serveur à partir de PictureBox
- 9. Envoi d'un ping vers le serveur pour une connectivité externe
- 10. Noeud JS: Serveur ping et client UDP
- 11. Comment faire un ping sur un serveur à partir d'une application iOS?
- 12. Je veux envoyer un ping à l'emulater à partir du serveur via LAN ou WIFI
- 13. Pas de ping à une adresse de multidiffusion à partir de Windows
- 14. Téléchargement de fichiers à partir de .exe vers le serveur
- 15. ICMP/PING "Serveur" dans Ruby
- 16. Port Python virtualenv vers un autre système
- 17. Socket du serveur: Pas de port Renvoi
- 18. comment faire un ping d'une URL à partir du serveur SQL 2008 stocké proc
- 19. Ping Tomcat à partir d'une autre machine
- 20. Migration vers un cpanel propre à partir d'un serveur piraté
- 21. Test de connectivité de serveur WCF Push. Ping()?
- 22. Impossible de transférer des fichiers vers un serveur proftpd à partir d'une adresse IP externe
- 23. Ping un site à ruby?
- 24. Comment Ping dans Linux Shell à partir de Windows
- 25. Rediriger la demande de sous-domaine vers un port spécifique
- 26. PHP - Ping un autre fichier PHP sur un serveur distant
- 27. MQTT recv à partir d'un ping publication et mqtt C
- 28. Commande Android Ping meurt sur un serveur inaccessible
- 29. Si un port UDP client envoie un pointeur à un serveur sur un port, puis-je supposer qu'il enverra du même port s'il envoie vers un port différent sur le serveur?
- 30. Créer une vue dans un serveur à partir d'une requête vers un serveur différent