2011-05-31 37 views
1

我爲客戶端服務器編程做了以下編程,但它不起作用。服務器無法接收連接setup.plz幫助請求。iphone:客戶端 - 服務器通信不發生

#import "clientserverprogramViewController.h" 
#import "secondview.h" 
#import <CoreFoundation/CFSocket.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 

NSInputStream *iStream; 
NSOutputStream *oStream; 

@implementation clientserverprogramViewController 

@synthesize name,filepath,display; 

-(IBAction) print { 
    NSString *urlStr = serverIP;] 
    [display setText : urlStr]; 

    if (![urlStr isEqualToString:@""]) { 
     NSURL *website = [NSURL URLWithString:urlStr]; 

     if (!website) { 
      NSLog(@"%@ is not a valid URL"); 
      return; 
     } 
     NSHost *host = [NSHost hostWithName:[website host]]; 

     [NSStream getStreamsToHost:host port:3000 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]; 

     [iStream open]; 
     [oStream open]; 
    } 
} 

-(IBAction) settings { 
    [self presentModalViewController:nextview animated: YES]; 
} 


-(IBAction) cancel { 
    exit(0); 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

@end 
+5

朋友,這是你的第25 question..You應該學會如何格式化您的問題.. – Krishnabhadra 2011-05-31 06:51:55

+1

....什麼工作,什麼不,什麼錯誤消息,等等,等等,等等..... – KevinDTimm 2011-05-31 07:11:18

回答

1

是否有您所使用流的具體原因是什麼?

使用NSURLConnection怎麼樣?這是我的一個項目的一段代碼。兩者都在KANetworkManager中。 KANetworkTransactionType只是一個枚舉,它幫助我知道如何解析響應。

+ (void) createAndStartUrlConnection:(NSMutableURLRequest *)request type:(KANetworkTransactionType)type target:(id)target callback:(SEL)callback; 
{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    NSDictionary *requestDict = [NSDictionary dictionaryWithObjectsAndKeys:request, @"request", [NSNumber numberWithInt:type], @"type", target, @"target", [NSValue valueWithPointer:callback], @"callback", nil]; 

    [KANetworkManager performSelectorInBackground:@selector(makeNetworkCall:) withObject:requestDict]; 
} 

我能夠進行同步網絡調用,因爲我總是在自己的線程上調用此方法。這是實現異步網絡通信而不涉及委託的簡單方法(儘管委託方法提供了一些優點)。您的parseResponse方法需要針對您發回的任何Web服務。 parseResponse會通知回調方法。如果您對此有其他疑問,請告知我。

+ (void) makeNetworkCall:(NSDictionary *)params 
{ 
    // We assume this method won't be called from the main thread, so we need our own NSAutoreleasePool. 
    NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init]; 

    NSMutableURLRequest *request = [params objectForKey:@"request"]; 
    KANetworkTransactionType type = [(NSNumber *)[params objectForKey:@"type"] intValue]; 
    id target = [params objectForKey:@"target"]; 
    SEL callback = (SEL)[[params objectForKey:@"callback"] pointerValue]; 

    NSURLResponse *response; 
    NSError *err; 
    // We make a synchronous request assuming we're on a background thread. 
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 
    if (data.length > 0) 
    {   
     [self parseResponse:data type:type target:target callback:callback]; 
    } 
    else 
    { 
     NSLog(@"Error occured during network call. %@", err); 
    } 

    [autoreleasePool drain]; 
} 
2

你只打開流,不要對它們做任何事情。這就像拿起電話而不撥號碼。使用NSStreamDelegate協議來實現數據傳輸代碼。

更新

你有這些線是流設置委託:

[iStream setDelegate:self]; 
[oStream setDelegate:self]; 

現在實施NSStreamDelegate協議中定義你自己的類方法(據我所知 - 這裏只有一個他們)。瞭解如何從那裏接收/發送數據。

+0

@ eimantas如何使用nsstreamdelegate和在哪裏使用它。 ima begginer,並沒有太多的知識,可以üplz建議我一個套接字編程的書在目標c中,其中編程與例 – 2011-06-02 10:57:50

+1

如果你是新來的Objective C和可可,我建議學習它的設計模式而不是直接跳入像網絡編程這樣複雜的話題。代表團是可可中最常用的設計模式之一。我相信你會找到一些例子,如果你只是嘗試谷歌。 – Eimantas 2011-06-02 11:08:18

+0

@ Eimantas是上面給出的能夠與服務器進行套接字連接的程序。 – 2011-06-02 11:27:18

0

JB門,

在你的代碼告知istream和ostream的,你clientserverprogramViewController目的是爲每一個代表。但是,一個合適的代表需要實際執行。你的類需要實現此方法:

– stream:handleEvent:

的細節記錄在這裏:

而且,您的代碼將無法在實際工作蘋果手機。有一個更新的Core Foundation API用於創建套接字對,details here

更新

只是想知道這是不是一個軟件問題,但也許服務器位於防火牆後面。請詳細說明一下服務器,Web服務器,即,netcat的,或簡單的TCP套接字等

彼得

+0

@ peter T爲什麼它不能在真正的手機上工作 – 2011-06-07 11:02:16

+0

@ Junior Bill gates [806116](http://stackoverflow.com/questions/806116/is-there-anything-like-getstreamstohost-on-real-iphone-device )描述瞭如何使用'CFStreamCreatePairWithSocketToHost'而不是'getStreamsToHost'。 – 2011-07-21 20:49:05