0
我想使用asyncsocket在ios應用程序與windows服務器(由C++編碼)之間進行通信。 ios發送數據包到服務器是好的,但ios不能從服務器讀取數據包。 我已經通過C++測試客戶端的服務器代碼,它工作正常。 這是我的一部分ios代碼。未調用didreaddata方法。使用asyncsocket
- (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
。 感謝您的時間。
謝謝,它的工作原理。 – Jun