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:
Make sure the self
class that you are initializing as your socket delegate is the class that you're expecting the callbacks on.
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> {
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;
Try calling receiveWithTimeout
with a non-zero timeout value. May give you different results.
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 *socket=[[AsyncUdpSocket alloc]initWithDelegate:self];
//receiveWithTimeout is necessary or you won't receive anything
[socket receiveWithTimeout:-1 tag:2]; //-------here
NSData *data=[@"Hello from iPhone" dataUsingEncoding:NSUTF8StringEncoding];
[socket sendData:data toHost:bchost port:9003 withTimeout:-1 tag:1];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With