iPhone網路編程初體驗聊天程式執行個體開發是本文要介紹的內容,講解了如何?聊天程式的案例,不多說,先來看內容。首先使用Xcode常見一個基於視圖(View)的應用程式項目,取名Network。
使用網路通訊流
使用通訊端在網路上通訊最簡單的方法是使用NSStream類,NSStream類是一個表示流的抽象類別,你可以使用它讀寫資料,它可以用在記憶體、檔案或網路上。使用NSStream類,你可以向伺服器寫資料,也可以從伺服器讀取資料。
在Mac OS X上,可以使用NSHost和NSStream對象建立到伺服器的串連,如:
- 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];
NSStream類有一個方法getStreamsToHost:port:inputStream:outputStream:,它建立一個到伺服器的輸入和輸出資料流,但問題是iPhone OS不支援getStreamsToHost:port:inputStream:outputStream:方法,因此上面的代碼在iPhone應用程式中是不能啟動並執行。
為瞭解決這個問題,你可以增加一個類別到現有的NSStream類上,替換 getStreamsToHost:port:inputStream:outputStream:方法提供的功能。在Xcode的Classes上點擊右鍵,添加一個檔案NSStreamAdditions.m,在NSStreamAdditions.h檔案中,增加下面的代碼:
- #import
- @interface NSStream (MyAdditions)
- + (void)getStreamsToHostNamed:(NSString *)hostName
- port:(NSInteger)port
- inputStream:(NSInputStream **)inputStreamPtr
- outputStream:(NSOutputStream **)outputStreamPtr;
- @end
在NSStreamAdditions檔案中加入以下代碼:
- #import "NSStreamAdditions.h"
- @implementation NSStream (MyAdditions)
- + (void)getStreamsToHostNamed:(NSString *)hostName
- port:(NSInteger)port
- inputStream:(NSInputStream **)inputStreamPtr
- outputStream:(NSOutputStream **)outputStreamPtr
- {
- CFReadStreamRef readStream;
- CFWriteStreamRef writeStream;
- assert(hostName != nil);
- assert( (port > 0) && (port < 65536) );
- assert( (inputStreamPtr != NULL) || (outputStreamPtr != NULL) );
- readStream = NULL;
- writeStream = NULL;
- CFStreamCreatePairWithSocketToHost(
- NULL,
- (CFStringRef) hostName,
- port,
- ((inputStreamPtr != nil) ? &readStream : NULL),
- ((outputStreamPtr != nil) ? &writeStream : NULL)
- );
- if (inputStreamPtr != NULL) {
- *inputStreamPtr = [NSMakeCollectable(readStream) autorelease];
- }
- if (outputStreamPtr != NULL) {
- *outputStreamPtr = [NSMakeCollectable(writeStream) autorelease];
- }
- }
- @end
上面的代碼為NSStream類增加了一個 getStreamsToHostNamed:port:inputStream:outputStream:方法,現在你可以在你的iPhone應用程式中使用這個方法,使用TCP協議串連到伺服器。
在NetworkViewController.m檔案中,插入下面的代碼:
- #import "NetworkViewController.h"
- #import "NSStreamAdditions.h"
- @implementation NetworkViewController
- NSMutableData *data;
- NSInputStream *iStream;
- NSOutputStream *oStream;
定義 connectToServerUsingStream:portNo:方法,以便串連到伺服器,然後建立輸入和輸出資料流對象:
- -(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];
- }
- }
- }
在一個運 行迴圈中,你可以調度輸入和輸出資料流接收事件,這樣可以避免阻塞。
小結:iPhone網路編程初體驗聊天程式執行個體開發的內容介紹完了,希望本文對你有所協助!