Wiljan
Wiljan

Reputation: 31

asyncudpsocket does not send UDP as expected

I have XCODE 3.1.4 running ona mini MAC 10.5.8 and Simulator 3.1 I want to send a short UDP string for some remote control and have made the following code

Basicly it does compile and run in the simulator ... but it nevers send any UDP to the target. I hope someone can give me a clue why it does not work

My .H code

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

@interface ChangeLabelViewController : UIViewController {
IBOutlet UILabel *label ;
AsyncUdpSocket *socket;

}

-(IBAction) ChangeLabel;
-(IBAction) ResetLabel;

@end

My .m code

#import "ChangeLabelViewController.h"

@implementation ChangeLabelViewController

-(IBAction) ChangeLabel
{
label.text = @"Hello";
}

-(IBAction) ResetLabel
{
label.text = @"Empty";

NSLog(@"%s", __PRETTY_FUNCTION__);

NSString * string = @"Testing iPhone";
NSString * address = @"192.168.1.11";
UInt16 port = 1234;
NSData * data = [string dataUsingEncoding: NSUTF8StringEncoding];

if ([socket sendData:data toHost:address port:port withTimeout:- 1 tag:1])  label.text = @"Send";
// if ([socket sendData:data toHost:address port:port withTimeout:-1 tag:1] == YES) label.text = @"Yes";
// if ([socket sendData:data toHost:address port:port withTimeout:-1 tag:1] == NO) then label.text = @"No";
;        

} 

@end

Upvotes: 0

Views: 1854

Answers (2)

Tereus Scott
Tereus Scott

Reputation: 682

If you are staring from a brand new project you will also need to do the following: 1. add CFNetwork to your frameworks in the project 2. add the cocaasyncsockets files to your project

Instructions are here: http://code.google.com/p/cocoaasyncsocket/wiki/iPhone Pay attention to the instructions on how to add CFNetwork, it is not shown in the list of available options. Hint: you will need to use the finder to select a path, I am a mac novice so it took me a while to figure out that I had to use Go/Go To Folder....

Upvotes: 0

Wiljan
Wiljan

Reputation: 31

Had to init the socket

under - (void) vievDidLoad
socket = [[AsyncUdpSocket alloc] initWithDelegate:self];

Then it worked as expected :-)

Upvotes: 1

Related Questions