自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone網(wǎng)絡(luò)編程初體驗聊天程序?qū)嵗_發(fā)

移動開發(fā) iOS
本文介紹的是iPhone網(wǎng)絡(luò)編程初體驗聊天程序?qū)嵗_發(fā),主要是實現(xiàn)聊天程序的一個實例,先來看詳細(xì)內(nèi)容。

iPhone網(wǎng)絡(luò)編程初體驗聊天程序實例開發(fā)是本文要介紹的內(nèi)容,講解了如何實現(xiàn)聊天程序的案例,不多說,先來看內(nèi)容。首先使用Xcode常見一個基于視圖(View)的應(yīng)用程序項目,取名Network。

使用網(wǎng)絡(luò)通信流

使用套接字在網(wǎng)絡(luò)上通信最簡單的方法是使用NSStream類,NSStream類是一個表示流的抽象類,你可以使用它讀寫數(shù)據(jù),它可以用在內(nèi)存、文件或網(wǎng)絡(luò)上。使用NSStream類,你可以向服務(wù)器寫數(shù)據(jù),也可以從服務(wù)器讀取數(shù)據(jù)。

在Mac OS X上,可以使用NSHost和NSStream對象建立到服務(wù)器的連接,如:

  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:,它創(chuàng)建一個到服務(wù)器的輸入和輸出流,但問題是iPhone OS不支持getStreamsToHost:port:inputStream:outputStream:方法,因此上面的代碼在iPhone應(yīng)用程序中是不能運行的。

為了解決這個問題,你可以增加一個類別到現(xiàn)有的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:方法,現(xiàn)在你可以在你的iPhone應(yīng)用程序中使用這個方法,使用TCP協(xié)議連接到服務(wù)器。

在NetworkViewController.m文件中,插入下面的代碼:

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

定義 connectToServerUsingStream:portNo:方法,以便連接到服務(wù)器,然后創(chuàng)建輸入和輸出流對象:

  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. }      

在一個運 行循環(huán)中,你可以調(diào)度輸入和輸出流接收事件,這樣可以避免阻塞。

小結(jié):iPhone網(wǎng)絡(luò)編程初體驗聊天程序實例開發(fā)的內(nèi)容介紹完了,希望本文對你有所幫助!

責(zé)任編輯:zhaolei
相關(guān)推薦

2011-06-07 17:35:39

iphone 多線程

2016-09-27 16:38:24

JavaScript微信Web

2021-08-12 14:33:20

Python多線程編程

2011-06-20 13:23:03

Qt Quick QML

2020-12-01 15:37:07

Python

2010-08-01 16:11:53

Android

2011-07-26 11:13:15

iPhone PXL

2010-05-23 10:29:29

Widget開發(fā)

2009-08-01 09:06:35

UbuntuOneLinux開源操作系統(tǒng)

2009-03-09 15:12:39

XenServer安裝

2012-05-16 10:50:17

Windows Pho

2011-07-25 18:02:51

iPhone LibFetion 移植

2018-03-22 15:36:26

程序員RubyiOS

2011-07-18 15:32:14

iPhone 錄音 播放

2009-06-23 10:06:03

2011-08-08 16:56:44

iPhone 字符處理 視圖

2010-11-22 10:31:17

Sencha touc

2011-05-30 15:12:10

App Invento 初體驗

2023-07-15 08:01:38

2017-10-31 08:19:04

iPhone
點贊
收藏

51CTO技術(shù)棧公眾號