GCDAsyncSocket 发送消息

发布于 2024-12-21 12:31:36 字数 1106 浏览 0 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空‖城人不在 2024-12-28 12:31:36

您的问题出在“readDataToData:receivedData”中 - 您要求 GCDAsyncSocket 读取数据,直到它发现“任何内容” - receiveData 未知。通常使用类似

[asyncSocket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 tag:0];

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

[asyncSocket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 tag:0];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文