2

我有一个小的 MAC OS X 应用程序,它应该向服务器发送一个 UDP 数据报。

我想使用可可 asyncudpsockets ( http://code.google.com/p/cocoaasyncsocket/ ) 来执行此操作,但我遇到的问题是我调用了“sendData”方法但没有任何反应。

[socket sendData:[NSData dataWithBytes:stream length:length] 
          toHost:host 
            port:(uint16)port 
     withTimeout:5 
             tag:1];

流是一个字节 * 包含数据报主机是一个 NSString 的 IP 地址。

有谁能够帮我?

4

1 回答 1

1

您是否正确设置了委托?

[socket setDelegate:self];

然后 :

[socket connectToHost:(NSString *)host
               onPort:(UInt16)port
          withTimeout:(NSTimeInterval)timeout
                error:(NSError **)errPtr];

然后在委托方法中:

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    [sock writeData:[NSData dataWithBytes:stream length:length] withTimeout:TIMEOUT_NONE tag:TAG_HEADER];
}

希望它会有所帮助;-)

于 2011-10-27T13:57:52.690 回答