送我使用这个库:https://github.com/robbiehanson/CocoaAsyncSocketUDP数据包没有在iPhone上
我的测试代码:
#import <UIKit/UIKit.h>
@class GCDAsyncUdpSocket;
@interface ThirdViewController : UIViewController
{
GCDAsyncUdpSocket *udpSocket;
}
.M:
#import "ThirdViewController.h"
#import "DDLog.h"
#import "DDTTYLogger.h"
#import "GCDAsyncUdpSocket.h"
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
@implementation ThirdViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[udpSocket bindToPort:7744 error:nil];
[udpSocket beginReceiving:nil];
[udpSocket enableBroadcast:YES error:nil];
//[udpSocket connectToHost:@"127.0.0.1" onPort:55544 error:nil];
NSData *data = [@"test" dataUsingEncoding:NSUTF8StringEncoding];
//[udpSocket sendData:data withTimeout:-1 tag:0];
[udpSocket sendData:data toHost:@"127.0.0.1" port:55544 withTimeout:-1 tag:0];
}
但不发送包。我使用这个数据包嗅探器:http://sourceforge.net/projects/packetpeeper/
在库中有一个客户端(Mac)的例子,我看到他的包。我试图在真实设备上运行应用程序,但也没有发送任何内容(地址当然是另一个)。问题是什么?
感谢您的回答!我改变了监听端口的传输方式。我试图发送eth1,并且我没有看到该包。从我看到的mac应用程序发送的数据包,即使地址是127.0.0.1 – ifau 2012-03-25 16:23:04
您如何确定要使用的'toHost'地址?您是否输入了屏幕上的iDevice的IP地址,因为除非您使用广播地址,否则这是您必须使用的地址 - 如果您的桌面在192.168.1.5上,则广播地址为192.168。 1.255'。您可以从“网络参数窗格”中确定您的桌面的IP地址。 – Petesh 2012-03-25 21:50:38
我的电脑连接到路由器,地址为'192.168.10.101'。当我尝试从iDevice发送数据包时,我选择这个地址作为'toHost'。 IDevice的地址为'192.168.10.102'。当我尝试从模拟器发送时,我选择地址“127.0.0.1”。我也尝试选择'192.168.10.101'。 enableBroadcast我设置为'NO'。 – ifau 2012-03-26 06:59:08