Package com.zebra.sdk.comm
Class TcpConnection
Object
com.zebra.sdk.comm.ConnectionA
com.zebra.sdk.comm.TcpConnection
- All Implemented Interfaces:
Connection,ConnectionWithWriteLogging,com.zebra.sdk.comm.internal.ConnectionI,IpAddressable
- Direct Known Subclasses:
TcpStatusConnection
public class TcpConnection
extends ConnectionA
implements IpAddressable, com.zebra.sdk.comm.internal.ConnectionI
Establishes a TCP connection to a device
package test.zebra.sdk.comm.examples;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.comm.TcpConnection;
import com.zebra.sdk.printer.ZebraPrinter;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
public class TcpConnectionExample {
public static void main(String[] args) throws Exception {
new TcpConnectionExample().sendZplOverTcp("1.2.3.4");
new TcpConnectionExample().sendCpclOverTcp("1.2.3.4");
new TcpConnectionExample().printConfigLabelUsingDnsName("PrinterName");
}
private void sendZplOverTcp(String theIpAddress) throws ConnectionException {
// Instantiate connection for ZPL TCP port at given address
Connection thePrinterConn = new TcpConnection(theIpAddress, TcpConnection.DEFAULT_ZPL_TCP_PORT);
try {
// Open the connection - physical connection is established here.
thePrinterConn.open();
// This example prints "This is a ZPL test." near the top of the label.
String zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
// Send the data to printer as a byte array.
thePrinterConn.write(zplData.getBytes());
} catch (ConnectionException e) {
// Handle communications error here.
e.printStackTrace();
} finally {
// Close the connection to release resources.
thePrinterConn.close();
}
}
private void sendCpclOverTcp(String theIpAddress) throws ConnectionException {
// Instantiate connection for CPCL TCP port at given address
Connection thePrinterConn = new TcpConnection(theIpAddress, TcpConnection.DEFAULT_CPCL_TCP_PORT);
try {
// Open the connection - physical connection is established here.
thePrinterConn.open();
// This example prints "This is a CPCL test." near the top of the label.
String cpclData = "! 0 200 200 210 1\r\n"
+ "TEXT 4 0 30 40 This is a CPCL test.\r\n"
+ "FORM\r\n"
+ "PRINT\r\n";
// Send the data to printer as a byte array.
thePrinterConn.write(cpclData.getBytes());
} catch (ConnectionException e) {
// Handle communications error here.
e.printStackTrace();
} finally {
// Close the connection to release resources.
thePrinterConn.close();
}
}
private void printConfigLabelUsingDnsName(String dnsName) throws ConnectionException {
Connection connection = new TcpConnection(dnsName, 9100);
try {
connection.open();
ZebraPrinter p = ZebraPrinterFactory.getInstance(connection);
p.printConfigurationLabel();
} catch (ConnectionException e) {
e.printStackTrace();
} catch (ZebraPrinterLanguageUnknownException e) {
e.printStackTrace();
} finally {
// Close the connection to release resources.
connection.close();
}
}
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default TCP port for CPCL devices.static final intThe default TCP port for ZPL devices.booleanFor internal use of the Zebra Printer API only.For internal use of the Zebra Printer API only. -
Constructor Summary
ConstructorsConstructorDescriptionTcpConnection(String address, int port) Initializes a new instance of theTcpConnectionclass.TcpConnection(String address, int port, int maxTimeoutForRead, int timeToWaitForMoreData) Initializes a new instance of theTcpConnectionclass. -
Method Summary
Modifier and TypeMethodDescriptionReturns the address which was passed into the constructor.getConnectionReestablisher(long thresholdTime) Returns aConnectionReestablisherwhich allows for easy recreation of a connection which may have been closed.Returns the port number which was passed into the constructor.Return the IP address as the description.toString()ReturnsTCP:[address]:[port number].
Theaddressandport numberare the parameters which were passed into the constructor.Methods inherited from class com.zebra.sdk.comm.ConnectionA
addWriteLogStream, bytesAvailable, close, getManufacturer, getMaxDataToWrite, getMaxTimeoutForRead, getTimeToWaitForMoreData, isConnected, open, read, read, read, read, readChar, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxDataToWrite, setMaxTimeoutForRead, setReadTimeout, setTimeToWaitForMoreData, waitForData, write, write, writeMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.zebra.sdk.comm.Connection
bytesAvailable, close, getMaxTimeoutForRead, getTimeToWaitForMoreData, isConnected, open, read, read, readChar, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxTimeoutForRead, setTimeToWaitForMoreData, waitForData, write, write, writeMethods inherited from interface com.zebra.sdk.comm.internal.ConnectionI
getManufacturer, getMaxDataToWrite, read, read, setMaxDataToWrite, setReadTimeout
-
Field Details
-
DEFAULT_ZPL_TCP_PORT
public static final int DEFAULT_ZPL_TCP_PORTThe default TCP port for ZPL devices.- See Also:
-
DEFAULT_CPCL_TCP_PORT
public static final int DEFAULT_CPCL_TCP_PORTThe default TCP port for CPCL devices.- See Also:
-
isCardPrinter
public boolean isCardPrinterFor internal use of the Zebra Printer API only. -
serialNumber
For internal use of the Zebra Printer API only.
-
-
Constructor Details
-
TcpConnection
Initializes a new instance of theTcpConnectionclass. This constructor will use the default timeouts forConnection.read(). The default timeout is a maximum of 5 seconds for any data to be received. If no more data is available after 500 milliseconds the read operation is assumed to be complete.
To specify timeouts other than the defaults, use:
TcpConnection(String, int, int, int).- Parameters:
address- the IP Address or DNS Hostname.port- the port number.
-
TcpConnection
Initializes a new instance of theTcpConnectionclass. This constructor will use the specified timeouts forConnection.read(). The timeout is a maximum ofmaxTimeoutForReadmilliseconds for any data to be received. If no more data is available aftertimeToWaitForMoreDatamilliseconds the read operation is assumed to be complete.- Parameters:
address- the IP Address or DNS Hostname.port- the port number.maxTimeoutForRead- the maximum time, in milliseconds, to wait for any data to be received.timeToWaitForMoreData- the maximum time, in milliseconds, to wait in-between reads after the initial read.
-
-
Method Details
-
toString
ReturnsTCP:[address]:[port number].
Theaddressandport numberare the parameters which were passed into the constructor.- Specified by:
toStringin interfaceConnection- Overrides:
toStringin classObject- Returns:
- the connection description string.
- See Also:
-
getSimpleConnectionName
Return the IP address as the description.- Specified by:
getSimpleConnectionNamein interfaceConnection- Returns:
- a human-readable description of the connection.
- See Also:
-
getAddress
Description copied from interface:IpAddressableReturns the address which was passed into the constructor.- Specified by:
getAddressin interfaceIpAddressable- Returns:
- the address used to establish this connection. This can be either a DNS Hostname or an IP address.
- See Also:
-
getPortNumber
Description copied from interface:IpAddressableReturns the port number which was passed into the constructor.- Specified by:
getPortNumberin interfaceIpAddressable- Returns:
- the port number associated with the connection.
- See Also:
-
getConnectionReestablisher
public ConnectionReestablisher getConnectionReestablisher(long thresholdTime) throws ConnectionException Description copied from interface:ConnectionReturns aConnectionReestablisherwhich allows for easy recreation of a connection which may have been closed.- This call should be made while there is still an active connection to the printer (prior to issuing a command which will make the printer non-responsive).
- Specified by:
getConnectionReestablisherin interfaceConnection- Overrides:
getConnectionReestablisherin classConnectionA- Parameters:
thresholdTime- how long the Connection reestablisher will wait before attempting to reconnection to the printer- Returns:
- ConnectionReestablisher
- Throws:
ConnectionException- if the ConnectionReestablisher could not be created.- See Also:
-