AsyncSocket 错误怎么解决

2025-12-17 10:14:42
推荐回答(2个)
回答1:

第一个解决法是你重新装个XP或2000因为这个问题好想只在98或更低的系统上出现过,如果你很喜欢继续使用,问题原因可能是:一.是你的SOCKET没有启动(SCKTSRVR.EXE)或者无法连接到服务器。……二,是SCKTSRVR.EXE文件被破坏

回答2:

#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地址就可以了