gpt4 book ai didi

ios - 未调用 GCDAsyncSocket readData

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:40 25 4
gpt4 key购买 nike

我正在尝试使用我的 iPhone 向服务器发送消息并接收响应。我可以使用以下方式连接到服务器:

telnet 123.123.123.1 6000
Trying 123.123.123.1...
Connected to 123.123.123.1.
Escape character is '^]'.
?VERSION
OK
VERSION=PROTOCOL: 1.1.0

?版本是我的问题

OK 表示它收到并理解了问题

VERSION=是服务器的响应

所以我尝试用 xcode 做同样的事情

所以我的 viewDidLoad 中有这个

dispatch_queue_t mainQueue = dispatch_get_main_queue();
asyncSocket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:mainQueue];
asyncSocket.delegate = self;

NSString *host = @"123.123.123.1";
uint16_t port = 6000;

NSLog(@"Connecting to \"%@\" on port %hu...", host, port);

NSError *error = nil;
if (![asyncSocket connectToHost:host onPort:port withTimeout:5.0 error:&error])
{
NSLog(@"Error connecting: %@", error);
}
else
{
NSLog(@"Connecting...");
}

我有以下代码显示它已连接

-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:
(UInt16)port

{
NSLog(@"socket:%p didConnectToHost:%@ port:%hu", sock, host, port);

// We're just going to send a test string to the server.

NSString *myStr = @"?VERSION";
NSData *myData2 = [myStr dataUsingEncoding:NSUTF8StringEncoding];

[asyncSocket writeData:myData2 withTimeout:-1 tag:0];


}

下面显示它写了

-(void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
NSLog(@"WRITING");
[asyncSocket readDataToData:[GCDAsyncSocket LFData] withTimeout:-1 tag:0];
}

遗憾的是,这永远不会被调用

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{


NSString *tempString = [[NSString alloc]initWithData:data
encoding: NSUTF8StringEncoding];


}

我迷路了,真的需要一些帮助

最佳答案

这是因为你说同时写和读。首先调用 [asyncSocket writeData:myData2 withTimeout:-1 tag:0]; 并在 didWriteDataWithTag 中调用 [asyncSocket readDataToData:[GCDAsyncSocket LFData] withTimeout:-1 tag :0];。您在一个线程上 - dispatch_get_main_queue() - 它不能同时做两件事。

关于ios - 未调用 GCDAsyncSocket readData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26468095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com