开发者

asyncudpsocket does not send UDP as expected

开发者 https://www.devze.com 2023-03-09 18:24 出处:网络
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

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开发者_如何学Python.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


Had to init the socket

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

Then it worked as expected :-)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消