2

I have a hardware device Acquisition unit. This device itself act as wifi router and it will send data. I want to connect it from iPhone/iPad.

I heard that using IP address and port number we can connect to the wifi router using socket programming. But I don't have any idea about this socket program to connect to the wifi using IP address and port number.

For this I have tried to get the IP address of a connected wifi router using following code.

NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
    // Loop through linked list of interfaces
    temp_addr = interfaces;
    while(temp_addr != NULL) {
        if(temp_addr->ifa_addr->sa_family == AF_INET) {
            // Check if interface is en0 which is the wifi connection on the iPhone
            if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                // Get NSString from C String
                address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];

            }

        }

        temp_addr = temp_addr->ifa_next;
    }
}
// Free memory
freeifaddrs(interfaces);
return address;

If it is possible to connect to the wifi router using ip address and port number could you please help me with some code or idea.

Please help me..

Thanks

1 Answers1

2

The code you pasted basically returns the device's own IP address, which does not help at all, unless you want to guess router's IPs. You actually can get correct router IP address, see questions below:

After you have this, you can easily connect to it via a normal TCP/IP connection socket, see the following links for more information:

Or to make things even easier for you, there are a couple of community libraries that will make connecting to a server like a breeze:

My suggestion is to read through those documents first, so you understand the basics of networking in iOS / OS X (which is actually fairly similar to Unix in this aspect). After try the libraries and wrappers you can find on CocoaPods and GitHub. If you are stopped by some specific issue at that point, we might be able to help you. We cannot do all the work for you.

So to answer: Yes, it is possible.

Community
  • 1
  • 1
Legoless
  • 10,942
  • 7
  • 48
  • 68
  • Thanks alot Legoless. I didn't ask for total source code. Definitely I will try with your links. Once again thanks alot for your long explanation... –  Nov 11 '14 at 12:34
  • One more thing. I am getting router using with my above code. I have checked now. I am getting router address. But I have tried many times to get the port number of the router.Please help me if you know. Thanks –  Nov 11 '14 at 12:42
  • You should know the port number of the router, usually device manufacturer provides this. You can use some kind of network command line tool to figure it out though (such as nmap). Then hardcode the port into your app. There could be many ports open, but you did not specify to what kind of server you are connecting to. If it is a HTTP server, try port 80, or check other common ports. – Legoless Nov 11 '14 at 12:46
  • Thanks alot for your support. I will try with your comment.Thanks alot once again. –  Nov 11 '14 at 12:52