我正在尝试使用GCDAsyncUdpSocket
创建客户端套接字。我看不到net cat
的任何内容,看起来第一次发送后连接就关闭了。
运行我的代码时,我会收到这些日志:
2014-04-16 14:00:48.434 WL002[20518:60b] is connected
2014-04-16 14:00:48.435 WL002[20518:60b] able to start receiving
2014-04-16 14:00:48.436 WL002[20518:60b] Ready
2014-04-16 14:00:48.437 WL002[20518:60b] the local address,host,port: <1002dd8e 7f000001 00000000 00000000>,127.0.0.1,56718
2014-04-16 14:00:48.437 WL002[20518:60b] the connected address,host,port: <10020017 7f000001 00000000 00000000>,127.0.0.1,23
2014-04-16 14:00:48.704 WL002[20518:60b] did connect to address:<10020017 7f000001 00000000 00000000>
2014-04-16 14:00:48.705 WL002[20518:60b] connection status: 1
2014-04-16 14:00:48.718 WL002[20518:60b] ATTEMPT SENDING (0): Hello World test
2014-04-16 14:00:48.719 WL002[20518:60b] connection status: 0
2014-04-16 14:00:48.719 WL002[20518:60b] ATTEMPT SENDING (1): Hello World test 2
2014-04-16 14:00:48.720 WL002[20518:60b] connection status: 0
2014-04-16 14:00:48.720 WL002[20518:60b] ATTEMPT SENDING (2): Hello World test 3
2014-04-16 14:00:48.722 WL002[20518:60b] socket sent with tag 0
2014-04-16 14:00:48.722 WL002[20518:60b] socket did close error Error Domain=GCDAsyncUdpSocketErrorDomain Code=4 "Socket closed" UserInfo=0xaa6b820 {NSLocalizedDescription=Socket closed}
2014-04-16 14:00:48.723 WL002[20518:60b] socket did not sent with tag 1 with error: Error Domain=GCDAsyncUdpSocketErrorDomain Code=1 "You must specify destination of packet for a non-connected socket" UserInfo=0x9599ba0 {NSLocalizedDescription=You must specify destination of packet for a non-connected socket}
2014-04-16 14:00:48.723 WL002[20518:60b] socket did not sent with tag 2 with error: Error Domain=GCDAsyncUdpSocketErrorDomain Code=1 "You must specify destination of packet for a non-connected socket" UserInfo=0x9591270 {NSLocalizedDescription=You must specify destination of packet for a non-connected socket}
我的代码是:
//wifi setup
if (udpSocket == nil)
{
[self setupSocket];
}
NSLog(@"the local address,host,port: %@,%@,%d",[udpSocket localAddress],[udpSocket localHost],[udpSocket localPort]);
NSLog(@"the connected address,host,port: %@,%@,%d",[udpSocket connectedAddress],[udpSocket connectedHost],[udpSocket connectedPort]);
return YES;
}
- (void)setupSocket
{
NSError *err = nil;
udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
udpSocket.delegate = self;
if (![udpSocket bindToPort:0 error:&err])
{
NSLog(@"Error binding: %@", err)
;
return;
}
//ensure socket is connected
if (![udpSocket connectToHost:@"127.0.0.1" onPort:23 error:&err]) {
NSLog(@"not connected");
return;
}
else NSLog(@"is connected");
//start ablitiy to continuously receive data
if (![udpSocket beginReceiving:&err])
{
NSLog(@"Error receiving: %@", err);
return;
}
else NSLog(@"able to start receiving");
[udpSocket setIPVersionNeutral];
NSLog(@"Ready");
}
- (void)send:(NSString *)msg
{
NSLog(@"connection status: %hhd",[udpSocket isConnected]);
if ([msg length] == 0)
{
NSLog(@"Message required");
return;
}
NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
[udpSocket sendData:data withTimeout:5 tag:tag];
NSLog(@"ATTEMPT SENDING (%i): %@", (int)tag, msg);
tag++;
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didConnectToAddress:(NSData *)address
{
NSLog(@"did connect to address:%@", address);
[self send:@"Hello World test"];
[self send:@"Hello World test 2"];
[self send:@"Hello World test 3"];
}
我遇到的问题是:
nc -lk 23
)我认为第二个错误可能导致第一个错误,但我不知道为什么。我已经找到了答案,但由于我是插座的初学者,所以找不到任何我能理解的内容。
感谢您的帮助。