Cover V10, I05

may2001.tar


Listing 6 tcpip.c

 * Functions to open a network device and write Intermec or
 * Zebra command objects to the network device.
 * Author: Ed Schaefer
 */
#include    <stdio.h>
#include    <sys/socket.h>
#include    <netinet/in.h>
#include    <netdb.h>
#include    "intermec.h" 
#include    "zebra.h" 
#include    "cemarks.h" 

/*
 * Print TCP/IP label
 * 1 for Intermec and 2 for Zebra
 */
int bc_tcpip(char *network_address, int port_number, int printer_choice)
{
   int lpr_fd;

   if(printer_choice == 1)
      {  /* Intermec TCP/IP */
      printf("Intermec  Network Printer test\n");

      printf("Loading Intermec CE graphics character\n");
      lpr_fd=open_network_device(network_address, port_number);
      sleep(2); 
      write_load_commands(intermec_ce, lpr_fd);

      printf("Loading Intermec TCP Format \n");
      write_load_commands(load_intermec_label, lpr_fd);

      printf("Printing First Intermec Label \n");
      write_load_commands(print_intermec_label1, lpr_fd);

      printf("Printing Second Intermec Label \n");
      write_load_commands(print_intermec_label2, lpr_fd); 
      close(lpr_fd); 
      }

   if(printer_choice == 2)
      {  /* Zebra TCP/IP */
      printf("Zebra Network Printer test\n");

      printf("Loading Zebra CE graphics character\n");
      lpr_fd=open_network_device(network_address, port_number);
      sleep(2);
      write_load_commands(zebra_ce, lpr_fd);

      printf("Loading Zebra TCP/IP Format \n");
      write_load_commands(load_zebra_label, lpr_fd);

      printf("Printing First Zebra Label \n");
      write_load_commands(print_zebra_label1, lpr_fd);

      printf("Printing Second Zebra Label \n");
      write_load_commands(print_zebra_label2, lpr_fd);
      close(lpr_fd);
      }
}

/*
 * This function opens network device 'device_name' on
 * 'port_number' and return the socket descriptor, lpr_fd.  Terminate if error.
 */
int open_network_device(char *device_name, int port_number)
{
   int lpr_fd;
   struct sockaddr_in pin;
   struct hostent *hp;

  /* Resolve the specified host name which is the printer */
  if((hp = gethostbyname(device_name)) == 0)  
    { 
    perror("gethostbyname");
    exit(1); 
    }
 
   /* Complete the socket structure */
   bzero(&pin,sizeof(pin));
   pin.sin_family = AF_INET;
   pin.sin_addr.s_addr = ((struct in_addr*)(hp->h_addr))->s_addr;
   pin.sin_port = htons(port_number);
  
   /* Get an Internet Domain Socket */
   if ((lpr_fd=socket(AF_INET,SOCK_STREAM,0)) == -1) 
     {
     perror("socket");
     exit(1); 
     }

   /* Connect to the printer */
   if (connect(lpr_fd,&pin,sizeof(pin)) == -1) 
     {
     perror("connect");
     exit(1); 
     }
   return(lpr_fd);
} /* end tcpip.c */