如何使用CocoaAsyncSocket读取数据? [英] How do I read data using CocoaAsyncSocket?

查看:141
本文介绍了如何使用CocoaAsyncSocket读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在appDelegate didFinishLaunchingWithOptions 方法中创建了一个 TCP套接字连接。这很简单,我已成功连接到我的服务器。我在View中从服务器读取数据时遇到了很大困难。我一直在阅读有关如何使用 CocoaAsyncSocket 正确(逐步)读取数据的教程,但我没有遇到任何有用的东西。

I have created a TCP Socket connection in my appDelegate didFinishLaunchingWithOptions method. That was the easy part, and I have successfully connected to my server. I am having great difficulty with reading the data from the server in my View. I have been looking through tutorials on how to appropriately (step by step) read data using CocoaAsyncSocket, but I haven't come across anything useful.

这是我appDelegate的代码:

This is my code from my appDelegate:

-(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;
} 

这是我的连接方法,位于appDelegate文件的底部:

And here is my connect method, at the bottom of the appDelegate file:

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

这很容易。我现在需要从服务器读取数据。我知道需要创建某种 NSData NSMutableData 对象来获取从服务器读取的数据的值。我找到任何指导我正确方向的教程或文档都是非常不成功的。有几个不同的读取功能,一些具有不同的参数,等等。如果有人能指出我的资源深入这个*(我是一个新手,毕竟= P)*我真的很感激 - 或者如果有人知道一个简单的方法来实现这个目标,并且不介意在这里提供示例代码:D

That was the easy part. I now need to read data from the server. I know some kind of NSData or NSMutableData object needs to be created for to take the value of the data I read from the server. I just have been very unsuccessful in finding any tutorial or documentation that points me in the right direction. There are several different read functions, some with different parameters, etc. If anyone could point me to a resource that goes over this in depth*(I am a newbie, after all =P)* I would really appreciate it -- Or if somebody knows of an easy way to accomplish this goal and wouldn't mind providing sample code here :D

这是我正在使用的库: CocoaAsyncSocket 。我正在使用库 AsyncSocket.h AsyncSocket.m

This is the library I'm using: CocoaAsyncSocket. I'm using the library AsyncSocket.h and AsyncSocket.m

我被困在这几个小时,所以任何帮助都会非常感激。

I've been stuck at this for hours, so any help would be great appreciated.

谢谢!

推荐答案

这应该有效:

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];
    NSString *msg = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
    if(msg)
    {
        NSLog(@"RX:%@",msg);
    }
}

您还应该实现一些其他委托方法,例如:

You should also implement some other delegate methods, for example:

- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
    NSLog(@"error - disconnecting");
    //you'd probably want to start reconnecting procedure here...
}

- (void)onSocketDidDisconnect:(AsyncSocket *)sock
{
    NSLog(@"disconnected");
}

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

编辑:如果内存对我有用,那么有一些文档和一些例子可用与图书馆。

if memory serves me right there is some documentation and also some examples available with the library.

这篇关于如何使用CocoaAsyncSocket读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