Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all devices' IP Addresses on wifi network iOS sdk without bonjour?

I am trying to get a list of all IP's on a LAN network. The reason for this is I am writing an application that uses the STAR TSP100LAN receipt printer.

The process for obtaining the IP address of the printer is quite cumbersome for the end user. It involves turning the printer off, holding the paper feed button, turning the printer back on, waiting 15 seconds for the printer to obtain an IP address through DHCP and then finally spitting out a receipt with this information on it.

Seeing as the printer is not Bonjour enabled is it possible to get the IP address through other methods?

Any help is much appreciated! I hope this isn't a repeat question, but through my searching I can't seem to find a solution!

UPDATE: Ok after a bit of thinking I have come up with a pseudo-solution:

  1. Determine the iPad's current IPAddress through NSHost.

  2. Strip the last quadrant from the IPAddress

  3. Using the stripped string as a prefix, iterate 1-255 for the last quadrant

  4. Each iteration, attempt to open a port to the given address using the printer's sdk If I get a valid response, I know that the IP is a printer If not I exclude the IP from the available printers list.

So far this has been working, I set a timeout of 5 milleseconds for each port open attempt. But have found that this can return some null results despite there actually being a printer on the network with an IP Address assigned.

Perhaps if I get a null result the first time I should increase the timeout to 15 milleseconds for a second attempt at searching.

like image 929
happs Avatar asked Feb 03 '12 08:02

happs


1 Answers

Your approach of polling the local /8 subnet is probably the best you can do. I can't find any API to get more detailed information about the network interface (i.e. subnet mask) in iOS. (Although using the subnet mask would be a more correct way to determine the range to iterate, if you could get it.)

As you've seen, 5ms is a pretty tight interval; In my experience, 15ms can STILL be pretty tight for a TCP connection over WiFi. As a next step, I would suggest parallelizing the polling of the range, and thereby enabling you to extend the interval you're willing to wait. The simplest way would probably be to use GCD. You could also start this polling process in the background before the user explicitly needs the printer, which might improve the user-perceived responsiveness of your app.

Alternately, you could use the CFSocket API to open all these connections (CFSocketCreate, CFSocketConnectToAddress, and friends) and get parallelism by servicing them all on the main thread with callbacks/the runloop. Then, as those callbacks come in, make a note of which addresses answer on the given port. Unless the printer isn't using TCP for some reason, this should be workable. Once you know which addresses answer on that port at all, you can iterate over that (hopefully much smaller) list connecting with the printer SDK itself. This approach will give you even more (and way more elegant) parallelism than spawning a huge number of GCD threads, but can be difficult to get your head around if you've not worked with runloops before.

Hope that helps.

like image 69
ipmcc Avatar answered Oct 21 '22 18:10

ipmcc