IOS Network Programming Topic: The Use of reachability

Source: Internet
Author: User

What is The difference between UDP and TCP?
TCP is all called Transmission Control protocol. This protocol can provide connection-oriented, reliable, point-to-point communication.
The UDP full name is the User Datagram Protocol, which can provide non-connected unreliable point-to-multipoint communication.
With TCP or UDP, it depends on which aspect your program focuses on. Reliable or fast?

The process of establishing a connection with TCP/IP
In the TCP/IP protocol, the TCP protocol provides a reliable connection service with a three-time handshake to establish a connection.
First handshake: When the connection is established, the client sends a connection request to the server and enters the Syn_send state, waiting for the server to confirm;
Second handshake: The server receives a client connection request and sends an allow connection reply to the client, at which point the server enters the SYN_RECV state;
Third handshake: The client receives the server's allow connection reply, sends the acknowledgement to the server, the client and the server enter the communication state, and completes three handshake.

(the so-called three-time handshake is the Send/receive process that has three connection information .) The establishment of a TCP connection requires the sending/receiving of three connection information . )


A: Confirm the network environment 3g/wifi

1. Adding source files and the framework

When developing network applications such as the Web, it is necessary to confirm the network environment, connection situation and other information. If they are not processed, they will not be examined by Apple (our).
The method of acquiring/detecting network state is described in Apple's routine reachability. To use reachability in an application program, first complete the following two parts:

1.1. Add the source file:
Use reachability in your program to copy only the Reachability.h and reachability.m in the routine to your project. Such as:



1.2. Add the framework:
Add systemconfiguration.framework into the project. Such as:


2. Network status

Three network states are defined in Reachability.h:
typedef enum {
notreachable = 0,//no connection
Reachableviawifi,//Use 3G/GPRS Network
Reachableviawwan//Use WiFi network
} networkstatus;

So you can check network status like this:

1reachability *r =[reachability reachabilitywithhostname:@ "www.apple.com"];2     Switch([R Currentreachabilitystatus]) {3              Casenotreachable:4                     //No network connection5                      Break;6              CaseReachableviawwan:7                     //using 3G Networks8                      Break;9              CaseReachableviawifi:Ten                     //Use WiFi network One                      Break; A}

3. Check the current network environment
When the program starts, if you want to detect the available network environment, you can

1 //whether WiFi2+(BOOL) Isenablewifi {3         return([[Reachability Reachabilityforlocalwifi] currentreachabilitystatus]! =notreachable);4     }5 6     //is 3G7+(BOOL) isenable3g {8         return([[Reachability reachabilityforinternetconnection] currentreachabilitystatus]! =notreachable);9     }Ten  /// examples  :    One- (void) Viewwillappear: (BOOL) Animated { A     if([reachability Reachabilityforinternetconnection].currentreachabilitystatus = = notreachable) && -([reachability Reachabilityforlocalwifi].currentreachabilitystatus = =notreachable)) { -Self.navigationItem.hidesBackButton =YES; the [Self.navigationitem Setleftbarbuttonitem:nil animated:no]; -         } -}

4. Real-time notification of link status
Real-time check of network connection status, notification is also very necessary in the network application. When the continuation status changes, you need to notify the user in a timely manner:

1Reachability1. Version 52     //my.appdelegate.h3     #import "Reachability.h"4 5     @interfaceMyappdelegate:nsobject <UIApplicationDelegate> {6 networkstatus remotehoststatus;7     }8 9 @property networkstatus remotehoststatus;Ten  One     @end A  -     //MY.APPDELEGATE.M -     #import "MyAppDelegate.h" the  -     @implementationmyappdelegate -     @synthesizeRemotehoststatus; -  +     //Update network Status -- (void) UpdateStatus { +Self.remotehoststatus =[[reachability sharedreachability] remotehoststatus]; A     } at  -     //Notification Network Status -- (void) reachabilitychanged: (Nsnotification *) Note { - [self updatestatus]; -         if(Self.remotehoststatus = =notreachable) { -Uialertview *alert = [[Uialertview alloc] Initwithtitle:nslocalizedstring (@"AppName", nil) inMessage:nslocalizedstring (@"notreachable", nil) -                         Delegate: Nil Cancelbuttontitle:@"OK"Otherbuttontitles:nil]; to [alert show]; + [alert release]; -         } the     } *  $     //program launcher to start network monitoringPanax Notoginseng- (void) Applicationdidfinishlaunching: (UIApplication *) Application { -      the         //set up a site for network detection +[[Reachability sharedreachability] SetHostName:@"www.apple.com"]; A [[reachability sharedreachability] setnetworkstatusnotificationsenabled:yes]; the         //to set the notification function when the network state changes + [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (reachabilitychanged:) -Name@"knetworkreachabilitychangednotification" Object: nil]; $ [self updatestatus]; $     } -  -- (void) Dealloc { the         //Delete a Notification object - [[Nsnotificationcenter Defaultcenter] removeobserver:self];Wuyi [window release]; the [Super Dealloc]; -     }  Wu      -Reachability2. version 0 About      $  -     //MyAppDelegate.h -     @classreachability; -  A         @interfaceMyappdelegate:nsobject <UIApplicationDelegate> { +reachability *Hostreach; the         } -  $     @end the  the     //MYAPPDELEGATE.M the- (void) reachabilitychanged: (Nsnotification *) Note { thereachability* Curreach = [NoteObject]; -Nsparameterassert ([Curreach iskindofclass: [reachabilityclass]]); inNetworkStatus status =[Curreach currentreachabilitystatus]; the      the         if(Status = =notreachable) { AboutUialertview *alert = [[Uialertview alloc] Initwithtitle:@"AppName "" the message:@"Notreachable" the                               Delegate: Nil theCancelbuttontitle:@"YES"Otherbuttontitles:nil]; + [alert show]; - [alert release]; the         }Bayi     } the                                the- (void) Applicationdidfinishlaunching: (UIApplication *) Application { -         // ... -                    the         //monitoring network conditions the [[Nsnotificationcenter Defaultcenter] addobserver:self the selector: @selector (reachabilitychanged:) the name:kreachabilitychangednotification -                               Object: nil]; theHostreach = [[reachability reachabilitywithhostname:@"www.google.com"] retain]; the Hostreach Startnotifer]; the         // ...94}

IOS Network Programming Topic: The Use of reachability

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.