不调用 didreaddata 方法。使用异步套接字
the didreaddata method is not called. Using asyncsocket
我想使用 asyncsocket 在 ios 应用程序与 windows 服务器(由 c++ 编码)之间进行通信。 ios 向服务器发送数据包可以,但是ios 无法从服务器读取数据包。
我已经通过客户端通过 C++ 测试了服务器代码,它工作正常。
这是我的 ios 代码部分。
- (int) connectServer: (NSString *) hostIP port:(int) hostPort{
_isBusy = YES;
client = nil;
client = [[AsyncSocket alloc] initWithDelegate:self];
self.HOST_IP = hostIP;
self.HOST_PORT = hostPort;
NSError *err = nil;
if([client connectToHost:hostIP onPort:hostPort error:&err])
return SRV_CONNECT_SUC;
else{
return SRV_CONNECT_FAIL;
}
}
- (void)sendMsg:(NSString *)msg
{
if ([client isConnected]){
NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
[client writeData:data withTimeout:-1 tag:0];
}
}
-(void*) readMsg
{
[client readDataWithTimeout:-1 tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Hava received datas is :%@",aStr);
[aStr release];
[client readDataWithTimeout:-1 tag:0];
}
-(void)onSocket:(AsyncSocket*)sock didConnectToHost:(NSString *)host port:(UInt16)port{
NSLog(@"Socket is connected!");
[client readDataWithTimeout:-1 tag:0];
}
-(void)onSocket:(AsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag {
NSLog(@"Received bytes: %d",partialLength);
}
我在每一个都下了断点,只是调用了didConnectToHost
,didReadParticalDataOfLength
和didReadData
没有被调用。
我考了很多次,但是ios什么都看不懂。
知道我做错了什么吗?
p.s。我使用 asyncsocket 而不是 GCDasyncsocket
。
谢谢你的时间。
缺少 [readMsg];
- (void)sendMsg:(NSString *)msg {
if ([client isConnected]){
NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
[client writeData:data withTimeout:-1 tag:0];
[readMsg];
}
}
我想使用 asyncsocket 在 ios 应用程序与 windows 服务器(由 c++ 编码)之间进行通信。 ios 向服务器发送数据包可以,但是ios 无法从服务器读取数据包。 我已经通过客户端通过 C++ 测试了服务器代码,它工作正常。 这是我的 ios 代码部分。
- (int) connectServer: (NSString *) hostIP port:(int) hostPort{
_isBusy = YES;
client = nil;
client = [[AsyncSocket alloc] initWithDelegate:self];
self.HOST_IP = hostIP;
self.HOST_PORT = hostPort;
NSError *err = nil;
if([client connectToHost:hostIP onPort:hostPort error:&err])
return SRV_CONNECT_SUC;
else{
return SRV_CONNECT_FAIL;
}
}
- (void)sendMsg:(NSString *)msg
{
if ([client isConnected]){
NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
[client writeData:data withTimeout:-1 tag:0];
}
}
-(void*) readMsg
{
[client readDataWithTimeout:-1 tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Hava received datas is :%@",aStr);
[aStr release];
[client readDataWithTimeout:-1 tag:0];
}
-(void)onSocket:(AsyncSocket*)sock didConnectToHost:(NSString *)host port:(UInt16)port{
NSLog(@"Socket is connected!");
[client readDataWithTimeout:-1 tag:0];
}
-(void)onSocket:(AsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag {
NSLog(@"Received bytes: %d",partialLength);
}
我在每一个都下了断点,只是调用了didConnectToHost
,didReadParticalDataOfLength
和didReadData
没有被调用。
我考了很多次,但是ios什么都看不懂。 知道我做错了什么吗?
p.s。我使用 asyncsocket 而不是 GCDasyncsocket
。
谢谢你的时间。
缺少 [readMsg];
- (void)sendMsg:(NSString *)msg {
if ([client isConnected]){
NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
[client writeData:data withTimeout:-1 tag:0];
[readMsg];
}
}