iPhone Network Programming---估计是ios3.0时候的(内功)


iPhone <wbr>Network <wbr>Programmingn myprevious article on iPhone programming, you saw how to consume Webservices from within an iPhone application and how to parse thereturning XML result. While Web services are very common nowadays,the payload needed for consuming Web services is pretty high,especially if you are simply fetching small amounts of data -- theSOAP packet itself takes up quite a number of bytes. A better wayto communicate is to use sockets, where information could be sentand received without the additional XML payload. In addition,socket allows you to maintain a connection to the server, whichallows your application to run asynchronously, and receive incomingdata as and when needed.

In this article, you will learn how to communicate with a serverusing TCP/IP. You will also learn how to build a simple chatapplication using the concepts covered in one of my earlierarticles.

For the sample project discussed in this article, you will useXcode and create a new View-based Application project and name itas Network.

Using Streams for Network Communications

The easiest way to communicate over the network using sockets isto use the NSStream class. The NSStream class is an abstract classrepresenting streams, where you can use it to read and write data.It can be used on memory, files, or networks. Using the NSStreamclass, you can communicate with a server simply by writing data toit and receive data from it by reading from an NSStream object.

On the Mac OS X, you can establish a connection to a server viathe use of an NSHost and NSStream objects, like this:


            NSInputStream *iStream;
            

NSOutputStream *oStream;


            uint portNo = 500;

 NSURL *website = [NSURL URLWithString:urlStr];

 NSHost *host = [NSHost hostWithName:[website host]];

 [NSStream getStreamsToHost:host 
                                  port:portNo 
                           inputStream:&iStream
                          outputStream:&oStream];

As you observed, the NSStream class has a class method named getStreamsToHost:port:inputStream:outputStream:, whichcreates an input stream and an output stream to a server where youcan read and write data to it. However, the problem is that the getStreamsToHost:port:inputStream:outputStream: method isnot supported on the iPhone OS. Hence, the above code will not workin your iPhone application.

To resolve this problem, you can add a category to the existingNSStream class to replace the functionality provided by thegetStreamsToHost:port:inputStream:outputStream: method. Todo so, right-click on the Classes group in Xcode and add a new fileand name it as NSStreamAdditions.m. In theNSStreamAdditions.h file, add in the following:


#import 

@interface NSStream (MyAdditions)

+ (void)getStreamsToHostNamed:(NSString *)hostName 
                         port:(NSInteger)port 
                  inputStream:(NSInputStream **)inputStreamPtr 
                 outputStream:(NSOutputStream **)outputStreamPtr;

@end

In the NSStreamAdditions.m file, add in the following( see Listing 1).

The above code adds the class method namedgetStreamsToHostNamed:port:inputStream:outputStream: tothe NSStream class. You can now use this method in your iPhoneapplication to connect to a server using TCP.

Author's Note: The code for the category outlined hereare based on Apple’s Technical Q&A1652.

In the NetworkViewController.m file, insert thefollowing statements in bold:


#import "NetworkViewController.h"

#import "NSStreamAdditions.h"

@implementation NetworkViewController

NSMutableData *data;

NSInputStream *iStream;
NSOutputStream *oStream;

Define the connectToServerUsingStream:portNo: method sothat you can connect to a server and then create an input andoutput stream objects:

-(void) connectToServerUsingStream:(NSString *)urlStr 
                            portNo: (uint) portNo 

{


    if (![urlStr isEqualToString:@""]) 

{
        NSURL *website = [NSURL URLWithString:urlStr];
        

if (!website) {
            NSLog(@"%@ is not a valid URL");


            return;


        } 

else 

{
            [NSStream getStreamsToHostNamed:urlStr 
                                       port:portNo 
                                inputStream:&iStream
                               outputStream:&oStream]; 

           
            [iStream retain];
            

[oStream retain];
            
            

[iStream setDelegate:self];

 [oStream setDelegate:self];
            
            

[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                               forMode:NSDefaultRunLoopMode];

 [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                               forMode:NSDefaultRunLoopMode];


            
            [oStream open];
            [iStream open];            
        

}


} 

   

}

You scheduled both the input and output streams to receive eventson a run loop. Doing so prevents your code from blocking when thereis no data available on the stream. The delegates for both streamobjects are also set to self as you will implement the method forreceiving data on the stream in this same class.

Using CFNetwork for Network Communication

Another way to establish a connection to a server using TCP isthrough the CFNetwork framework. The CFNetwork is a framework inthe Core Services Framework (C libraries), which providesabstractions for network protocols, such as HTTP, FTP, and BSDsockets.

