AsyncUdpSocket如何使用receive [英] AsyncUdpSocket how to use receive

查看:216
本文介绍了AsyncUdpSocket如何使用receive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





我假设这样:

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

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



我把一个NSLog在那个方法,它从来没有被调用。 Well [socket receive,..]是唯一的接收方法,所以我想应该是那个...或者有另一种方法,我必须使用?或者我必须对我的代理或任何...添加一些补充...我只是不知道如何我必须这样做



我搜索asyncUdpSocket示例s),教程,如何(的)和更多,但我只是找不到一个例子。因此,如果任何人想解释它或知道一个坐在一个良好的解释,这将是非常感激。



如果你不知道答案感谢无论如何阅读!

解决方案

我是Objective C的新手(所以忍受我的无知),但我已经能够得到AsyncUdpSocketDelegate接收,大部分。 c


  • 请确保您的类采用了 AsyncUdpSocketDelegate protocol。我不知道这是否是必要的,但它不能伤害。在类头文件中,如下所示:



    @interface | :| super class | < |其他协议|,Asy​​ncUdpSocketDelegate> {


  • 确保您的代理方法已在您的接口和方法签名应该是这样:
    - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost: :(UInt16)port;


  • 请尝试使用非默认方式调用 receiveWithTimeout -zero超时值。可能会给你不同的结果。


  • 使用Wireshark确保你是
    实际上接收UDP数据时和
    ,是。我不是
    试图侮ult你的智力,
    ,但我花了相当多的时间试图
    跟踪网络代码错误在
    的过去,当问题实际上是
    我的网络配置。



  • 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.

    I assume something like this:

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

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

    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

    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!

    解决方案

    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:

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

    2. 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 |your class| : |super class| <|Other protocol(s)|, AsyncUdpSocketDelegate> {

    3. 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;

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

    5. 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.

    这篇关于AsyncUdpSocket如何使用receive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