0

I am using the CocoaAsyncSockets library in order to create a tcp socket connection in my app. Currently, I have successfully created a connection by opening the socket connection in my didFinishLaunchingWithOptions method in my appDelegate.m file. My code looks like so:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:  (NSDictionary *)launchOptions
{
    socket = [[AsyncSocket alloc] initWithDelegate:self];
    [self connect];

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]    autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[tekMatrixViewController alloc]     initWithNibName:@"tekMatrixViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

And here's my connect method:

- (void)connect 
{
    [socket connectToHost:@"9.5.3.6" onPort:11005 error:nil];
}

Now, what I have been struggling with is how to use read/write methods in my views without having to re-establish a connection.

  • I have established a tcp socket connection when the application launches
  • I open the socket in the didFinishLaunchWithOptions method so that I can create the connection and stay connected the entire time my app is running
  • I don't know how to read/write from/to the server from my views
  • I'm very new to socket/iOS development, so I'm hoping for the easiest possible solution

I would appreciate any help I can get on this. I'm still pretty new to iOS and still picking up on the syntax, so the more details anyone can provide, the better off I'll be.

Thanks so much for the help!

Skizz
  • 1,211
  • 5
  • 17
  • 28

1 Answers1

0

AsyncSocket comes with this method for writing:

- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;

And several variants of read methods, here is one: (more from the header file of the library)

- (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag;

You should get the samples code from the same website that you have downloaded the AsyncSocket library to see how they are used. One thing to keep in mind is that these methods are asynchronous operations.

user523234
  • 14,323
  • 10
  • 62
  • 102
  • I'll have to look up some documentation on how to implement/invoke these methods. Thanks for the heads up. – Skizz Jun 05 '12 at 17:35
  • Where does the "read" data from the server go? I would think I would need to store the read data into an NSMutableData object. What gives? – Skizz Jun 05 '12 at 18:54
  • - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag For GCD verison: - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag There are two sample mini apps that come with the library called EchoServer and TCPEchoClient. Just study those two for the basic of how to use the library. – user523234 Jun 07 '12 at 02:01