To see how to use the various classes in the CFNetworkframework, add the following statements in bold to theNetworkViewController.m file:


#import "NetworkViewController.h"


#import "NSStreamAdditions.h"



#import 

@implementation NetworkViewController



NSMutableData *data;


NSInputStream *iStream;


NSOutputStream *oStream;


CFReadStreamRef readStream = NULL;


CFWriteStreamRef writeStream = NULL;

Define the following connectToServerUsingCFStream:portNo:method as follows ( see Listing 2).

You first use the CFStreamCreatePairWithSocketToHost()method to create a readable and writable stream connected to aserver via TCP/IP. This method returns a reference to a readablestream (readStream) and a writable stream (writeStream). They arethen type-casted to their respective equivalent in Objective C --NSInputStream and NSOutputStream. You then do the same as you didpreviously -- set their delegates as well as their run loop.

Sending Data

To send data to a server, you simply use the NSOutputStreamobject, like this:


-(void) writeToServer:(const uint8_t *) buf {
    [oStream write:buf maxLength:strlen((char*)buf)];    
}

The above method writes an array of unsigned integer bytes to theserver.

Reading Data

When data are received from the server, thestream:handleEvent: method will be fired. Hence, you willread all incoming data in this method. Implement this method asshown below:


- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode 

{


    
    switch(eventCode) 

{
        

case NSStreamEventHasBytesAvailable:

 {
            if (data == nil) {
                data = [[NSMutableData alloc] init];
            }


            uint8_t buf[1024];
            unsigned int len = 0;

 len = [(NSInputStream *)stream read:buf maxLength:1024];

 if(len) {    
                [data appendBytes:(const void *)buf length:len];
                int bytesRead;


                bytesRead += len;

 } 

else

 {
                NSLog(@"No data.");
            }


            
            NSString *str = [[NSString alloc] initWithData:data 
                                encoding:NSUTF8StringEncoding];


            NSLog(str);
            

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"From server" 
                                                            message:str 
                                                           delegate:self 
                                                  cancelButtonTitle:@"OK" 
                                                  otherButtonTitles:nil];
            

[alert show];


            [alert release];


                        
            [str release];
            

[data release];

 data = nil;
        

} 

break;
    }
}

This method contains two parameters -- an NSStream instance, and anNSStreamEvent constant. The NSStreamEvent constant can be one ofthe following:
  • NSStreamEventNone -- No event has occurred.
  • NSStreamEventOpenCompleted -- The open has completedsuccessfully.
  • NSStreamEventHasBytesAvailable -- The stream has bytes to beread.
  • NSStreamEventHasSpaceAvailable -- The stream can accept bytesfor writing.
  • NSStreamEventErrorOccurred -- An error has occurred on thestream.
  • NSStreamEventEndEncountered -- The end of the stream has beenreached.
For reading incoming data, you would check for theNSStreamEventHasBytesAva ilable constant. What you did in thismethod was to read from the incoming stream and then display thereceived data using an UIAlertView object.

The stream:handleEvent: method is also a good place tocheck for connection error. For example, if theconnectToServerUsingStream:portNo: method fails to connectto the server, the error will be notified via thestream:handleEvent: method, with the NSStreamEventconstant set to NSStreamEventErrorOccurred.


Disconnecting

To disconnect from the server, define the disconnect method asshown below:


-(void) disconnect 

{
    

[iStream close];


    [oStream close];


}

iPhone <wbr>Network <wbr>Programming
advertisement
Also, add in the following lines in bold to the dealloc method:

- (void)dealloc {


[self disconnect];
    
    

[iStream release];


    [oStream release];
    
    

if (readStream) 

CFRelease(readStream);


    if (writeStream) CFRelease(writeStream);
        
    

[super dealloc];
}

Testing the Application


Figure 1.Populate: Populate the View window with views.
 
Figure 2.Verify: Verify the connections on the File’s Owneritem.
 
Figure 3.View: Add more views.

You are now ready to put all the pieces together and test thecode against a server. In the NetworkViewController.hfile, declare the following outlet and action:


#import 

@interface NetworkViewController : UIViewController {
    IBOutlet UITextField *txtMessage;
}



@property (nonatomic, retain) UITextField *txtMessage;


-(IBAction) btnSend: (id) sender;

@end

Double-click on the NetworkViewController.xib file to editit in Interface Builder. In the View window, populate it with thefollowing views ( see also Figure 1):
  • Text Field
  • Round Rect Button
