2013-04-28 30 views
0

我試圖創建TCP連接按照該鏈接 http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-serveriphone tcp連接 - 正在創建一個以上的插座

我的同事是誰寫的服務器部分,說當我運行客戶端我得到連接。當我點擊加入按鈕時,我打開一個連接並通過它發送初始消息,但同時打開另一個套接字。 然後我繼續收到來自第一個連接的所有消息,當我發送消息時,它會通過第二個連接。 我的疑惑是 1)爲什麼在這裏創建了2個連接/套接字? 2)爲什麼我無法通過相同的連接讀寫? 3)這是否意味着如果有300個客戶端用戶600個連接將被打開?

我是iphone編程的初學者。所以請原諒,如果我不清楚。

#import "ViewController.h" 
@implementation ViewController 
@synthesize txtUsername,txtPassword,webData,responseString,sessionId; 
@synthesize inputStream, outputStream,messages; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
    NSLog(@"Entered main"); 
    [self initNetworkCommunication]; 
    messages = [[NSMutableArray alloc] init]; 
} 


- (IBAction)usernExit:(id)sender { 
    [txtUsername resignFirstResponder]; 
} 

- (IBAction)pwdExit:(id)sender { 
    [txtPassword resignFirstResponder]; 

} 

- (IBAction)btnLogin:(id)sender { 

    NSLog(@"Clicked button1"); 

    NSString *response = [NSString stringWithFormat:@"PMS|Login|user1|sssss"]; 

NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]]; 
[outputStream write:[data bytes] maxLength:[data length]]; 

} 

