8

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.

Peter M
  • 7,309
  • 3
  • 50
  • 91
happs
  • 139
  • 2
  • 9
  • 3
    *Strip the last quadrant from the IPAddress ... iterate 1-255* Please Google "subnet mask" – ta.speot.is Feb 04 '12 at 10:39
  • 2
    According to the manufacturer there is a SDP (Star Discovery Protocol) available. You should try to implement it. Because you could 'successfully' open the same port on an unassociated device thinking that you're connected to the printer, if you don't. – Rok Jarc Feb 04 '12 at 10:55

2 Answers2

2

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.

ipmcc
  • 29,581
  • 5
  • 84
  • 147
  • Thanks for the repsonse ipmcc, I am running with my solution for now, except probably going to increase the defualt timeout to 20ms and increase from there on any subsquent searches resulting from no responses. Thanks again! – happs Feb 16 '12 at 04:17
  • 1
    @ipmcc This recent SO question lists how to get the subnet mask http://stackoverflow.com/questions/15156028/how-to-get-subnet-mask-and-broadcast-address-of-personal-hotspot-in-ios – Peter M Aug 09 '13 at 17:50
0

You can quickly winnow the list down from 255 to a smaller number by pinging the broadcast address then looking into your arp cache.

Only works for hosts that respond to broadcast pings.

alfwatt
  • 2,010
  • 2
  • 18
  • 29