0
我有問題通過套接字使用GCDAsyncSocket發送消息。我建立連接並嘗試向服務器發送消息,但應用程序退出時正在發送消息。我需要一種方法來做到這一點。我試過[asyncSocket disconnect];但它沒有改變......請幫助。這裏是我的代碼部分:GCDAsyncSocket發送消息
NSError *err = nil;
asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
if(![asyncSocket connectToHost:@"localhost" onPort:7777 error:&err]){
// If there was an error, it's likely something like "already connected" or "no delegate set"
NSLog(@"I goofed: %@", err);
}
NSString *requestStr = @"<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><root><service>1</service><type>1</type><userProperties><username>someusername</username></userProperties></root>";
NSData *requestData = [requestStr dataUsingEncoding:NSUTF8StringEncoding];
[asyncSocket writeData:requestData withTimeout:1.0 tag:0];
NSData *receivedData;
[asyncSocket readDataToData:receivedData withTimeout:2.0 tag:0];
NSString *httpResponse = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"%@",httpResponse);
另請參閱我非常相似的回答問題:http://stackoverflow.com/questions/11998408/having-trouble-programming-streams – khaliq 2012-08-19 07:27:24