Problem with AsyncUdpSocket. Can not receive any data ?!

332 views
Skip to first unread message

eemc...@googlemail.com

unread,
Feb 1, 2011, 5:17:34 AM2/1/11
to CocoaAsyncSocket
Hi

I am at a dead end. I have no idea what else I should try. I connect
to a UDPServer, I can send data, which is received by the server, but
when the server sends a reply I do not get it.

Here is what I have :

RemoteControlViewController.h

#import <UIKit/UIKit.h>
#import "AsyncUdpSocket.h"

@interface RemoteControlViewController : UIViewController
<AsyncUdpSocketDelegate>
{

AsyncUdpSocket *updSocket;
}

@property (nonatomic, retain) AsyncUdpSocket *udpSocket;
- (void) initCommunicationSockets;
- (void)openUDPToSever:(NSString *)server withPort:(NSInteger)port;
- (void)sendTextToServer:(NSString *)t;
- (void)processServerData:(NSString *)data;
@end


RemoteControlViewController.m

#import "RemoteControlViewController.h"

@implementation RemoteControlViewController

@synthesize udpSocket;

- (void) viewDidLoad {
[self initCommunicationSockets];
}

- (void) initCommunicationSockets{
NSString *host = @"10.0.0.1";
int port = 6001;
udpSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
[self openUDPToSever:host withPort:port];
}

- (void)openUDPToSever:(NSString *)server withPort:
(NSInteger)port{
NSError *error = nil;
if ([udpSocket connectToHost:server onPort:port error:&error]) {
DebugLog(@"Connected to Server: %@:%d", server,port);
[udpSocket receiveWithTimeout:10 tag:0];
[self sendTextToServer:@"|TXHELLO|\r"];
} else{
DebugLog(@"Error connecting to Server: %@", error);
}
}

- (void)sendTextToServer:(NSString *)t{
[udpSocket sendData:[t dataUsingEncoding:NSASCIIStringEncoding]
withTimeout:-1 tag:0];
DebugLog (@"Out: %@",t);
}

- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData
*)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
DebugLog(@"onUdpSocket:didReceiveData");
[sock receiveWithTimeout:-1 tag:0];
NSString *e = [[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding];
[self processServerData:e];
[e release];
return YES;
}

- (void)processServerData:(NSString *)data
{
DebugLog(@"processServerData");
}

@end

I hope someone can help me out here.

-eemceebee

Shukuyen

unread,
Feb 1, 2011, 11:23:11 AM2/1/11
to CocoaAsyncSocket
Hi, maybe change the receive timeout to -1?

On Feb 1, 11:17 am, "eemcee...@googlemail.com"

Robbie Hanson

unread,
Feb 1, 2011, 12:17:47 PM2/1/11
to cocoaasy...@googlegroups.com
Some quick tips:

You need to invoke [udpSocket receive...] for each receive you want. This puts you in full control with timeouts, tags, etc.

UDP is a connectionless protocol. You do not need to connect. If you choose to connect, you should read and understand the documentation concerning the connect method in the AsyncUdpSocket header file. If you don't appear to be receiving data, try not connecting your UDP socket. Sometimes servers use different send/receive ports or the IP address isn't what you expected (localhost vs 10.0.0.1).

A great tool to use is always something like wireshark. I like CocoaPacketAnalyzer. This will tell you for sure if your computer is receiving the packets you think it should be receiving.

-Robbie Hanson

> --
> You received this message because you are subscribed to the Google Groups "CocoaAsyncSocket" group.
> To post to this group, send email to cocoaasy...@googlegroups.com.
> To unsubscribe from this group, send email to cocoaasyncsock...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cocoaasyncsocket?hl=en.
>

Reply all
Reply to author
Forward
0 new messages