我試圖創建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
你需要發佈代碼,說明你在做什麼,以便我們弄清楚發生了什麼。從你的描述來看,這聽起來像是以某種方式觸發了多次打開,這會導致多重連接。 – gaige 2013-04-28 11:30:24
添加了各自的代碼。感謝abt – 2013-04-28 12:18:31
在那裏似乎沒有任何明顯的錯誤。你在日誌中看到什麼? – gaige 2013-04-28 12:54:10