According to " How Network Printing Works, " it really depends on the type of printer and protocol that it supports. If you know the ip and port used by your printer, and if your printer supports SNMP (only for protocol selection), you can use the SNMP protocoll protocol to request your printer. There is a Java lib SNMP4j that can help you with this. I would suggest not using it if the printer, ip and port never (!) Change for your installation. This is due to the fact that you may encounter several problems.
- How to detect an unknown printer?
- How to open the port used by the printer?
- How to detect the protocol used by the printer?
Assume that the above questions are not a problem and suggest that each printer will support SNMP. How to get information from this? Besides using the mentioned java lib, you can use snmpget on linux from the terminal. The syntax is as follows:
snmpget -v1 -c public host-ip OID
OID is an object identifier for each property of your printer, reaching from pagecount to toner cartridge information. If you do not add an OID, you will get the entire list of available OIDs. The bottom line is that although all OIDs are standardized, the use of OIDs differs from brand to brand and from printer model to printer model. For my HP, the following work:
snmpget -v1 -c public 192.168.1.10 iso.3.6.1.2.1.43.17.6.1.5.1.2
and returns
iso.3.6.1.2.1.43.17.6.1.5.1.2 = STRING: "Ready"
OID OID returns the printer status for my HP. But if I use the same OID on my Canon, I will get
Error in packet Reason: (noSuchName) There is no such variable name in this MIB. Failed object: iso.3.6.1.2.1.43.17.6.1.5.1.2
Therefore, even SNMP is not applicable at all, not to mention other available protocols.
Given all this information, the easiest way in my view is to check whether it is possible to establish a connection to the printer on one of the printer's shared ports through this code.
boolean available = false; try { String serverAddress = "192.168.1.10"; Socket s = new Socket(serverAddress, 9100); s.close(); available = true; } catch (IOException e) { available = false; } System.out.println("printer available: " + available);
Of course, this only works if you already know the IP address of the printer.