AsyncUdpSocket: Receiving duplicate UDP Packet
AsyncUdpSocket: Receiving duplicate UDP Packet
- Subject: AsyncUdpSocket: Receiving duplicate UDP Packet
- From: Todd Burch <email@hidden>
- Date: Wed, 12 May 2010 22:35:30 -0400
Hi all,
I am using the AsyncUdpSocket class from the AsyncSocket library (http://code.google.com/p/cocoaasyncsocket/) to receive UDP packets.
I'm early in development, and all I'm doing is receiving a UDP packet over a given port, and logging it to the console. However, for some reason my UDP packet (transmitted from other software) appears to be arriving twice! I'm able to verify (via an independent UDP monitoring utility) that the software is only broadcasting each packet once....so something must be wrong with my code. Can someone help? This is my first Cocoa project. The code's not pretty...just doing a concept test right now.
The other software is broadcasting UDP packets that look like this: "X=1.1243533512, Y=18.129583212, Z=194.129182412" (ASCII encoding) and each line is terminated by a newline character.
Here are the relevant bits of my code, from my view controller:
- (void)viewDidLoad {
[super viewDidLoad];
AsyncUdpSocket *aSyncSocket=[[AsyncUdpSocket alloc] initWithDelegate:self]; //We are the delegate for the asynchronous socket object.
[aSyncSocket bindToPort:1234 error:nil]; //We want to listen on port 1234...don't care about errors for now.
[aSyncSocket receiveWithTimeout:-1 tag:1]; //Start listening for a UDP packet.
}
#pragma mark AsyncUdpSocket Delegate Method
//This method is called by the AsyncUdpSocket object when a packet is received:
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
NSString *theLine=[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; //Convert the UDP data to an NSString
NSLog(@"%@", theLine);
[theLine release];
[aSyncSocket receiveWithTimeout:-1 tag:1]; //Listen for the next UDP packet to arrive...which will call this method again in turn.
return YES; //Signal that we didn't ignore the packet.
}
Even though the transmitting software sends out a single packet, like this:
"X=1.12345678, Y=2.3456789, Z=3.4567890" (verified independently),
my console keeps showing this:
"X=1.12345678, Y=2.3456789, Z=3.4567890"
"X=1.12345678, Y=2.3456789, Z=3.4567890"
It's as if the packet is not cleared from the AsyncUdpSocket object's queue until *after* I return YES from the delegate method. If I remove the [aSyncSocket receiveWithTimeout:-1 tag:1]; line from the delegate method, then I receive one packet...but of course, since I'm not continuing to put more "listen" requests into the socket's queue, no more packets are received.
Can anyone who's used AsyncUdpSocket before help out a beginner? Frustrated...
Thanks,
Todd_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden