我的第一篇文章,也是我的第一个问题。我目前正在使用基于 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;
}
提前致谢,
马蒂亚斯