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 Details

    • DEFAULT_ZPL_TCP_PORT

      public static final int DEFAULT_ZPL_TCP_PORT
      The default TCP port for ZPL devices.
      See Also:
    • DEFAULT_CPCL_TCP_PORT

      public static final int DEFAULT_CPCL_TCP_PORT
      The default TCP port for CPCL devices.
      See Also:
    • isCardPrinter

      public boolean isCardPrinter
      For internal use of the Zebra Printer API only.
    • serialNumber

      public String serialNumber
      For internal use of the Zebra Printer API only.
  • Constructor Details

    • TcpConnection

      public TcpConnection(String address, int port)
      Initializes a new instance of the 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.
      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

      public TcpConnection(String address, int port, int maxTimeoutForRead, int timeToWaitForMoreData)
      Initializes a new instance of the 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.
      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