1

I set socket connection in appdelegate from app starting up connecting to server. in appdelegate.h

@interface AppDelegate : NSObject <NSStreamDelegate,UIApplicationDelegate> {
    UIWindow *window;
    UITabBarController *tabBarController;
    NSInputStream *inputStream;
 NSOutputStream *outputStream;
}

then in appdelegate.m set connecting to server:

 CFReadStreamRef readStream;
 CFWriteStreamRef writeStream;
 CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"111.111.111.11", 111, &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];

It runs well when app startup. communicate well also.

and then I have a tab controller. Every one of tabs needs exchanged datas to server by this socket. I dont want to creat different socket for every tab.

How do I use the same outputstream/inputstream?

I try this in firstviewcontroall.m but failed:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSData *data = [[NSData alloc] initWithData:[@"hello this is firstview" dataUsingEncoding:NSUTF8StringEncoding]];
 [outputStream write:[data bytes] maxLength:[data length]];
}

There is no datas sending to server. I dont want to create a socket on every viewcontroller to server. that wastes too much resources. My question is how do I send/recieve datas by one socket connection?

STANLEY
  • 171
  • 1
  • 1
  • 8

2 Answers2

2

Use the streams by below way:

AppDelegate *appDel = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDel.outputStream write:[data bytes] maxLength:[data length]];
[appDel.inputStream <CALL_YOUR_METHOD>];
Apurv
  • 17,116
  • 8
  • 51
  • 67
  • yes, thanks,the outputstream is workin now. but I could not make inputstream working. the inputstream is working fine in appdelegate.m but firstviewcontroller.m. – STANLEY Jun 22 '13 at 11:50
  • It shows a warning "NSInputstream may not respond to 'method'". there is a method responds to inputstream already in appdelegate.m. I guess these two method conflig each other. Is there a way pass a variable from appdelegate.m to fristviewcontrol.m , then activates another method to do something else? – STANLEY Jun 22 '13 at 14:36
1

I would create a utility/manager class to handle communication with the server. This way you can easily access that from other parts of your code. Also easy to make sure that it is thread safe. Note that you should consider not doing these on the main thread.

However, here is the code if you do want to access the variables defined in AppDelegate:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.outputStream <method>];
rakmoh
  • 2,953
  • 1
  • 16
  • 14
  • If I create a class handling this with server, do I have to make connection on every tab? If not, I will stil go back to this question. I guess they should be similiar thing. – STANLEY Jun 20 '13 at 00:33
  • There are various ways to share the access to a object. Check the following link for how to make your object a singleton and share from different tabs/classes: http://stackoverflow.com/questions/145154/what-should-my-objective-c-singleton-look-like – rakmoh Jun 20 '13 at 00:46