我试图向服务器发送一条消息,并使用我的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];
}我迷路了,真的需要一些帮助
发布于 2014-10-20 14:57:55
这是因为你说,写和读同时。首先调用[asyncSocket writeData:myData2 withTimeout:-1 tag:0];,在didWriteDataWithTag中调用[asyncSocket readDataToData:[GCDAsyncSocket LFData] withTimeout:-1 tag:0];。你在一个线程上-- dispatch_get_main_queue() --它不能同时做两件事。
https://stackoverflow.com/questions/26468095
复制相似问题