Solved

Print Server problem

Posted on 2007-03-26
23
286 Views
Last Modified: 2008-01-09
Dear Experts:

Im trying to make a print server for an existing sales program, so it can print on a ticket printer on another computer. I've tried to program this out, but i can only print once, cuz a portinuseException shows if i try to print the second ticket.
Can anyone help me with this code? Or even better, do anyone knows where can i get a printserver code? (just wishful thinking :) )

Thx!!


----------------------------------- Print Client ---------------------------------------


public class PrintClient extends Thread {
   
    private static Logger logger=Logger.getLogger(PrintClient.class);
   
    private Socket socket;
    DataOutputStream DOSCliente;
    DataInputStream DISCliente;
    String Puerto;
    int Printer;
    private PrintWriter out;
    private BufferedReader in;
   
    private final String CRLN="\r\n";
   
    public PrintClient(Socket socket) {
        this.socket=socket;
        try{
           
            logger.info("Generating Outputstream");
            DOSCliente=new DataOutputStream(socket.getOutputStream());
            logger.info("Generating Inputstream");
            DISCliente=new DataInputStream(socket.getInputStream());
            logger.info("1: Streams ready. Starting...");
             start();
        }catch(Exception e){
            logger.error("Error ocurred: ", e);
        }
       
    }
   
    public PrintClient(Socket socket, int SelectedPrinter, String PuertoSeleccionado) {
        this.socket=socket;
        try{
           
            logger.info("Generating Outputstream");
            DOSCliente=new DataOutputStream(socket.getOutputStream());
            logger.info("Generating Inputstream");
            DISCliente=new DataInputStream(socket.getInputStream());
            Puerto = PuertoSeleccionado;
            Printer = SelectedPrinter;
            logger.info("2: Streams ready. Starting...");
             start();
        }catch(Exception e){
            logger.error("Error ocurred: ", e);
        }
       
    }
   
   
    public void run(){
       
        String send="";
        logger.info("PrinterClient Running, what should I do?");
        try{
            if(socket.isConnected()){
                while(!socket.isClosed() && !send.equalsIgnoreCase("quit")){
                   
                    logger.info("Starting printing test");
                    int impresora = Integer.parseInt(SmartClientSettings.getInstance().getSelectedPrinter());
                    String PuertoSeleccionado = SmartClientSettings.getInstance().getSerialPort();
                    logger.info("Printing on printer  " + impresora + " :" + PuertoSeleccionado);
                    String datos[] = new String[11];
                    datos[0]=TicketPrinter.centrar("+------------------------------+");
                    datos[1]=TicketPrinter.centrar("|          Tienda PRO          |");
                    datos[2]=TicketPrinter.centrar("+------------------------------+");
                    datos[3]=TicketPrinter.centrar("Print Test");
                    datos[4]=TicketPrinter.centrar("Succesfully");
                    datos[5]=" ";
                    datos[6]=" ";
                    datos[7]=" ";
                    datos[8]=" ";
                    datos[9]=" ";
                    datos[10]=" ";
                    int i = 0;
                    logger.info("Attempting to writeUTF to printserver");
                    while(i < 11){
                        logger.info("\"" + datos[i] + "\"");
                        this.DOSCliente.writeUTF(datos[i]);
                        i++;
                    }
                   
                    logger.info("Sending EXIT to let the server know its done...");
                    this.DOSCliente.writeUTF("EXIT");
                    logger.info("\"EXIT" + "\"");
                   
                    this.DOSCliente.writeUTF("");
                    logger.info("\"\"");
                   
                    this.DOSCliente.flush();
                    send="quit";
                }
            }
            logger.info("PrintClient Communication over");
            socket.close();
            logger.info("Attempted to close the communication socket. Socket status now: " + socket.isConnected());
        }catch(Exception e){
            logger.info("Error on PrintClient thread",e);
        }
    }
}



----------------------------------------- Print Server -------------------------------------------


    public PrintServer(int Printer, String PuertoSeleccionado) {
         try
        {
            logger.info("Starting server...");
            ServerSocket socketServidor = new ServerSocket(9999);
            logger.info("Starting server on port 9999");
            while (true)
            {
               
                    logger.info("Accepting Connections");
                    Socket ncliente = socketServidor.accept();
                    logger.info("Connection accepted. Starting Net Printing");
                    Runnable nuevoCliente = new PrintNetTicket(ncliente, Printer, PuertoSeleccionado);            
                    logger.info("Creating Printing Thread");
                    Thread hilo = new Thread(nuevoCliente);
                    logger.info("Initiating Thread");
                    hilo.start();
                    logger.info("Printing finished. Closing client...");
                    ncliente.close();
                    logger.info("Closed Client.");
               
            }
        } catch (Exception e)
        {
            logger.info("An error ocurred on PrintServer function @ PrintServer.class");
        }
    }

    public void run() {
    }
   
}

class PrintNetTicket implements Runnable{
    static Logger logger = Logger.getLogger(PrintNetTicket.class);
    Socket cliente;
    DataOutputStream DOSCliente;
    DataInputStream DISCliente;
    String Puerto;
    int Printer;
    boolean ocupado=true;
   
    public PrintNetTicket(Socket ncliente, int SelectedPrinter, String PuertoSeleccionado){
       
        cliente=ncliente;
        try {
            ocupado=true;
            logger.info("Generating Outputstream");
            DOSCliente=new DataOutputStream(cliente.getOutputStream());
            logger.info("Generating Inputstream");
            DISCliente=new DataInputStream(cliente.getInputStream());
            Puerto=PuertoSeleccionado;
            Printer=SelectedPrinter;
            logger.info("Streams ready");
        }
        catch (Exception e){
            logger.info("An error ocurred on PrintNetTicket");
           
        }
    }
   
    public void run(){
        String datos[] = new String[100];
        String recibir = "";
        try {
           
            int i=0;
            logger.info("Reading data...");
            do{
                recibir=DISCliente.readUTF();
                if (recibir!="EXIT"){
                    datos[i]=recibir;
                    logger.info(datos[i]);
                }
                i++;
            }
            while(recibir!="EXIT" || recibir!="");
        } catch (IOException ex) {
            logger.info("Error en reading data.");
           
        }
        logger.info("Sending data to printer");
        boolean checaImpresion=false;
        checaImpresion= TicketPrinter.print(Puerto, datos);
        logger.info("Printing done with result : " + checaImpresion);
        ocupado=false;
    }
   
