public class TcpConnection extends ConnectionA implements IpAddressable
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();
}
}
}
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_CPCL_TCP_PORT
The default TCP port for CPCL devices.
|
static int |
DEFAULT_ZPL_TCP_PORT
The default TCP port for ZPL devices.
|
Constructor and Description |
---|
TcpConnection(String address,
int port)
Initializes a new instance of the
TcpConnection class. |
TcpConnection(String address,
int port,
int maxTimeoutForRead,
int timeToWaitForMoreData)
Initializes a new instance of the
TcpConnection class. |
Modifier and Type | Method and Description |
---|---|
String |
getAddress()
Returns the address which was passed into the constructor.
|
ConnectionReestablisher |
getConnectionReestablisher(long thresholdTime)
Returns a
ConnectionReestablisher which allows for easy recreation of a connection which may have
been closed. |
String |
getPortNumber()
Returns the port number which was passed into the constructor.
|
String |
getSimpleConnectionName()
Return the IP address as the description.
|
String |
toString()
Returns
TCP :[address]:[port number].The address and port number are the parameters which were passed into the constructor. |
addWriteLogStream, bytesAvailable, close, getMaxTimeoutForRead, getTimeToWaitForMoreData, isConnected, open, read, read, readChar, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxTimeoutForRead, setTimeToWaitForMoreData, waitForData, write, write, write
public static final int DEFAULT_ZPL_TCP_PORT
public static final int DEFAULT_CPCL_TCP_PORT
public TcpConnection(String address, int port)
TcpConnection
class. This constructor will use the default
timeouts for Connection.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.TcpConnection(String, int, int, int)
.address
- the IP Address or DNS Hostname.port
- the port number.public TcpConnection(String address, int port, int maxTimeoutForRead, int timeToWaitForMoreData)
TcpConnection
class. This constructor will use the specified
timeouts for Connection.read()
. The timeout is a maximum of maxTimeoutForRead
milliseconds
for any data to be received. If no more data is available after timeToWaitForMoreData
milliseconds
the read operation is assumed to be complete.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.public String toString()
TCP
:[address]:[port number].address
and port number
are the parameters which were passed into the constructor.toString
in interface Connection
toString
in class Object
Connection.toString()
public String getSimpleConnectionName()
getSimpleConnectionName
in interface Connection
Connection.getSimpleConnectionName()
public String getAddress()
IpAddressable
getAddress
in interface IpAddressable
IpAddressable.getAddress()
public String getPortNumber()
IpAddressable
getPortNumber
in interface IpAddressable
IpAddressable.getPortNumber()
public ConnectionReestablisher getConnectionReestablisher(long thresholdTime) throws ConnectionException
Connection
ConnectionReestablisher
which allows for easy recreation of a connection which may have
been closed.getConnectionReestablisher
in interface Connection
getConnectionReestablisher
in class ConnectionA
thresholdTime
- how long the Connection reestablisher will wait before attempting to reconnection to the
printerConnectionException
- if the ConnectionReestablisher could not be created.ConnectionA.getConnectionReestablisher(long thresholdTime)
© 2016 ZIH Corp. All Rights Reserved.