Domanda

I'm trying to write an app to send/receive data by means of the CocoaAsyncSocket library.

In the first version of the App, the socket is created/initialized in the View Controller where I also put the delegate methods that are correctly invoked:

WakmanFirstViewController.m

#import "WakmanFirstViewController.h"
#import "GCDAsyncSocket.h"

@implementation WakmanFirstViewController

- (void)viewDidLoad
 {  
    [super viewDidLoad];

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

    NSError *error = nil;

    if (![asyncSocket connectToHost:@"192.168.1.13" onPort:2000 error:&error]) {
        NSLog(@"Unable to connect to due to invalid configuration: %@",error);
    }

    NSData *requestData = [@"C" dataUsingEncoding:NSUTF8StringEncoding];
    [asyncSocket writeData:requestData withTimeout:-1.0 tag:0];
    [asyncSocket readDataWithTimeout:1 tag:0];
}

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {

    NSLog(@"socket:didConnectToHost:%@ port:%hu", host, port);
}

... (other delegate methods)

Now I'm trying to remove the creation of the socket from the ViewController and put it in a Singleton class so that I can use the same connection also from other views.

To do this I have created a new class (SocketConnection) where I moved also the delegate methods:

wakmanSocketConnection.h

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

@interface SocketConnection : NSObject {
}

+ (GCDAsyncSocket *)getInstance;

@end

wakmanSocketConnection.m

#import "SocketConnection.h"

static GCDAsyncSocket *socket;

@implementation SocketConnection

+ (GCDAsyncSocket *)getInstance
{
    @synchronized(self) {

        if (socket == nil) {
            socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];           
        }

        if (![socket isConnected]) {
            NSError *error = nil;

            if (![socket connectToHost:@"192.168.1.13" onPort:2000 error:&error]){
                    NSLog(@"Error connecting: %@", error);
            }
        }
    }
    return socket;
}

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
    NSLog(@"socket connected");
}

... (other delegate methods)

I then modified the Viewcontroller:

WakmanFirstViewController.h

#import <UIKit/UIKit.h>
#import "SocketConnection.h"

@class GCDAsyncSocket;

@interface WakmanFirstViewController : UIViewController  {
    GCDAsyncSocket *socket; 
}

@end

WakmanFirstViewController.m

#import "WakmanFirstViewController.h"

@implementation WakmanFirstViewController

- (void)viewDidLoad
 {
    [super viewDidLoad];

    socket = [SocketConnection getInstance]; 

    NSData *requestData = [@"C" dataUsingEncoding:NSUTF8StringEncoding];
    [asyncSocket writeData:requestData withTimeout:-1.0 tag:0];
    [asyncSocket readDataWithTimeout:1 tag:0];
}

The connection is established but the problem is that the delegate methods are not invoked.

In wakmanSocketConnection.m I set the delegate to self so it should refer to the class where I copied the methods.

Can someone help me in finding the problem ?

Thanks, Corrado

È stato utile?

Soluzione

The problem is that didReadData delegate method is called only once and you have to keep listening socket by calling:

[_socket readDataWithTimeout:-1 tag:0]; 

My method look like that:

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    dispatch_async(dispatch_get_main_queue(), ^{
        @autoreleasepool {
            // DO STH
            [_socket readDataWithTimeout:-1 tag:0];
        }
    });
}

Init method:

- (id)init
{
    if (self = [super init]) 
    {
        _socketQueue = dispatch_queue_create("Socket TCP Queue", nil);
        _delegateQueue = dispatch_queue_create("Socket Delegate Queue", nil);
        _socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:_delegateQueue socketQueue:_socketQueue];

    }
    return self;
}

and connect method:

- (void)connectToServer:(NSString *)host port:(NSInteger)port
{
    self.host = host;
    self.port = port;

    NSError *error = nil;
    if (![_socket connectToHost:host onPort:port error:&error])
    {

    }
}

write method:

- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag
{
    if ([self.socket isConnected]) {
        [self.socket writeData:data withTimeout:timeout tag:tag];
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top