iPhone網路編程初體驗聊天程式執行個體開發

來源:互聯網
上載者:User

iPhone網路編程初體驗聊天程式執行個體開發是本文要介紹的內容,講解了如何?聊天程式的案例,不多說,先來看內容。首先使用Xcode常見一個基於視圖(View)的應用程式項目,取名Network。

使用網路通訊流

使用通訊端在網路上通訊最簡單的方法是使用NSStream類,NSStream類是一個表示流的抽象類別,你可以使用它讀寫資料,它可以用在記憶體、檔案或網路上。使用NSStream類,你可以向伺服器寫資料,也可以從伺服器讀取資料。

在Mac OS X上,可以使用NSHost和NSStream對象建立到伺服器的串連,如:

 
  1. NSInputStream *iStream;  
  2.             NSOutputStream *oStream;  
  3.             uint portNo = 500;  
  4.             NSURL *website = [NSURL URLWithString:urlStr];  
  5.             NSHost *host = [NSHost hostWithName:[website host]];  
  6.             [NSStream getStreamsToHost:host  
  7.                                   port:portNo  
  8.                            inputStream:&iStream  
  9.                           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檔案中,增加下面的代碼:

 
  1. #import  
  2. @interface NSStream (MyAdditions)  
  3. + (void)getStreamsToHostNamed:(NSString *)hostName  
  4.                          port:(NSInteger)port  
  5.                   inputStream:(NSInputStream **)inputStreamPtr  
  6.                  outputStream:(NSOutputStream **)outputStreamPtr;  
  7. @end 

在NSStreamAdditions檔案中加入以下代碼:

 
  1. #import "NSStreamAdditions.h"  
  2. @implementation NSStream (MyAdditions)  
  3. + (void)getStreamsToHostNamed:(NSString *)hostName  
  4.                           port:(NSInteger)port  
  5.                    inputStream:(NSInputStream **)inputStreamPtr  
  6.                   outputStream:(NSOutputStream **)outputStreamPtr  
  7.  {  
  8.      CFReadStreamRef     readStream;  
  9.      CFWriteStreamRef    writeStream;  
  10.      assert(hostName != nil);  
  11.      assert( (port > 0) && (port < 65536) );  
  12.      assert( (inputStreamPtr != NULL) || (outputStreamPtr != NULL) );  
  13.      readStream = NULL;  
  14.      writeStream = NULL;  
  15.      CFStreamCreatePairWithSocketToHost(  
  16.                                         NULL,  
  17.                                         (CFStringRef) hostName,  
  18.                                         port,  
  19.                                         ((inputStreamPtr  != nil) ? &readStream : NULL),  
  20.                                         ((outputStreamPtr != nil) ? &writeStream : NULL)  
  21.                                         );  
  22.          if (inputStreamPtr != NULL) {  
  23.         *inputStreamPtr  = [NSMakeCollectable(readStream) autorelease];  
  24.      }  
  25.      if (outputStreamPtr != NULL) {  
  26.          *outputStreamPtr = [NSMakeCollectable(writeStream) autorelease];  
  27.      }  
  28.  }  
  29.  @end 

上面的代碼為NSStream類增加了一個 getStreamsToHostNamed:port:inputStream:outputStream:方法,現在你可以在你的iPhone應用程式中使用這個方法,使用TCP協議串連到伺服器。

在NetworkViewController.m檔案中,插入下面的代碼:

 
  1. #import "NetworkViewController.h"  
  2. #import "NSStreamAdditions.h"  
  3. @implementation NetworkViewController  
  4. NSMutableData *data;  
  5. NSInputStream *iStream;  
  6. NSOutputStream *oStream; 

定義 connectToServerUsingStream:portNo:方法,以便串連到伺服器,然後建立輸入和輸出資料流對象:

 
  1. -(void) connectToServerUsingStream:(NSString *)urlStr  
  2.                             portNo: (uint) portNo {  
  3.     if (![urlStr isEqualToString:@""]) {  
  4.         NSURL *website = [NSURL URLWithString:urlStr];  
  5.         if (!website) {  
  6.             NSLog(@"%@ is not a valid URL");  
  7.             return;  
  8.         } else {  
  9.             [NSStream getStreamsToHostNamed:urlStr  
  10.                                        port:portNo  
  11.                                 inputStream:&iStream  
  12.                                outputStream:&oStream];              
  13.             [iStream retain];  
  14.             [oStream retain];  
  15.             [iStream setDelegate:self];  
  16.             [oStream setDelegate:self];  
  17.               
  18.             [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]   forMode:NSDefaultRunLoopMode];  
  19.             [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]   forMode:NSDefaultRunLoopMode];  
  20.             [oStream open];  
  21.             [iStream open];              
  22.         }  
  23. }      

在一個運 行迴圈中,你可以調度輸入和輸出資料流接收事件,這樣可以避免阻塞。

小結:iPhone網路編程初體驗聊天程式執行個體開發的內容介紹完了,希望本文對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.