1

我的第一篇文章,也是我的第一个问题。我目前正在使用基于 UDP 的 Artnet protokoll。试图开发一个读取 Artnet 数据的 Cocoa 应用程序。我决定使用AsyncUDPSocket Cocoa 框架并让它以某种方式工作。所以当我尝试使用

nc -u localhost 6454

我的代表被叫了。如果我用另一个 Artnet 应用程序尝试这个,我的委托永远不会被回调,但我可以在数据包分析器中看到数据包..

我怀疑这一定是由“标签”引起的。有人可以解释(长)标签变量,因为我找不到关于这个值的文档,我在网上找不到答案。另外我可能会补充一点,我对可可开发相对较新,所以也许这是一个非常基本的错误..

下面是我的套接字初始化代码:

 listenSocket_unicast = [[AsyncUdpSocket alloc] initWithDelegate:self];  // This one is not added to the Autorelease pool, so cocoa doesnt delete my socket object.
 listenSocket_broadcast = [[AsyncUdpSocket alloc] initWithDelegate:self];  // This one is not added to the Autorelease pool, so cocoa doesnt delete my socket object.

 // Bind unicast socket..if adress is not the loopback device

 if (![SocketAddress isEqualToString:@LOOPBACK])
 if ([listenSocket_unicast bindToAddress:SocketAddress port:ARTNET_PORT error:nil] == NO)
  {
  NSLog (@"Could not bind unicast socket on Adress %@ and port %i",SocketAddress, ARTNET_PORT);
  return NO;
  }
 else  NSLog (@"Unicast Socket bind to on Adress %@ and port %i",SocketAddress, ARTNET_PORT);
 // Bind broadcast socket..
 if ([listenSocket_broadcast bindToAddress:SocketBroadcastAdress port:ARTNET_PORT error:nil] == NO)
  {
  NSLog (@"Could not bind broadcast socket on Adress %@ and port %i",SocketBroadcastAdress, ARTNET_PORT);
  return NO;
  }
 else
  NSLog (@"Broadcast Socket bind to on Adress %@ and port %i",SocketBroadcastAdress, ARTNET_PORT);




 [listenSocket_unicast receiveWithTimeout:-1 tag:0];
 [listenSocket_broadcast receiveWithTimeout:-1 tag:0];

 [listenSocket_broadcast enableBroadcast:YES error:nil];

和我目前尝试开始工作的代码:

    -(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
 [sock receiveWithTimeout:-1 tag:0];
 NSLog (@"UDP Delegate executed");
 return YES;
}

提前致谢,

马蒂亚斯

4

2 回答 2

3

很高兴您找到了问题,但要真正回答您关于标签变量的问题,它只是一种跟踪消息的方法。标签不会随消息一起发送,但是当收到回复时,它会具有相同的标签,因此您可以组织它们。如果您不需要做那种事情,只需将 tag 保留为 0,您可以忽略它。

于 2010-10-01T18:32:17.153 回答
0

问题是我一开始没有调用 Read 方法。所以阅读从未开始。

于 2010-02-19T13:16:41.597 回答