开发者

Checking private network for local devices with a certain port open (iPhone/Objective C)

开发者 https://www.devze.com 2023-03-22 06:36 出处:网络
I\'ve spent the whole day creating a system between my Mac and iPhone where i have used cocoaasyncsocket to create a listen server on my mac and a client on my iPhone. The basic idea is to leave the a

I've spent the whole day creating a system between my Mac and iPhone where i have used cocoaasyncsocket to create a listen server on my mac and a client on my iPhone. The basic idea is to leave the app on the mac running while the computer is on and then when I wish to transfer data from the iPhone app, fire up the app an it connects and sends the data... I have this system working exactly how I want it to function however I have 1 issue what I have been trying to solve for about 4 hours in total!

I wanted to create something what scans my wireless network for my mac with the listener running... I thought this would be simple, but I was wrong. I have searched high and low with no luck on the case and I am using stackoverflow as my last resort.

My current plan was to "autoscan" by retrieving the internal IP of the iPhone (ie 192.168.1.94) then use that to figure out what the other IP's on the network will be (192.168.1.0-254), now I know what IP's to scan i can loop through each one and check to see if the port is open/I get a response.

Now I want to do this as quick as possible however I haven't been able to get ANYTHING to give me accurate results...

Using connectToAddress:error: in the cocoaasyncsocket will simply just return true for every one of the 255 different IP addresses, so will any other reachability functions that I have come across... I have read that it is because they only check to see if the connection gets made and don't care about what happens at the other end so I need to think of something else.

My only other solution that I can think of, is to maybe ping each internal IP and see if i get a response but im not sure if this is going to take up too much time having to go through 255 IP addresses... and then, once i get what IP's are active I still then have to check to see if the port is open somehow :/

开发者_开发技巧

If anyone here knows how it can be done or has any better idea how I can check for the open port (i'm not very good with networking) I would be VERY grateful.

Thanks for reading,

Liam


Ok, I had a pay around today and I managed to get it working using Bonjour!

As it took me time to figure it all out I thought I will help anyone else out...

First off, on the listener side we need to set up a NSNetService, this can be done like so:

    listenService = [[NSNetService alloc] initWithDomain:@"" type:@"_appname._tcp" name:@"Display Name" port:2427];
    [listenService setDelegate:self]; //make sure you include the NSNetServiceDelegate
    [listenService publish];

You can then plug into the NSNetServiceDelegate to make sure the service was published successfully and I used Bonjour Browser to check that my service was running fine (and it was)...

Then on the client we need to search for the service using [NSNetServiceBrowser][3]... This can be done like so:

    serviceBrowser = [[NSNetServiceBrowser alloc] init];
    [serviceBrowser setDelegate:self]; //remember to include NSNetServiceBrowserDelegate
    [serviceBrowser searchForServicesOfType:@"_appname._tcp" inDomain:@""];

If you then include the NSNetServiceBrowserDelegate methods you can listen in to

netServiceBrowser:didFindService:moreComing:

You must then retain the service, give it a delegate and then resolve the service... Then if you listen into the NSNetServiceDeligate for the netServiceDidResolveAddress: you can then run the following code to convert the sockaddr into a readable IP address:

#include <arpa/inet.h>

-(void)netServiceDidResolveAddress:(NSNetService *)sender {
for (NSData* data in [sender addresses]) {

    char addressBuffer[100];

    struct sockaddr_in* socketAddress = (struct sockaddr_in*) [data bytes];

    int sockFamily = socketAddress->sin_family;

    if (sockFamily == AF_INET ) {//|| sockFamily == AF_INET6) { /*only support ipv4 atm*/

        const char* addressStr = inet_ntop(sockFamily,
                                           &(socketAddress->sin_addr), addressBuffer,
                                           sizeof(addressBuffer));

        int port = ntohs(socketAddress->sin_port);

        if (addressStr && port) {
            //you now have the services IP and Port.. all done
        }
    }
}
[sender release];}

Hope this helps anyone who is stuck with this.. Note that I borrowed parts of other samples/ instructions into one complete post what explains the whole system.

Enjoy.


I haven't worked with it myself but basically Bonjour is actually what you are looking for it's purpose is publishing and discovery of services

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号