AsyncUdpSocket 如何使用接收如何使用、AsyncUdpSocket

由网友(男影帝)分享简介:我正在尝试让 iPhone 程序在模拟器上运行.我的问题是接收 UDP 数据.我使用 asyncUdpSocket.如果我创建一个套接字并使用 sendData:(NSData) toHost:,... 它工作正常.I am trying to get a program for iPhone running on...

我正在尝试让 iPhone 程序在模拟器上运行.我的问题是接收 UDP 数据.我使用 asyncUdpSocket.如果我创建一个套接字并使用 sendData:(NSData) toHost:,... 它工作正常.

I am trying to get a program for iPhone running on the simulator. My problem is with receiving UDP data. I use asyncUdpSocket. If I make a socket and use sendData:(NSData) toHost:,... well it works fine.

我想不明白的是接收函数是如何工作的.

The think i can just not figure out is how the receive functions works.

我假设是这样的:

socket = [[AsyncUdpSocket alloc] initWithDelegate:self];
[socket bindToPort:8000] error:nil] //returns YES
[socket receiveWithTimeout:-1 tag:1];  

我相信它应该然后调用方法 -(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long) fromHost:(NSString *)host port:(UInt16)port

I believe it should then call the method -(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long) fromHost:(NSString *)host port:(UInt16)port

好吧,我在该方法中放置了一个 NSLog,它从未被调用过.那么 [socket receive,..] 是唯一的接收方法,所以我想它应该是那个......或者我必须使用另一种方法吗?还是我必须对我的代表做一些补充或其他什么......我只是不知道我该怎么做

Well i put a NSLog in that method and it is never called. Well [socket receive,..] is the only receive method so i guess it should be that one... or is there another method i have to use? Or do I have to make some additions to my delegate or whatever... I just can't figure out how i have to do it

我搜索了 asyncUdpSocket 示例、教程、操作方法等,但我找不到示例.因此,如果有人想解释它或知道有一个很好的解释,将不胜感激.

I searched for asyncUdpSocket example(s), tutorials, how to('s) and more but I just can't find a example. So if anyone would like to explain it or knows a sit with a good explanation it would be very appreciated.

如果您不知道答案,无论如何感谢您的阅读!

If you don't know the answer thanks anyway for reading!

推荐答案

我是 Objective C 的新手(所以请忍受我对它的无知),但在大多数情况下,我已经能够让 AsyncUdpSocketDelegate 接收.需要尝试/确认的几件事:

I'm new with Objective C (so bear with my ignorance of it), but I have been able to get AsyncUdpSocketDelegate to receive, for the most part. A few things to try/confirm:

确保您作为套接字委托初始化的 self 类是您期望回调的类.

Make sure the self class that you are initializing as your socket delegate is the class that you're expecting the callbacks on.

确保你的类采用 AsyncUdpSocketDelegate 协议.我不确定这是否真的有必要,但它不会受到伤害.在您的班级标题中,如下所示:

Make sure your class adopts the AsyncUdpSocketDelegate protocol. I'm not sure if this is actually necessary, but it couldn't hurt. In your class header, looks like:

@interface |你的班级|: |超级类|<|其他协议|,Asy​​ncUdpSocketDelegate>{

确保在接口和实现中声明了委托方法.方法签名应如下所示:- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port;

Make sure you have your delegate methods declared in your interface and implementation. The method signature should look like this: - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port;

尝试使用非零超时值调用 receiveWithTimeout.可能会给你不同的结果.

Try calling receiveWithTimeout with a non-zero timeout value. May give you different results.

使用 Wireshark 确保您是实际上接收 UDP 数据时和你认为你在哪里.我不是试图侮辱你的智商,但我花了很长时间尝试追踪网络代码错误问题实际上是过去我的网络配置.

Use Wireshark to make sure you are in fact receiving UDP data when and where you think you are. I'm not trying to insult your intelligence, but I've spent quite a while trying to track down network code bugs in the past when the issue was actually my network configuration.

阅读全文

相关推荐

最新文章