3

I am trying to use the GCDAsyncSocket class to connect to a device but am having trouble. It seems to fail to inform me if it connects, but also fails to inform me it failed to connect.

Here is my .h file

#import <Foundation/Foundation.h>
#import "GCDAsyncSocket.h"

@interface SocketTestClass : NSObject <GCDAsyncSocketDelegate>
{
    GCDAsyncSocket* socket;
}

@end

and here is my .m file

@implementation SocketTestClass

- (void)dealloc
{
    [self disconnect];
}

- (void)connect
{
    NSLog(@"Ready to create socket");

    socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

    NSError* error = Nil;
    if (![socket connectToHost:@"192.168.1.200" onPort:80 withTimeout:10 error:&error])
    {
        NSLog(@"Failed to create socket");
    }
}

- (void)disconnect
{
    if ([socket isConnected])
    {
        [socket disconnect];
    }
}

- (void)socket:(GCDAsyncSocket *)sock
didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
    NSLog(@"Socket did accept new socket");
}

- (void)socket:(GCDAsyncSocket *)sock
didConnectToHost:(NSString *)host
          port:(uint16_t)port
{
    NSLog(@"Socket did connect to host on port");
}

- (void)socket:(GCDAsyncSocket *)sock
   didReadData:(NSData *)data
       withTag:(long)tag
{
    NSLog(@"Socket did read data with tag");
}

- (void)socket:(GCDAsyncSocket *)sock
didReadPartialDataOfLength:(NSUInteger)partialLength
           tag:(long)tag
{
    NSLog(@"Socket did read partial data of length");
}

- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
    NSLog(@"Socket did write data with tag");
}

- (void)socket:(GCDAsyncSocket *)sock
didWritePartialDataOfLength:(NSUInteger)partialLength
           tag:(long)tag
{
    NSLog(@"Socket did write partial data of length");
}

- (void)socketDidCloseReadStream:(GCDAsyncSocket *)sock
{
    NSLog(@"Socket did close read stream");
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock
                  withError:(NSError *)err
{
    NSLog(@"Socket did disconnect with error");
}

- (void)socketDidSecure:(GCDAsyncSocket *)sock
{
    NSLog(@"Socket did secure");
}

@end

From my view controller I call the connect method of this class but the only thing that prints out to the console is:

Ready to create socket

Note: I have implemented all delegates for GCDAsyncSocket and have all writing to the console via NSLog.

Any idea as to why I do not get the message "Socket did connect to host on port" or the message "Socket did disconnect with error"?

prashant
  • 1,920
  • 14
  • 26
  • 1
    Is your SocketTestClass object going out of scope and being released? – joerick Sep 09 '12 at 16:29
  • Wow, that is exactly what it is, forgot to make my connection a variable in my ViewController, instead it was a local variable in a method. Thanks for the help. –  Sep 09 '12 at 16:49
  • 1
    I'm having this exact issue as well, but I'm not exactly sure what you mean by it "going out of scope." Could you explain how exactly you fixed this? I have what you have in your "connect" method in my viewDidLoad. Thanks :) – Charles Jun 13 '13 at 17:12
  • @prashant Could you please explain how exactly you fixed this? because i have exactly same issue. Any help is appreciated...!! – jigs Oct 27 '15 at 11:20

0 Answers0