如何使用AsyncSocket,一直提示错误
1个回答
2014-11-22 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517201
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
#import "IntroLayer.h"
@implementation IntroLayer
@synthesize sock=_sock;
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
IntroLayer *layer = [IntroLayer node];
[scene addChild: layer];
return scene;
}
- (id) init
{
self = [super init];
if (self != nil) {
_sock = [[AsyncSocket alloc]initWithDelegate:self];
NSString *host = @"127.0.0.1";
int nPort = 10005;
NSError *error = nil;
[_sock connectToHost:host onPort:nPort withTimeout:2 error:&error];
NSLog(@"%@",error);
}
return self;
}
- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
NSLog(@"Info___willDisconnectWithError==%@",err);
[_sock release];
_sock = nil;
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"Info___didConnectToHost");
[sock readDataWithTimeout:-1 tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSLog(@"Info___didReadData");
NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];
NSString *msg = [[[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding] autorelease];
if(msg)
{
NSLog(@"%@",msg);
}
else
{
NSLog(@"Error converting received data into UTF-8 String");
}
[sock readDataWithTimeout:-1 tag:0];
}
-(void)dealloc
{
[_sock release];
[super dealloc];
}
@end
提示的错误为:Info___willDisconnectWithError==Error Domain=NSPOSIXErrorDomain Code=61 "The operation couldn’t be completed. Connection refused"
把127.0.0.1换成实际IP地址就可以了
@implementation IntroLayer
@synthesize sock=_sock;
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
IntroLayer *layer = [IntroLayer node];
[scene addChild: layer];
return scene;
}
- (id) init
{
self = [super init];
if (self != nil) {
_sock = [[AsyncSocket alloc]initWithDelegate:self];
NSString *host = @"127.0.0.1";
int nPort = 10005;
NSError *error = nil;
[_sock connectToHost:host onPort:nPort withTimeout:2 error:&error];
NSLog(@"%@",error);
}
return self;
}
- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
NSLog(@"Info___willDisconnectWithError==%@",err);
[_sock release];
_sock = nil;
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"Info___didConnectToHost");
[sock readDataWithTimeout:-1 tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSLog(@"Info___didReadData");
NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];
NSString *msg = [[[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding] autorelease];
if(msg)
{
NSLog(@"%@",msg);
}
else
{
NSLog(@"Error converting received data into UTF-8 String");
}
[sock readDataWithTimeout:-1 tag:0];
}
-(void)dealloc
{
[_sock release];
[super dealloc];
}
@end
提示的错误为:Info___willDisconnectWithError==Error Domain=NSPOSIXErrorDomain Code=61 "The operation couldn’t be completed. Connection refused"
把127.0.0.1换成实际IP地址就可以了
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询