- (void) initNetworkCommunication { 
    NSLog(@"initNetworkCommunication called"); 

CFReadStreamRef readStream; 
CFWriteStreamRef writeStream; 
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"xxx.xxx.xx.xx", xxxx, &readStream, &writeStream); 
inputStream = (NSInputStream *)readStream; 
outputStream = (NSOutputStream *)writeStream; 
[inputStream setDelegate:self]; 
[outputStream setDelegate:self]; 
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
[inputStream open]; 
[outputStream open]; 
} 
- (IBAction)sendMessage:(id)sender { 
    NSLog(@"sendMessage called"); 

    NSString *response = [NSString stringWithFormat:@"PMS|Msg"]; 
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]]; 
[outputStream write:[data bytes] maxLength:[data length]]; 




} 

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent { 

NSLog(@"stream event %i", streamEvent); 
switch (streamEvent) { 
case NSStreamEventOpenCompleted: 
NSLog(@"Stream opened"); 
break; 
case NSStreamEventHasBytesAvailable: 

if (theStream == inputStream) { 
uint8_t buffer[1024]; 
int len; 
while ([inputStream hasBytesAvailable]) { 
len = [inputStream read:buffer maxLength:sizeof(buffer)]; 
if (len > 0) { 
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; 

if (nil != output) { 

NSLog(@"server said: %@", output); 
[self messageReceived:output]; 
} 
} 
} 
} 
break; 

case NSStreamEventErrorOccurred: 
NSLog(@"Can not connect to the host!"); 
break; 
case NSStreamEventEndEncountered: 
      NSLog(@"NSStreamEventEndEncountered:method is called"); 
      [theStream close]; 
      NSLog(@"theStream is closed"); 

      [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
      NSLog(@"theStream is removed from runloop"); 

      [theStream release]; 
      NSLog(@"theStream is released"); 

      theStream = nil; 
break; 
default: 
NSLog(@"Unknown event"); 
} 

}//End of stream 
- (void) messageReceived:(NSString *)message { 
    NSLog(@"Entered MessageRecieved"); 
[messages addObject:message]; 

} 


- (void)dealloc { 

[messages release]; 
    [super dealloc]; 
} 


@end 

.h文件中

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <NSStreamDelegate> 

@property (retain, nonatomic) IBOutlet UITextField *txtUsername; 
@property (retain, nonatomic) IBOutlet UITextField *txtPassword; 
@property (retain, nonatomic) NSMutableData *webData; 
@property (retain, nonatomic) NSString *responseString; 
@property (retain, nonatomic) NSString *sessionId; 


@property (retain, nonatomic) NSInputStream *inputStream; 
@property (retain, nonatomic) NSOutputStream *outputStream; 
@property (nonatomic, retain) NSMutableArray *messages; 

- (IBAction)usernExit:(id)sender; 
- (IBAction)pwdExit:(id)sender; 
- (IBAction)btnLogin:(id)sender; 
- (void)initNetworkCommunication; 
- (IBAction)sendMessage:(id)sender; 
- (IBAction)sendMsgAgain:(id)sender; 

@end 
the logs 
------------------ 
When i run the program 
----------------------------- 
2013-04-29 03:57:57.826 TestConnection[13670:11303] Entered main 
2013-04-29 03:57:57.827 TestConnection[13670:11303] initNetworkCommunication called 
2013-04-29 03:57:57.846 TestConnection[13670:11303] stream event 1 
2013-04-29 03:57:57.847 TestConnection[13670:11303] Stream opened 
2013-04-29 03:57:57.847 TestConnection[13670:11303] stream event 1 
2013-04-29 03:57:57.847 TestConnection[13670:11303] Stream opened 
2013-04-29 03:57:57.847 TestConnection[13670:11303] stream event 4 
2013-04-29 03:57:57.847 TestConnection[13670:11303] Unknown event 

When i click the first button 
------------------------------------ 
2013-04-29 03:58:17.994 TestConnection[13670:11303] Clicked button1 
2013-04-29 03:58:17.997 TestConnection[13670:11303] Entered main 
2013-04-29 03:58:17.998 TestConnection[13670:11303] initNetworkCommunication called 
2013-04-29 03:58:17.999 TestConnection[13670:11303] stream event 4 
2013-04-29 03:58:17.999 TestConnection[13670:11303] Unknown event 
2013-04-29 03:58:18.005 TestConnection[13670:11303] stream event 1 
2013-04-29 03:58:18.005 TestConnection[13670:11303] Stream opened 
2013-04-29 03:58:18.005 TestConnection[13670:11303] stream event 1 
2013-04-29 03:58:18.005 TestConnection[13670:11303] Stream opened 
2013-04-29 03:58:18.005 TestConnection[13670:11303] stream event 4 
2013-04-29 03:58:18.006 TestConnection[13670:11303] Unknown event 
2013-04-29 03:58:18.122 TestConnection[13670:11303] stream event 2 
2013-04-29 03:58:18.123 TestConnection[13670:11303] New String JSESSIONID=56be3c2f6bbb30a6a8d0728dc710 
2013-04-29 03:58:18.123 TestConnection[13670:11303] server said: JSESSIONID=56be3c2f6bbb30a6a8d0728dc710 
WebLoginReplyType_t.Accepted 
2013-04-29 03:58:18.124 TestConnection[13670:11303] Entered MessageRecieved 
+0

你需要發佈代碼,說明你在做什麼,以便我們弄清楚發生了什麼。從你的描述來看,這聽起來像是以某種方式觸發了多次打開,這會導致多重連接。 – gaige 2013-04-28 11:30:24

+0

添加了各自的代碼。感謝abt – 2013-04-28 12:18:31

+0

在那裏似乎沒有任何明顯的錯誤。你在日誌中看到什麼? – gaige 2013-04-28 12:54:10

回答

1

您的日誌表明您要麼收到兩個viewDidLoad消息,要麼每兩個收到一個消息不同的意見。你根本沒有提到改變看法,因此似乎排除了後者,這使得前者不存在。

嘗試在viewDidLoad中放置斷點並查看ViewController實例(self)是否兩次都是相同的。如果相同,則需要查看堆棧抓取並查看第二個調用來自哪裏,因爲帽子通常會在低內存事件之後指示重新加載,而現代設備不應該這樣做。如果ViewController實例不同,那麼你需要明白爲什麼要分配第二個ViewController。同樣,堆棧跟蹤也可以幫助創建init方法並在其上設置斷點。

+0

非常感謝你的reply.it幫助我認爲:)即使我沒有以編程方式調用另一個視圖,我正在通過使用故事板的xib繼續到不同的視圖控制器。我刪除了賽格,然後停止打開第二個連接。 – 2013-04-30 12:37:46

-1

不是一個真正的答案你的問題,但爲什麼不直接使用CocoaAsyncSocket

此框架提供了一個穩定的Socket,您可以輕鬆使用。它只會創建一個套接字,並具有更多的功能以及許多故障,例如,您可以使用像您的套接字已經修復的套接字;

+0

真的與問題無關。在這種情況下,它根本不能解決問題。 – gaige 2013-04-29 12:10:41