GCDAsyncSocket 发送消息
我在使用 GCDAsyncSocket 通过套接字发送消息时遇到问题。我建立连接并尝试向服务器发送消息,但应用程序退出时正在发送消息。我需要一种方法来做到这一点。我尝试了[asyncSocket断开连接];但它没有改变任何东西......请帮忙。这是我的代码的一部分:
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);
I have issues sending messages over socket using GCDAsyncSocket. I establish a connection and try to send a message to server, but message is being sent when application quits. I need a way to do it before. I tried [asyncSocket disconnect]; but it changed nothing... Please help. Here is part of my code:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题出在“readDataToData:receivedData”中 - 您要求 GCDAsyncSocket 读取数据,直到它发现“任何内容” - receiveData 未知。通常使用类似
That will read 的东西,直到它得到 CR/LF 组合来终止读取。您还可以读取标头并确定要读取的数据的长度。
您需要告诉它何时从读取中返回(这就是为什么您没有得到任何返回)。此外,它可能只会在应用程序退出时返回给您,因为应用程序终止时套接字会关闭。
Your problem is in the "readDataToData:receivedData" - You're asking GCDAsyncSocket to Read Data until it finds "nothing" - receivedData is unknown. One normally uses something like
That will read until it gets a CR/LF combination to terminate the read. You could also read the header and determine the length of the data to be read.
You need to tell it when to return from the read (that's why you're getting nothing back). Also it's probably only returning to you when the application quits because the socket is getting closed when the app terminates.