asyncudpsocket不按预期发送UDP

时间:2011-05-31 14:46:13

标签: udp asyncsocket

我有XCODE 3.1.4在mini MAC 10.5.8和Simulator 3.1上运行 我想为一些远程控制发送一个简短的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

2 个答案:

答案 0 :(得分:1)

必须初始化套接字

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

然后按预期工作: - )

答案 1 :(得分:0)

如果您正在盯着一个全新的项目,您还需要执行以下操作: 1.将CFNetwork添加到项目中的框架中 2.将cocaasyncsockets文件添加到项目中

说明在这里:http://code.google.com/p/cocoaasyncsocket/wiki/iPhone 请注意有关如何添加CFNetwork的说明,它不会显示在可用选项列表中。 提示:你需要使用finder来选择一个路径,我是一个mac新手所以我花了一段时间才弄清楚我必须使用Go / Go To Folder ....