    public boolean isBusy(){
        return this.ocupado;
    }
}

------------------  TicketPrinter (Print function) --------------------------------



public static boolean print(String puerto, String datos[]) throws java.lang.NullPointerException {
        logger.info("Function print(puerto,datos) @ TicketPrinter");
        SerialDriver serial =  new SerialDriver(puerto);
        logger.info("Opening port");
        if(!serial.open()){
            logger.info("Couldnt print on printer port");
            return false;
        }
       
        boolean respuesta = true;
       
        int impresora = Integer.parseInt(SmartClientSettings.getInstance().getSelectedPrinter());
                         
        if (impresora == 0){
           
            for(int i=0; i < datos.length; i++){
               
                if(!serial.write(convierte(datos[i]))){
                    respuesta = false;
                    logger.info("Couldnt convert data to bytes");
                }
               
            }
           
           
        } else if ( impresora == 1 || impresora == 2 || impresora == 3 ) {
           
            for(int i=0; i < datos.length; i++){
               
                if(!serial.write(datos[i].getBytes())){
                    logger.info("Couldnt convert data to bytes");
                    respuesta = false;
                }
               
            }
           
        }
       
        logger.info("Closing port...");
        serial.close();
        logger.info("Port apparently closed");
       
        return respuesta;
    }
0
Comment
Question by:memozebadua
  • 11
  • 11
23 Comments
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
What line does the exception occur at? Have you tried to print the stack trace of the exception using e.printStackTrace () - if not, pls do.
0
 
LVL 10

Expert Comment

by:ADSLMark
Comment Utility
This seems odd:
--
Thread hilo = new Thread(nuevoCliente);
hilo.start();
ncliente.close();
--
You start a thread for receiving the data from the client and immediatly after starting this thread you close it. What if the thread you just started doesn't get any time from the system? then the connection is closed and i believe thereby also the in/output streams. You should let the thread close itself when it's done. You already pass the ncliente to the PrintNetTicket class.

Anyway, that was odd,
Mark
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Good catch.

Also: >> recibir!="EXIT"

should be ! recibir.equalsIgnoreCase ( "EXIT" )
0
 

Author Comment

by:memozebadua
Comment Utility
Thx for the quick response guys!
Couldn't answer because was swamped with work, but corrected what you said and this are the results. Anyway Im here now. I appreciate the help youre giving me.


Youre right Mark, i forgot to take that line out, was trying to close the listening port so no other print job enters while the printer busy, but one problem at a time
Thx for remembering me :)


Thanks Mayankeagle, i did what you asked me to, well, not exactly though. Im working with javaw, so it wont display the black command window, so i sent everything to a logger, here is the logger output. Let me know if it works for you or need more info, or anything.


