0

I am new to objective C and ios development. I generally program Android apps with Java and always used the onPause call to close my sockets. However when I call my close socket method under applicationWillResignActive I loose communication with my stream listener and cannot close the input and output streams. In fact both stream's stream status show NSStreamStatusNotOpen or 0 even though the app is connected to the server. Any ideas?

Thank you

Edit

Connect method in SocketHandler.m Class:

  -(void)connect:(int *)port ipAddress:(NSString *)ipAddress
{
        CFReadStreamRef readStream;
        CFWriteStreamRef writeStream;
        CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)ipAddress, port, &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];
        NSLog(@"input stream id %i", inputStream);  
}

-(void)disconnect{

    NSLog(@"disconnect method called");

    socketStatus = [outputStream streamStatus];
    int status = socketStatus;
    NSLog(@"Stream Status is %i", status);

    if (status == 2) {
        [inputStream close];
        [outputStream close];
        NSLog(@"Socket Closed");
    }



}
Travis Elliott
  • 291
  • 2
  • 5
  • 18
  • Could you elaborate a little? Calling sending `close:` to an `NSStream` should close it, and send your delegates `stream:handleEvent:`. – arbales Jul 23 '12 at 20:11
  • I Believe the problem is not being able to access the NSInputStream and NSOutputStream Objects from my AppDelegate. I have a class called SocketHandler which has a connect and disconnect method. I call the connect and disconnect methods from buttons in the UI. However when I call the disconnect method from the applicationwillresignactive in the app delegate it acts like it cant access the stream objects which is wierd because every other class in the app can. – Travis Elliott Jul 23 '12 at 21:22
  • How did you create the SocketHandler object that you connect up to your buttons? How do you discover this object from your app delegate? – torrey.lyons Jul 23 '12 at 21:55
  • The NSInputStream, NSOutputStream, and StreamStatus objects are created and init in the SocketHandler class I created. So what happens is the App Delegate class calls methods of the SocketHandler class which create the streams, connect them, and disconnect them. It can also reply back with stream status if needed. However when the connect method in the SocketHandler class is called to create the Stream objects and connect them they cannot be accessed from any other class other than the App Delegate which is strange because the streams are not created in the App Delegate class. – Travis Elliott Jul 24 '12 at 19:53

1 Answers1

0

Ok, Here is the answer to my own question.

I created 3 classes:

TCPSocketAppDelegate TCPSocketViewController Socket Handler

Inside the Socket handler Class I have a Method Called Connect and another called Disconnect. Here they are:

Connect Method:

-(void)connect:(int *)port ipAddress:(NSString *)ipAddress
{
        NSLog(@"input stream id %i", inputStream);
        CFReadStreamRef readStream;
        CFWriteStreamRef writeStream;
        CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)ipAddress, port, &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];
        NSLog(@"input stream id %i", inputStream);  
}

Disconnect Method:

-(void)disconnect{

    NSLog(@"disconnect method called");
    NSLog(@"input stream id %i", inputStream);

    socketStatus = [outputStream streamStatus];
    int status = socketStatus;
    NSLog(@"Stream Status is %i", status);

    if (status == 2) {
        [inputStream close];
        [outputStream close];
        NSLog(@"Socket Closed");
    }



}

I then created reference objects to the Socket Handler class in the TCPSocketAppDelegate class and the TCPSocketView Controller class. However when I did this it created a new instance of the SocketHandler class for each so they were not addressing the same socket, hence one could connect the socket but the other could not access it. To remedy this I simply made the connect and disconnect methods globally accessible using +(void). Now I dont have to create a reference object in the other classes to call the methods. I hope this information will help someone down the road.

Travis Elliott
  • 291
  • 2
  • 5
  • 18