How to listen on a specific port on iPhone ios4.2?

3.3k views Asked by At

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.

1

There are 1 answers

0
MoKeS On BEST ANSWER

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;


}