我已经使用CocoaAsyncSocket创建了一个TCP连接,每当我尝试执行didReadData时,我都会得到空白。当我设置断点并尝试调试时,我发现"msg“的值是@"”。
这就是我的应用程序代表的样子:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSData *data = nil;
// Initialize socket object and make it a delegate. Then call the delegate methods.
socket = [[AsyncSocket alloc] initWithDelegate:self];
[self connect];
[self onSocket:socket didConnectToHost:@"9.5.8.6" port:11005];
[self onSocket:socket didReadData:data withTag:1]; // tags COULD be #defined *******
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;
}下面是我的onSocket::data withTag: method:
- (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);
if(msg == nil)
{
NSLog(@"msg is all Blanks");
}
}
else
{
NSLog(@"Fail");
}
}注意,此方法是来自CocoaAsyncLibrary的方法。我不知道我是否正确地调用了这个方法,或者我没有传递正确的参数,或者什么。
当我运行这个应用程序时,我在我的控制台中看到的就是:
2012-06-06 11:44:00.434 tekMatrix[1378:f803] connected
2012-06-06 11:45:14.312 tekMatrix[1378:f803] RX:所有的帮助都是非常感谢的。
谢谢!
编辑
下面是我的didFinishLaunchingWithOptions方法中的内容:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
socket = [[AsyncSocket alloc] initWithDelegate:self];
NSError *error = nil;
if (![socket connectToHost:@"199.5.83.63" onPort:11005 error:&error])
{
NSLog(@"Error connecting: %@", error);
}
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;
}现在我可以看到我已经连接到了,但是我仍然不理解onSocket:socket :data withTag: method是不被调用的。如果能在这方面提供任何帮助,我将不胜感激。谢谢!
发布于 2012-06-06 19:39:13
对不起,我不得不说:你对代表们的看法全错了。
您不需要自己调用实现委托协议的方法--委托者(在本例中为套接字)就是这样做的
所以这个代码
[self connect];
[self onSocket:socket didConnectToHost:@"9.5.8.6" port:11005];
[self onSocket:socket didReadData:data withTag:1]一点意义都没有。把它移开。
相反,应该有如下代码来连接
NSError *error = nil;
if (![socket connectToHost:@"9.5.8.6" onPort:11005 error:&error]) {
NSLog(@"Error connecting: %@", error);
}与之相比,套接字将调用socket:didConnectToHost:port:,这可能类似于
-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
[sock writeData:self.data withTimeout:-1 tag:1];
}对象套接字将调用您提供的进一步的委托方法实现。
但是,由于代表团是一个非常重要的模式,通过可可(-touch),确保,你得到了正确的。
https://stackoverflow.com/questions/10920923
复制相似问题