asyncudpsocket无法按预期发送UDP

问题描述

| 我在迷你MAC 10.5.8和Simulator 3.1上运行XCODE 3.1.4 我想为一些遥控器发送一个短的UDP字符串,并做了以下代码 基本上,它确实可以在模拟器中编译并运行...但是它从不向目标发送任何UDP。我希望有人可以给我一个线索,为什么它不起作用 我的.H代码
#import <UIKit/UIKit.h>
#import \"AsyncUdpSocket.h\"
#import \"AsyncSocket.h\"

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

}

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

@end
我的.m代码
#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
    

解决方法

        不得不初始化套接字
under - (void) vievDidLoad
socket = [[AsyncUdpSocket alloc] initWithDelegate:self];
然后它按预期工作:-)     ,        如果您正在观看全新项目,则还需要执行以下操作: 1.将CFNetwork添加到项目中的框架 2.将cocaasyncsockets文件添加到您的项目中 说明在这里:http://code.google.com/p/cocoaasyncsocket/wiki/iPhone 请注意有关如何添加CFNetwork的说明,该选项未在可用选项列表中显示。 提示:您将需要使用finder来选择路径,我是mac新手,所以花了我一段时间才弄清楚我必须使用Go / Go To Folder...。