2007-03-26 19:36:42,374 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintServer - Connection Accepted. Starting OnLan Printing...
2007-03-26 19:36:42,375 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Generating Outputstream
2007-03-26 19:36:42,375 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Generating Inputstream
2007-03-26 19:36:42,375 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Streams ready
2007-03-26 19:36:42,375 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintServer - Creating Printing Thread
2007-03-26 19:36:42,376 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintServer - Starting Thread
2007-03-26 19:36:42,376 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintServer - Accepting connections...
2007-03-26 19:36:42,377 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Reading data...
2007-03-26 19:36:42,384 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - +------------------------------+
2007-03-26 19:36:42,386 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - |          Tienda PRO          |
2007-03-26 19:36:42,386 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - +------------------------------+
2007-03-26 19:36:42,387 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -         Print Test On LAN
2007-03-26 19:36:42,387 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -             Successful
2007-03-26 19:36:42,387 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,390 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,390 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,390 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,391 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,392 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,392 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - EXIT
2007-03-26 19:36:42,392 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,393 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Error en reading data. Probably because printing has finished or hasnt started.
2007-03-26 19:36:42,394 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Sending received info to printer
2007-03-26 19:36:42,394 [Thread-7] INFO  com.storecheck.detailconnect.utils.TicketPrinter - TicketPrinter: print(port,data) function
2007-03-26 19:36:42,395 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - SerialDriver Constructor
2007-03-26 19:36:42,395 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - PortId test passed
2007-03-26 19:36:42,395 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - DefaultPort asign: SerialDriver(port) method
2007-03-26 19:36:42,395 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - DefaultPort: /dev/ttyS0
2007-03-26 19:36:42,396 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - Default port: /dev/ttyS0
2007-03-26 19:36:42,396 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - Open defaultPort Method
2007-03-26 19:36:42,396 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - Found 1 port
2007-03-26 19:36:42,396 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - and its Serial
2007-03-26 19:36:42,397 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - Found 1 port
2007-03-26 19:36:42,397 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - and its Serial
2007-03-26 19:35:51,403 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - This is the preferred port
2007-03-26 19:35:51,403 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Trying to open the port with javax
2007-03-26 19:35:51,419 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Opened the port Successfully!!
2007-03-26 19:35:51,421 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - OutputStream OK
2007-03-26 19:35:51,424 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Setting SerialPort parameters
2007-03-26 19:35:51,425 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Done setting SerialPort params
2007-03-26 19:36:44,404 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - Found 1 port
2007-03-26 19:36:44,404 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - and its Parallel
2007-03-26 19:35:51,426 [Thread-5] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Converting data to Bytes: [B@1aa0a15
2007-03-26 19:35:51,427 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-26 19:35:51,427 [Thread-5] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Converting data to Bytes: [B@7bfc04
2007-03-26 19:35:51,427 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-26 19:35:51,427 [Thread-5] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Converting data to Bytes: [B@1de8aa8
2007-03-26 19:35:51,427 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-26 19:35:51,428 [Thread-5] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Converting data to Bytes: [B@1613b53
2007-03-26 19:35:51,428 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-26 19:35:51,428 [Thread-5] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Converting data to Bytes: [B@7df60a
2007-03-26 19:35:51,428 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-26 19:35:51,429 [Thread-5] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Converting data to Bytes: [B@1a4181c
2007-03-26 19:35:51,429 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-26 19:35:51,429 [Thread-5] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Converting data to Bytes: [B@2af8f5
2007-03-26 19:35:51,429 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-26 19:35:51,430 [Thread-5] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Converting data to Bytes: [B@13ec2dd
2007-03-26 19:35:51,430 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-26 19:35:51,430 [Thread-5] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Converting data to Bytes: [B@16b61c3
2007-03-26 19:35:51,430 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-26 19:35:51,431 [Thread-5] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Converting data to Bytes: [B@df4bfc
2007-03-26 19:35:51,431 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-26 19:35:51,431 [Thread-5] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Converting data to Bytes: [B@1f5b4d1
2007-03-26 19:35:51,431 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-26 19:35:51,432 [Thread-5] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Converting data to Bytes: [B@323274
2007-03-26 19:35:51,432 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-26 19:35:51,432 [Thread-5] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Converting data to Bytes: [B@18e80a6
2007-03-26 19:35:51,432 [Thread-5] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer

*************************************************************

2007-03-26 19:36:42,374 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintServer - Connection Accepted. Starting OnLan Printing...
2007-03-26 19:36:42,375 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Generating Outputstream
2007-03-26 19:36:42,375 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Generating Inputstream
2007-03-26 19:36:42,375 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Streams ready
2007-03-26 19:36:42,375 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintServer - Creating Printing Thread
2007-03-26 19:36:42,376 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintServer - Starting Thread
2007-03-26 19:36:42,376 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintServer - Accepting connections...
2007-03-26 19:36:42,377 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Reading data...
2007-03-26 19:36:42,384 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - +------------------------------+
2007-03-26 19:36:42,386 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - |          Tienda PRO          |
2007-03-26 19:36:42,386 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - +------------------------------+
2007-03-26 19:36:42,387 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -         Print Test On LAN
2007-03-26 19:36:42,387 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -             Successful
2007-03-26 19:36:42,387 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,390 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,390 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,390 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,391 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,392 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,392 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - EXIT
2007-03-26 19:36:42,392 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-26 19:36:42,393 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Error en reading data. Probably because printing has finished or hasnt started.
2007-03-26 19:36:42,394 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Sending received info to printer
2007-03-26 19:36:42,394 [Thread-7] INFO  com.storecheck.detailconnect.utils.TicketPrinter - TicketPrinter: print(port,data) function
2007-03-26 19:36:42,395 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - SerialDriver Constructor
2007-03-26 19:36:42,395 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - PortId test passed
2007-03-26 19:36:42,395 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - DefaultPort asign: SerialDriver(port) method
2007-03-26 19:36:42,395 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - DefaultPort: /dev/ttyS0
2007-03-26 19:36:42,396 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - Default port: /dev/ttyS0
2007-03-26 19:36:42,396 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - Open defaultPort Method
2007-03-26 19:36:42,396 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - Found 1 port
2007-03-26 19:36:42,396 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - and its Serial
2007-03-26 19:36:42,397 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - Found 1 port
2007-03-26 19:36:42,397 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - and its Serial
2007-03-26 19:36:42,397 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - This is the preferred port
2007-03-26 19:36:42,397 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - Trying to open the port with javax
2007-03-26 19:36:44,403 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - Couldnt open port with javax
2007-03-26 19:36:44,404 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - javax.comm.PortInUseException: Port currently owned by SimpleWrite
2007-03-26 19:36:44,404 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - Found 1 port
2007-03-26 19:36:44,404 [Thread-7] INFO  com.storecheck.detailconnect.utils.SerialDriver - and its Parallel
2007-03-26 19:36:44,405 [Thread-7] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Couldnt open the printer port. Notifying app...
2007-03-26 19:36:44,405 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Printing finished with result : false





Sorry for the length of this. The ****** line i put it in so i can explain this.
Before the **** line its the first PrintOnLan test and it prints fine, nice and smooth. But when i try to print a second time, it wont work. I dont know why, and the first try it should have another line like this one : Printing finished with result : false  , but with a true result. It seems it stops somewhere but dunno why.
All of the above is the print server side (the computer with the printer connected).
The other one goes like this:



23843 18:39:20,421 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.ui.panels.SettingsPanel  - Connection Succedeed!!
23859 18:39:20,437 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintClient  - Generating Outputstream
23859 18:39:20,437 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintClient  - Generating Inputstream
23859 18:39:20,437 [AWT-EventQueue-0] INFO  com.storecheck.detailconnect.utils.PrintClient  - 1: Streams ready. Starting...
23859 18:39:20,437 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - PrinterClient Running, what should I do?
23859 18:39:20,437 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - Printing test starting
23859 18:39:20,437 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - Attempting to writeUTF to printserver
23859 18:39:20,437 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - "+------------------------------+"
23875 18:39:20,453 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - "|          Tienda PRO          |"
23875 18:39:20,453 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - "+------------------------------+"
23875 18:39:20,453 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - "       Print Test On LAN"
23875 18:39:20,453 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - "            Successful"
23875 18:39:20,453 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - " "
23875 18:39:20,453 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - " "
23890 18:39:20,468 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - " "
23890 18:39:20,468 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - " "
23890 18:39:20,468 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - " "
23890 18:39:20,468 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - " "
23890 18:39:20,468 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - Sending EXIT to let the server know its done...
23890 18:39:20,468 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - "EXIT"
23906 18:39:20,484 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - ""
23906 18:39:20,484 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - PrintClient Communication over
23906 18:39:20,484 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - Attempting to close the communication socket
23906 18:39:20,484 [Thread-7] INFO  com.storecheck.detailconnect.utils.PrintClient  - Attempted to close the communication socket. Socket status now: true




Mmm It seems that its not closing the socket, even do the code says clearly socket.close();
Any ideas?



Thanks in advance.

NeMo
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
In the run () method of PrintNetTicket, put a finally block and close the socket there. In the constructor, call getInputStream () before getOutputStream (). Make sure the socket is closed in finally blocks wherever it needs to be closed on the server as well as client.
0
 

Author Comment

by:memozebadua
Comment Utility
Thx mayankeagle.
Im doing the changes now but im running out of time in the office, so i will continue doing them at home.

If everything goes wrong (and it will, Murphy's law), i'll have the changes tmmw morning. Thanks for the advices.

See ya.

Nemo
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> so i will continue doing them at home

Sure :)

>> and it will, Murphy's law

Ah its my favourite law of life because I haven't seen anything more true ;-)
0
 

Author Comment

by:memozebadua
Comment Utility
Hello guys!

Mmm been very busy with this, trying to make it work, but so far i accomplished little...

Let me paste some updated code  :)  with your fixes and mine too.


-----------------------------------  Print Server -----------------------------------------

public class PrintServer extends Thread{
    static Logger logger = Logger.getLogger(PrintServer.class);
    PrintNetTicket hilo= null;
    int Printr;
    String portSelectd;
    private boolean running=false;
    private ArrayList<Thread> clients;
   
    /** Creates a new instance of PrintServer */
   
    public PrintServer(int Printer, String PuertoSeleccionado) {
        running=true;
        this.Printr = Printer;
        this.portSelectd = PuertoSeleccionado;
        Thread cleaner = new Thread("Connection cleaner"){
            public void run(){
                while(running){
                    try{
                        Thread.sleep(5000);
                        purge();
                    }catch(InterruptedException e){

                    }
                }
            }
        };
        cleaner.start();
        start();
    }

    public void run() {
         try {
             
            logger.info("Starting server...");
            ServerSocket socketServidor = new ServerSocket(9999);
            //socketServidor.setSoTimeout(5000);
            logger.info("Server started in port 9999");
            while (true)
            {
                    try {
                       
                        logger.info("Accepting connections...");
                        Socket ncliente = socketServidor.accept();
                        logger.info("Creating Print Thread");
                        PrintNetTicket nuevoCliente = new PrintNetTicket(ncliente, this.Printr, this.portSelectd);
                        logger.info("Starting Thread");
                        nuevoCliente.start();
                        logger.info("Adding to Array");
                        getClients().add(nuevoCliente);
                        logger.info("Run process done.");
                       
                    } catch (java.lang.NullPointerException e){
                        logger.info("NullPointerException in adding to array of PrintServer run.");
                        logger.info(e);
                    } catch (java.net.BindException e){
                        JOptionPane.showMessageDialog(null,"There's already a server running!\nTry closing the app or using the open one","E00155",2);
                    }catch (Exception e){    
                        logger.info("An error ocurred in PrintServer @ PrintServer.class");
                        logger.info(e);
                    }
                   
                   
            }
        } catch (Exception e){    
            logger.info("An error ocurred in PrintServer @ PrintServer.class");
            logger.info(e);
           
        }finally{
           
        }
    }
   
    public ArrayList<Thread> getClients() {
        return clients;
    }
 
    public synchronized void stopServer(){
        running = false;
    }
   
    public synchronized void closeAllConnections(){
        for (Thread elem : clients) {
            elem.interrupt();
        }
    }
   
   
    public synchronized void purge(){
        //System.out.println("Conexiones: " + getClients().size());
        Stack<Thread> s = new Stack<Thread>();
       
        for (int i=0; i < getClients().size(); i++) {
            Thread serverThread = getClients().get(i);
            if(!serverThread.isAlive()){
                s.push(serverThread);
            }
        }
       
        while(!s.empty()){
            //WarehouseServerThread st = s.pop();
            //clients.remove(st);
            getClients().remove(s.pop());
        }
       
    }
   
}

class PrintNetTicket extends Thread{
    static Logger logger = Logger.getLogger(PrintNetTicket.class);
    Socket cliente;
    DataOutputStream DOSCliente;
    DataInputStream DISCliente;
    String Puerto;
    int Printer;
    boolean ocupado=true;
    private ArrayList<ClientListener> listeners;
    private boolean bConnected = true;
   
    public PrintNetTicket(Socket ncliente, int SelectedPrinter, String PuertoSeleccionado){
       
        cliente=ncliente;
        try {
            ocupado=true;
            logger.info("Generating Inputstream");
            DISCliente=new DataInputStream(cliente.getInputStream());
            logger.info("Generating Outputstream");
            DOSCliente=new DataOutputStream(cliente.getOutputStream());
            Puerto=PuertoSeleccionado;
            Printer=SelectedPrinter;
            logger.info("Streams ready");
        }
        catch (Exception e){
            logger.info("An error ocurred in PrintNetTicket");
            logger.info(e);
           
        }
    }
   
    public void run(){
        String datos[] = new String[100];
        String recibir = "";
        try {
           
            int i=0;
            logger.info("Reading data...");
            do{
                recibir=DISCliente.readUTF();
                if (!recibir.equalsIgnoreCase("EXIT")){
                    datos[i]=recibir;
                    logger.info(datos[i]);
                }
                i++;
            }
            while( ( !recibir.equalsIgnoreCase("EXIT") || !recibir.equalsIgnoreCase("") ) && bConnected == true );
        } catch (java.io.EOFException e) {
            logger.info("Transmision over (End of File)");
        } catch (IOException ex) {
            logger.info("Error en reading data. Probably because printing has finished or hasnt started. ");
            logger.info(ex);
        }

        logger.info("Print job sent to printer on local port");
        boolean checaImpresion=false;
        checaImpresion= TicketPrinter.print(Puerto, datos);
        logger.info("Printing finished with result : " + checaImpresion);
        try{
            this.cliente.close();
            bConnected=false;
        }catch(IOException e){
            logger.error("Error closing socket", e);
        }
       
    }
   
    public boolean isBusy(){
        return this.ocupado;
    }
   
    public boolean isConnected(){
        return this.bConnected;
    }
   
    public void SetAvailable(boolean estado){
        this.ocupado = estado;
    }
   
    public void close(){
        try {
            cliente.close();
        } catch (IOException ex) {
            logger.info("Error @ closing connection");
            logger.info(ex);
        }
    }
   
    public void addClientListener(ClientListener listener){
        listeners.add(listener);
    }
   
}



-----------------------------------  Print Client -----------------------------------------

The only difference is


        }finally{
            logger.info("PrintClient Communication over. Closing socket...");
            try {
                socket.close();
            } catch (IOException ex) {
                logger.info(ex);
            }
            logger.info("Socket closed (it seems). Socket status now: " + socket.isConnected());
        }


at the end of the file, replacing the old one without the try.





Mmmm and the logger may also help us   :)



2007-03-27 18:16:54,539 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - Starting server...
2007-03-27 18:16:54,540 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - Server started in port 9999
2007-03-27 18:16:54,540 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - Accepting connections...
2007-03-27 18:16:59,685 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - Creating Print Thread
2007-03-27 18:16:59,689 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Generating Inputstream
2007-03-27 18:16:59,690 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Generating Outputstream
2007-03-27 18:16:59,691 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Streams ready
2007-03-27 18:16:59,692 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - Mirror Client test : /dev/ttyS1
2007-03-27 18:16:59,692 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - Starting Thread
2007-03-27 18:16:59,692 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - Adding mirror to Array
2007-03-27 18:16:59,693 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - NullPointerException in some point of PrintServer run.
2007-03-27 18:16:59,693 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - java.lang.NullPointerException
2007-03-27 18:16:59,694 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - Accepting connections...
2007-03-27 18:16:59,694 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Reading data...
2007-03-27 18:16:59,710 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - +------------------------------+
2007-03-27 18:16:59,714 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - |          Tienda PRO          |
2007-03-27 18:16:59,714 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - +------------------------------+
2007-03-27 18:16:59,714 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -        Print On LAN Test
2007-03-27 18:16:59,715 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -            Successful
2007-03-27 18:16:59,715 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -  
2007-03-27 18:16:59,715 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -  
2007-03-27 18:16:59,715 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -  
2007-03-27 18:16:59,717 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -  
2007-03-27 18:16:59,718 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -  
2007-03-27 18:16:59,718 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -  
2007-03-27 18:16:59,725 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-27 18:16:59,727 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Transmision over (End of File)
2007-03-27 18:16:59,728 [Thread-8] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Print job sent to printer on local port
2007-03-27 18:16:59,734 [Thread-8] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Print(puerto,datos) function @  TicketPrinter
2007-03-27 18:16:59,735 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - SerialDriver Constructor
2007-03-27 18:16:59,735 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - PortID OK... Jumping to the other Constructor method...
2007-03-27 18:16:59,735 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - SerialDriver(port) Method
2007-03-27 18:16:59,735 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - DefaultPort: /dev/ttyS1
2007-03-27 18:16:59,736 [Thread-8] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Lets open the selected port
2007-03-27 18:16:59,736 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Open Method
2007-03-27 18:16:59,736 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Found 1 port
2007-03-27 18:16:59,736 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - and its Serial
2007-03-27 18:16:59,737 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Found 1 port
2007-03-27 18:16:59,740 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - and its Serial
2007-03-27 18:16:59,740 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - This is the preferred port
2007-03-27 18:16:59,740 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Trying to open port with Javax...
2007-03-27 18:16:59,757 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Opened the port Successfully with Javax!!
2007-03-27 18:16:59,757 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Obtaining the OutputStream...
2007-03-27 18:16:59,759 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - OutputStream OK
2007-03-27 18:16:59,759 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Setting Serial parameters
2007-03-27 18:16:59,759 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Done setting SerialPort Params
2007-03-27 18:16:59,759 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Found 1 port
2007-03-27 18:16:59,760 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - and its Parallel
2007-03-27 18:16:59,760 [Thread-8] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Data converted to Bytes: [B@1e2161d
2007-03-27 18:16:59,761 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-27 18:16:59,761 [Thread-8] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Data converted to Bytes: [B@1a034d
2007-03-27 18:16:59,761 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-27 18:16:59,762 [Thread-8] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Data converted to Bytes: [B@1cee792
2007-03-27 18:16:59,762 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-27 18:16:59,763 [Thread-8] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Data converted to Bytes: [B@c5577c
2007-03-27 18:16:59,763 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-27 18:16:59,763 [Thread-8] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Data converted to Bytes: [B@1f2af1c
2007-03-27 18:16:59,763 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-27 18:16:59,763 [Thread-8] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Data converted to Bytes: [B@1c6866d
2007-03-27 18:16:59,764 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-27 18:16:59,764 [Thread-8] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Data converted to Bytes: [B@a36b53
2007-03-27 18:16:59,764 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-27 18:16:59,764 [Thread-8] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Data converted to Bytes: [B@db3331
2007-03-27 18:16:59,765 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-27 18:16:59,765 [Thread-8] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Data converted to Bytes: [B@fe0fd9
2007-03-27 18:16:59,765 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-27 18:16:59,765 [Thread-8] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Data converted to Bytes: [B@1c293f8
2007-03-27 18:16:59,765 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-27 18:16:59,767 [Thread-8] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Data converted to Bytes: [B@180a8b7
2007-03-27 18:16:59,767 [Thread-8] INFO  com.storecheck.detailconnect.utils.SerialDriver - Data sent correctly to printer
2007-03-27 18:17:44,336 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - Creating Print Thread
2007-03-27 18:17:44,336 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Generating Inputstream
2007-03-27 18:17:44,337 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Generating Outputstream
2007-03-27 18:17:44,337 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Streams ready
2007-03-27 18:17:44,337 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - Mirror Client test : /dev/ttyS1
2007-03-27 18:17:44,337 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - Starting Thread
2007-03-27 18:17:44,338 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - Adding mirror to Array
2007-03-27 18:17:44,338 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - NullPointerException in some point of PrintServer run.
2007-03-27 18:17:44,338 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - java.lang.NullPointerException
2007-03-27 18:17:44,338 [Thread-6] INFO  com.storecheck.detailconnect.utils.PrintServer - Accepting connections...
2007-03-27 18:17:44,339 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Reading data...
2007-03-27 18:17:44,353 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - +------------------------------+
2007-03-27 18:17:44,355 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - |          Tienda PRO          |
2007-03-27 18:17:44,356 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - +------------------------------+
2007-03-27 18:17:44,356 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -        Print On LAN Test
2007-03-27 18:17:44,356 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -            Successful
2007-03-27 18:17:44,356 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -  
2007-03-27 18:17:44,357 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -  
2007-03-27 18:17:44,358 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -  
2007-03-27 18:17:44,358 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -  
2007-03-27 18:17:44,363 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -  
2007-03-27 18:17:44,365 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -  
2007-03-27 18:17:44,375 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket -
2007-03-27 18:17:44,375 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Transmision over (End of File)
2007-03-27 18:17:44,375 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Print job sent to printer on local port
2007-03-27 18:17:44,376 [Thread-10] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Print(puerto,datos) function @  TicketPrinter
2007-03-27 18:17:44,376 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - SerialDriver Constructor
2007-03-27 18:17:44,376 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - PortID OK... Jumping to the other Constructor method...
2007-03-27 18:17:44,376 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - SerialDriver(port) Method
2007-03-27 18:17:44,376 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - DefaultPort: /dev/ttyS1
2007-03-27 18:17:44,377 [Thread-10] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Lets open the selected port
2007-03-27 18:17:44,377 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - Open Method
2007-03-27 18:17:44,379 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - Found 1 port
2007-03-27 18:17:44,380 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - and its Serial
2007-03-27 18:17:44,380 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - Found 1 port
2007-03-27 18:17:44,380 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - and its Serial
2007-03-27 18:17:44,380 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - This is the preferred port
2007-03-27 18:17:44,380 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - Trying to open port with Javax...
2007-03-27 18:17:46,385 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - Couldnt open port with javax
2007-03-27 18:17:46,386 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - javax.comm.PortInUseException: Port currently owned by SimpleWrite
2007-03-27 18:17:49,241 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - Found 1 port
2007-03-27 18:17:49,241 [Thread-10] INFO  com.storecheck.detailconnect.utils.SerialDriver - and its Parallel
2007-03-27 18:17:49,241 [Thread-10] INFO  com.storecheck.detailconnect.utils.TicketPrinter - Couldnt open the printer port
2007-03-27 18:17:49,258 [Thread-10] INFO  com.storecheck.detailconnect.utils.PrintNetTicket - Printing finished with result : false




So far, as you can see, i can print once for every time i reopen the app.
Still dont know why. Hope you can see something i dont.

Thanks a lot


Nemo
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>>  if(socket.isConnected()){

Is an unreliable check.

Also try reading the inputstream first on the print client and then the server.

The log is too long to analyze. Can you tell what you want it to be like and what is different?
0
 

Author Comment

by:memozebadua
Comment Utility
Hi mayankeagle!

Mmm maybe ill just take that line off the code...

And the inputstream first on the client... mmm i read this when i click on a "Test" button. Can i just read it before then (like when loading the class) ?


Mmm about the log, sorry, i just tried to give the most info :)  ,  I'll explain it now:


Basically, the logger says that the second time the print test is done a javax.comm.PortInUseException is generated, although the code does close the port (well, so far to my understanding)


I cleaned up the log, so we can understand it:


...
...

Reading data...

+------------------------------+        <---- This is my print test that i send to the printer
|          Tienda PRO          |
+------------------------------+
       Print On LAN Test
           Successful
 
 
 
 
 
 
Transmision over (End of File)                 < ---- On the other side, i receive the data
Print job sent to printer on local port <-- Once received, i snd it to the print function
Print(puerto,datos) function @  TicketPrinter
SerialDriver Constructor
PortID OK... Jumping to the other Constructor method...
SerialDriver(port) Method
DefaultPort: /dev/ttyS1
Lets open the selected port
Trying to open port with Javax...
Opened the port Successfully with Javax!!
Obtaining the OutputStream...
OutputStream OK
Setting Serial parameters
Done setting SerialPort Params

Data converted to Bytes: [B@1e2161d       <--- Data converted to bytes
Data sent correctly to printer                              just me checking if data is sent OK
Data converted to Bytes: [B@1a034d
Data sent correctly to printer
Data converted to Bytes: [B@1cee792
Data sent correctly to printer
Data converted to Bytes: [B@c5577c
Data sent correctly to printer
Data converted to Bytes: [B@1f2af1c
Data sent correctly to printer
Data converted to Bytes: [B@1c6866d
Data sent correctly to printer
Data converted to Bytes: [B@a36b53
Data sent correctly to printer
Data converted to Bytes: [B@db3331
Data sent correctly to printer
Data converted to Bytes: [B@fe0fd9
Data sent correctly to printer
Data converted to Bytes: [B@1c293f8
Data sent correctly to printer
Data converted to Bytes: [B@180a8b7
Data sent correctly to printer                        <--- Print job finished!
                                                                             But here it should be like this:



                                                                             Closing port...
                                                                             Port apparently closed
                                                                             Printing finished with result : true




Creating Print Thread                                   <---- Heres the second print test
Generating Inputstream                                       The one that does not print
Generating Outputstream                                     cuz the port is still open of
Streams ready                                                       the above test
Mirror Client test : /dev/ttyS1
Starting Thread
Adding mirror to Array
NullPointerException in some point of PrintServer run.
java.lang.NullPointerException
Accepting connections...
Reading data...
+------------------------------+
|          Tienda PRO          |
+------------------------------+
       Print On LAN Test
           Successful
 
 
 
 
 
Transmision over (End of File)
Print job sent to printer on local port
Print(puerto,datos) function @  TicketPrinter
SerialDriver Constructor
PortID OK... Jumping to the other Constructor method...
SerialDriver(port) Method
DefaultPort: /dev/ttyS1
Lets open the selected port
Open Method
Found 1 port
and its Serial
Found 1 port
and its Serial
This is the preferred port
Trying to open port with Javax...                           <--- This is what happens:
Couldnt open port with javax
javax.comm.PortInUseException: Port currently owned by SimpleWrite
Found 1 port
and its Parallel
Couldnt open the printer port
Printing finished with result : false



Can you help me?
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
I don't see clients = new ArrayList <Thread> () being done anywhere/
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:memozebadua
Comment Utility
Duh! ::embarrasing::
I forgot to code that line... it should be just above
ServerSocket socketServidor = new ServerSocket(9999);

... mmm let me try it now, and see if it works...

Thx
0
 

Author Comment

by:memozebadua
Comment Utility
Mmmm it definitely get rid of that awful java.lang.NullPointerException and it does help closing socket connections (thanks BTW), but it doesnt helps closing the javax.comm.connection. The log remain the same (but 2 lines of nullpointerexception).

Any other ideas?

Thx

Nemo
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Where does it give you the error and where does it not?

Can you print the stack trace of the exception using e.printStackTrace () in the catch blocks? You wil know where it is occuring and what is null in a minute,
0
 

Author Comment

by:memozebadua
Comment Utility
Ok, lets try it one more time...

I did not know how to put the printstacktrace into the logger, thats why i did not post it the first time, it kept saying void is not permitted, but i found a friends code with the solution to that logger situation   :)


Mmm lets see then. The awful logger:


Starting server...
Server started in port 9999
Accepting connections...
Creating Print Thread
Generating Inputstream
Generating Outputstream
Streams ready
Starting Thread
Adding to Array
Run process done.
Accepting connections...
Reading data...
+------------------------------+         <---- This is my print test that i sent to the printer
|          Tienda PRO          |
+------------------------------+
       Print On LAN Test
           Successful
 
 
 
 
 
 

                                                        <--- On the other side, i receive the data
                                                        <--- EOFException: i assume because
                                                               transmission is over
                                                     
java.io.EOFException
      at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:310)
      at java.io.DataInputStream.readUTF(DataInputStream.java:545)
      at java.io.DataInputStream.readUTF(DataInputStream.java:522)
      at com.storecheck.detailconnect.utils.PrintNetTicket.run(PrintServer.java:165)


Print job sent to printer on local port               <-- I send it to the print function
Starting print(port,data) function @ TicketPrinter.class
SerialDriver Constructor
PortID OK... Jumping to the other Constructor method...
SerialDriver(port) Method
DefaultPort: /dev/ttyS1
Lets try to open the selected port...
Open Method
Found 1 port
and its Serial
Found 1 port
and its Serial
This is the preferred port
Trying to open port with Javax...
Opened the port Successfully with Javax!!
Obtaining the OutputStream...
OutputStream OK
Setting Serial parameters
Done setting SerialPort Params
Found 1 port
and its Parallel
Data to Bytes: [B@1f5b4d1                        <--- Data converted to bytes
Data sent correctly to printer                      <-- just me checking if data is OK
Data to Bytes: [B@323274
Data sent correctly to printer
Data to Bytes: [B@18e80a6
Data sent correctly to printer
Data to Bytes: [B@a030d6
Data sent correctly to printer
Data to Bytes: [B@e63606
Data sent correctly to printer
Data to Bytes: [B@1e4eb5b
Data sent correctly to printer
Data to Bytes: [B@17b2b2
Data sent correctly to printer
Data to Bytes: [B@1d4d493
Data sent correctly to printer
Data to Bytes: [B@1d03a4e
Data sent correctly to printer
Data to Bytes: [B@1a3e41f
Data sent correctly to printer
Data to Bytes: [B@d5cabc
Data sent correctly to printer                        <--- Print job finished!
                                                                             But here it should be like this:



                                                                             Closing port...
                                                                             Port apparently closed
                                                                             Printing finished with result : true




Creating Print Thread                                   <---- Here is the second print test
Generating Inputstream                                       The one that does not print
Generating Outputstream                                     cuz the port is still open of
Streams ready                                                       the above test
Starting Thread
Adding to Array
Run process done.
Accepting connections...
Reading data...
+------------------------------+
|          Tienda PRO          |
+------------------------------+
       Print On LAN Test
           Successful
 



 
 

java.io.EOFException
      at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:310)
      at java.io.DataInputStream.readUTF(DataInputStream.java:545)
      at java.io.DataInputStream.readUTF(DataInputStream.java:522)
      at com.storecheck.detailconnect.utils.PrintNetTicket.run(PrintServer.java:165)


Print job sent to printer on local port
Starting print(port,data) function @ TicketPrinter.class
SerialDriver Constructor
PortID OK... Jumping to the other Constructor method...
SerialDriver(port) Method
DefaultPort: /dev/ttyS1
Lets try to open the selected port...
Open Method
Found 1 port
and its Serial
Found 1 port
and its Serial
This is the preferred port
Trying to open port with Javax...                         <<<<------- This is what happens

Couldnt open port with javax
javax.comm.PortInUseException: Port currently owned by SimpleWrite
      at javax.comm.CommPortIdentifier.open(CommPortIdentifier.java:357)
      at com.storecheck.detailconnect.utils.SerialDriver.open(SerialDriver.java:104)
      at com.storecheck.detailconnect.utils.TicketPrinter.print(TicketPrinter.java:281)
      at com.storecheck.detailconnect.utils.PrintNetTicket.run(PrintServer.java:181)

Found 1 port
and its Parallel

Couldnt open the printer port!
Printing finished with result : false
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
You have a process called SimpleWrite running somewhere which has obtained a lock on the same port - stop that application
0
 

Author Comment

by:memozebadua
Comment Utility
Its my own application, when it opens the port it calls this:

serialPort = (SerialPort) portId.open("SimpleWrite", 2000);



Heres the open Method code:



public synchronized boolean open(){
    boolean abierto=true;
    logger.info("Open Method");
    while (portList.hasMoreElements()) {
        logger.info("Found 1 port");
        portId = (CommPortIdentifier) portList.nextElement();

        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            logger.info("and its Serial");
            if (portId.getName().equals(defaultPort)) {

                logger.info("This is the preferred port ");
                portFound = true;

                try {
                    logger.info("Trying to open port with Javax...");
                    double number = (Math.random() + 1) *  (Math.random() * 10000);
                    int numero = (int) number;
                    serialPort = (SerialPort) portId.open("SimpleWrite", 2000);

                    logger.info("Opened the port Successfully with Javax!!");
                } catch (Exception e) {

                    logger.error("Couldnt open port with javax", e);
                    JOptionPane.showMessageDialog(null,"Couldnt open the printer port!!","E00156",2);
                    abierto=false;
                    continue;
                }
                logger.info("Obtaining OutputStream...");
                try {
                    outputStream = serialPort.getOutputStream();
                    logger.info("OutputStream OK");
                } catch (IOException e) {
                    logger.error("Error getting OutputStream port",e);
                    abierto=false;
                }

                //try {

                     int impresora = Integer.parseInt(SmartClientSettings.getInstance().getSelectedPrinter());
                     logger.info("Setting Serial parameters");
                     if (impresora == 0){ //Mobile Pro Basic (Roja)
                        //serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                     }else if(impresora == 1){ //Posiflex PP7000-II
                        //serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                     }
                    logger.info("Done setting SerialPort Params");
                //} catch (UnsupportedCommOperationException e) {
                 //   logger.error("Error setting port parameters", e);
                  //  abierto=false;
                //}


                try {
                    serialPort.notifyOnOutputEmpty(true);
                } catch (Exception e) {
                    logger.error("Error on notifyOnOutputEmpty",e);
                    abierto=false;
                }

            }
        }else{   //LPT1
            logger.info("and its Parallel");
            if (portId.getName().equals(defaultPort)) {

                logger.info("This is the preferred port");
                portFound = true;

                try {
                    logger.info("Trying to open port with Javax...");
                    parallelPort = (ParallelPort) portId.open("SimpleWrite", 2000);
                    logger.info("Opened the port Successfully with Javax!!");

                }catch (javax.comm.PortInUseException ex1){
                    logger.error("Used Port Exception: " + portId.isCurrentlyOwned(), ex1 );

                } catch (Exception e) {

                    logger.error("Couldnt open port with javax", e);
                    abierto=false;
                    continue;
                }
                logger.info("Obteniendo el OutputStream...");
                try {
                    outputStream = parallelPort.getOutputStream();
                     logger.info("OutputStream OK");
                } catch (IOException e) {
                    logger.error("Error getting OutputStream port", e);
                    abierto=false;
                }


                try {
                    if (parallelPort.isPaperOut()){
                        Toolkit.getDefaultToolkit().beep();
                    }
                } catch (Exception e) {
                    logger.error("Error on notifyOnOutputEmpty",e);
                    abierto=false;
                }

            }


        }
    }

    if (!portFound) {
        abierto=false;
    }

    return abierto;
}
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
I know its your application - the name looked like that. But it looks like the two applications are fighting for the port
0
 

Author Comment

by:memozebadua
Comment Utility
Exactly, but they shouldnt! Thats where this whole thing begins to confuse me...

The code explicitly closes the port, in order to let the other test (the same app, different time) run ok:



 public static synchronized boolean print(String puerto, String datos[]) throws java.lang.NullPointerException {
        logger.info("Starting print(port,data) function @ TicketPrinter.class");
        SerialDriver serial =  new SerialDriver(puerto);
        logger.info("Lets try to open the selected port...");
        if(!serial.open()){
            logger.info("Couldnt open the printer port!!");
            return false;
        }
       
        boolean respuesta = true;
       
        int impresora = Integer.parseInt(SmartClientSettings.getInstance().getSelectedPrinter());
                         
        if (impresora == 0){ //Mobile Pro Basic (Roja)
           
            for(int i=0; i < datos.length; i++){
               
                if(!serial.write(convierte(datos[i]))){
                    respuesta = false;
                    logger.info("Couldnt convert data to bytes in order to send them to the printer");
                }
               
            }
            logger.info("Closing port...");
            serial.close();                                                  <---  HEREEEE  :S
            logger.info("Port apparently closed");
           
        } else if ( impresora == 1 || impresora == 2 || impresora == 3 ) {
           
            for(int i=0; i < datos.length; i++){
               
                if(!serial.write(datos[i].getBytes())){
                    logger.info("Couldnt convert data to bytes in order to send them to the printer (POSIFLEX COM1)");
                    respuesta = false;
                }
               
            }
            logger.info("Closing port...");
            serial.close();
            logger.info("Port apparently closed");
           
        }

       

        return respuesta;
    }
0
 

Author Comment

by:memozebadua
Comment Utility
and just in case, here is the closing code:



    public synchronized void close(){
         try {
            logger.info("Attempting to close Output stream");
            outputStream.close();
            logger.info("Output stream closed succesfully");
           
        } catch (Exception e) {
            logger.error("Error closing OutputStream",e);
        }
       
        String PuertoSeleccionado = SmartClientSettings.getInstance().getSerialPort();
        String OS = System.getProperty("os.name").toString();
        logger.info("OS: " + OS + " on " + PuertoSeleccionado);
       
        if (OS.startsWith("Windows")){
            if(PuertoSeleccionado.startsWith("LPT")){
                parallelPort.close();
                //parallelPort.removeEventListener();
                logger.info("Parallel port closed succesfully");
            }else{
                serialPort.close();
                //serialPort.removeEventListener();
                logger.info("Serial port closed succesfully");
            }
        }else{
            if(PuertoSeleccionado.startsWith("/dev/parport")){
                parallelPort.close();
                //parallelPort.removeEventListener();
                logger.info("Parallel port closed succesfully");
            }else{
                serialPort.close();
                //serialPort.removeEventListener();
                logger.info("Serial port closed succesfully");
            }    
        }
    }
0
 
LVL 30

Accepted Solution

by:
mayankeagle earned 500 total points
Comment Utility
>> The code explicitly closes the port,

That should be done in a finally block. also make sure that line of code executes.
0
 

Author Comment

by:memozebadua
Comment Utility
Wow!!

You did it!

It was the finally block!  I just put the try around the print code and the close() in the finally and it works!

Thank you, Thank you, Thank you. For real. (Many nights i been wondering why this thing didnt worked...)

Youre the best mayankeagle  :)

Happy Hugs,

NeMo

See ya!
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> Youre the best mayankeagle  :)

He he.... thanks, but there are many here who're much better :-P
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now