如何在iPhone ios4.2上监听特定端口?

发布于 2024-10-17 19:00:53 字数 1474 浏览 1 评论 0原文

我对我的实际项目感到疯狂......

我有一个在特定端口上发送数据的设备。我的目标是检测该端口上的所有此类设备。 示例:我在同一网络上有 4 个此类设备,我将使用 iPhone 检测所有这些设备。

它与 JAVA 发现工具配合使用,现在我正尝试在我的 iPhone 上执行此操作。

JAVA发现工具中使用的步骤如下:

  • 创建DatagramSocket(JAVA)
  • 获取广播地址(255.255.255.255)
  • 创建DatagramPacket(JAVA)
  • 在端口30303的广播地址上发送数据包
  • 进入接收模式
  • 由于套接字,它得到答案数据包
  • 从设备中提取 IP 和主机名

我尝试使用 AsyncUdpSocket 类重新执行相同的步骤: http://code.google.com/p/cocoaasyncsocket/

这是我迄今为止尝试过的代码:

NSError *e;
NSString *d = @"abcd";
NSData *data = [d dataUsingEncoding:NSUTF8StringEncoding];


ssocket = [[AsyncUdpSocket alloc]initWithDelegate:self];
[ssocket setDelegate:self];



[ssocket enableBroadcast:YES error:&e];


[ssocket connectToHost:@"255.255.255.255" onPort:30303 error:&e];


[ssocket sendData:data withTimeout:-1 tag:0];

[ssocket receiveWithTimeout:-1 tag:0];

委托 - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData: (NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port从未被调用...

我不知道出了什么问题..你能给我你的吗关于这个主题的意见?如果我使用正确的方法,看看我是否做错了什么?

先感谢您 !!

MoKeS

编辑:

我使用wireshark 通过 JavaDiscoverTool 和我的应用程序来嗅探网络流量。 事实上,两个数据包(iphone 和 javadiscovertool 的)之间的区别在于源端口。 事实上,在我的应用程序中,源端口与目标端口不同,我认为这导致了问题。 我需要找到一种方法来强制我发送的数据包上的源端口...

有什么想法吗?它位于 AsyncUdpSocket 类上。

I'm going crazy about my actual project...

I've got a device which is sending data on a specific port. My goal is to detect all this kind of device on that port.
Example : I've got 4 this kind of devices on the same network and I would with the iPhone detect all this device.

It works with a JAVA discover tool and now i'm trying to do it on my iPhone.

There are the steps used in the JAVA discover tool :

  • Create a DatagramSocket (JAVA)
  • Get the broadcast address (255.255.255.255)
  • Create DatagramPacket (JAVA)
  • Send the packet on the broadcast adresse with the port 30303
  • Go in receive mode
  • Due to the socket, it get the answers packets
  • Extract the ip and the hostname from the device

I tried to repoduce the same steps by using the AsyncUdpSocket class : http://code.google.com/p/cocoaasyncsocket/

And here is the code I tried so far :

NSError *e;
NSString *d = @"abcd";
NSData *data = [d dataUsingEncoding:NSUTF8StringEncoding];


ssocket = [[AsyncUdpSocket alloc]initWithDelegate:self];
[ssocket setDelegate:self];



[ssocket enableBroadcast:YES error:&e];


[ssocket connectToHost:@"255.255.255.255" onPort:30303 error:&e];


[ssocket sendData:data withTimeout:-1 tag:0];

[ssocket receiveWithTimeout:-1 tag:0];

The delegate - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)portis never called ...

I don't know what is wrong .. Can you please give me your opinions about this subject ? If i'm using the right methodology and see If I did anything wrong ??

Thank you in advance !!

MoKeS

EDIT :

I used wireshark to sniff the network traffic with the JavaDiscoverTool and my application.
In fact the difference between the 2 packets (iphone's and javadiscovertool's) is the Source Port.
Indeed, on my application, the source port is different than the destination port and I think that is causing the issue.
I need to find a way to force the source port on the packet i'm sending ...

Any ideas ? It's on the AsyncUdpSocket class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2024-10-24 19:00:53

我找到了解决方案。

使用:

NSError *e;
NSString *d = @"Discovery: Who is out there?\0\n";
NSData *data = [d dataUsingEncoding:NSUTF8StringEncoding];


ssocket = [[AsyncUdpSocket alloc]initWithDelegate:self];
[ssocket setDelegate:self];

[ssocket bindToPort:PORT error:&e];

[ssocket enableBroadcast:YES error:&e];


//[ssocket connectToHost:@"255.255.255.255" onPort:30303 error:&e];


//[ssocket sendData:data withTimeout:-1 tag:0];

[ssocket sendData:data toHost:@"255.255.255.255" port:30303 withTimeout:-1 tag:0];


[ssocket receiveWithTimeout:-1 tag:0];

和代表:

- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
    NSString* aStr;

    aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];


    NSLog(@"didreceivedata : %@", aStr);

    return NO;


}

I found the solution.

use :

NSError *e;
NSString *d = @"Discovery: Who is out there?\0\n";
NSData *data = [d dataUsingEncoding:NSUTF8StringEncoding];


ssocket = [[AsyncUdpSocket alloc]initWithDelegate:self];
[ssocket setDelegate:self];

[ssocket bindToPort:PORT error:&e];

[ssocket enableBroadcast:YES error:&e];


//[ssocket connectToHost:@"255.255.255.255" onPort:30303 error:&e];


//[ssocket sendData:data withTimeout:-1 tag:0];

[ssocket sendData:data toHost:@"255.255.255.255" port:30303 withTimeout:-1 tag:0];


[ssocket receiveWithTimeout:-1 tag:0];

And the delegate :

- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
    NSString* aStr;

    aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];


    NSLog(@"didreceivedata : %@", aStr);

    return NO;


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文