Perform the following actions:
  • Control-click on the File’s Owner item and drag and drop itover the Text Field view. Select txtMessage.
  • Control-click on the Round Rect Button view and drag and dropit over the File’s Owner item. Select btnSend:.
Right-click on the File’s Owner item to verify its connections (see Figure 2).

Back in the NetworkViewController.m file, add thefollowing lines of code in bold to the viewDidLoadmethod:


- (void)viewDidLoad {
[self connectToServerUsingStream:@"192.168.1.102" portNo:500];

 //---OR---
    //[self connectToServerUsingCFStream:@"192.168.1.102" portNo:500];
    
    [super viewDidLoad];
}

The above code assumes you are connecting to a server of IP address192.168.1.102, at port 500. You will see how to write the servercode shortly.

Implement the btnSend: method as follows:


-(IBAction) btnSend: (id) sender 

{
    const uint8_t *str = 
        (uint8_t *) [txtMessage.text cStringUsingEncoding:NSASCIIStringEncoding];


    [self writeToServer:str];
    txtMessage.text = @"";
}

Release the txtMessage outlet in the dealloc method:

- (void)dealloc 

{
[txtMessage release];

 [self disconnect];    
    

[iStream release];


    [oStream release];

 if (readStream) CFRelease(readStream);


if (writeStream) CFRelease(writeStream);
        
    

[super dealloc];
}

Building the Server

Up till this point, you have built a client on the iPhone thatis ready to send some text over to a server. So what about theserver? To test this application, I have built a very simpleconsole server using C#. Here is the code for theProgram.cs file (see Listing 3).

The server program performs the following:

  • It assumes that the IP address of the server is 192.168.1.102.When testing on your end, replace this IP address with the IPaddress of your computer running this server application.
  • It sends back to the client whatever data it receives.
  • Once the data is received, the server no longer listens forincoming data. In order for the client to send data to it again,the client needs to reconnect to the server.
Author's Note: Download the accompanying source code forall the examples illustrated in this article.
 
Figure 4. Change isgood: Change the font size of the Text View view.

With the server code explained, you can now test your iPhoneapplication. Type some text into the Text Field view and click theSend button. If the connection is established, you should see theAlert View displaying the data received.

A More Interesting Example

In one of my earlier articles for DevX.com, I wrote about how tobuild your own instant messenger application (see “Home-brew Your OwnInstant Messenger App with Visual Studio .NET">using .NET. In that article, I wrote about how to write a serverand a client application to allow chat messages to be sent betweenmultiple users. Using the code in that article, you could actuallymodify your iPhone application as a chat client and communicatewith other users on other platforms.

Using the same project created earlier, add the following viewsto the View window (see also Figure 3):

  • Label
  • Text Field
  • Round Rect Button
  • Text View
Select the Text View, press Command-T and change its font size to 9(see Figure 4).

In the NetworkViewController.h file, add the followingstatements in bold:


#import 

@interface NetworkViewController : UIViewController {

 IBOutlet UITextField *txtMessage; 

   
    IBOutlet UITextField *txtNickName;
    

IBOutlet UITextView  *txtMessages;


}



@property (nonatomic, retain) UITextField *txtMessage;


@property (nonatomic, retain) UITextField *txtNickName;


@property (nonatomic, retain) UITextView *txtMessages;


-(IBAction) btnSend:(id) sender; 
 
Figure 5. Connections:Verify the connections.
-(IBAction) btnLogin:(id) sender;

@end
Perform the following actions:
  • Control-click on the File’s Owner item and drag and drop itover the top Text Field view. Select txtNickName.
  • Control-click on the File’s Owner item and drag and drop itover the top Text View view. Select txtMessages.
  • Control-click on the Round Rect Button view and drag and dropit over the File’s Owner item. Select btnLogin:.
Right-click on the File’s Owner item to verify its connections (see Figure 5). In the NetworkViewController.m file, addthe following statements in bold (see Listing 4).
 
Figure 6. Chat: Startchatting on your iPhone.
That’s all! Press Command-R to test the application. First, enter anickname for yourself and tap the Login button (see Figure6). You can now start chatting by typing a message and tappingthe Send button.
Author's Note: For your convenience, I have provided theC# version of the chat server in the download package for thisarticle.

Summary

In this article, you have seen how to communicate with anotherserver using TCP/IP. Knowing how to communicate with the outsideworld allows you to build very interesting applications. In thenext article, I will talk about Bluetooth programming on theiPhone.

iPhone <wbr>Network <wbr>Programming



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值