This discussion is archived
14 Replies Latest reply: Dec 1, 2012 3:00 AM by 977248 RSS

FileOutputStream to remote computer

804881 Newbie
Currently Being Moderated
Dear Experts,

My operating system is Ubuntu Linux. I have successfully using printer connected to /dev/lp0 by

out = new BufferedWriter(new OutputStreamWriter(Channels.newOutputStream((new FileOutputStream("/dev/lp0")).getChannel())));


Now, my problem I don't know how to make it work with printer connected to /dev/lp0 on remote computer.

I'm able to print using this command:

ssh guest@123.123.0.1 'echo test123 > /dev/lp0'

Could someone tell me how to replace "/dev/lp0* from local computer to /dev/lp0 on guest@123.123.0.1 on *FileOutputStream* line?

Thanks in advance,
Patrick
  • 1. Re: FileOutputStream to remote computer
    sabre150 Expert
    Currently Being Moderated
    Patrick wrote:
    Dear Experts,

    My operating system is Ubuntu Linux. I have successfully using printer connected to /dev/lp0 by

    out = new BufferedWriter(new OutputStreamWriter(Channels.newOutputStream((new FileOutputStream("/dev/lp0")).getChannel())));
    I'm surprised you needed to be this complicated. Why not just
    out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/dev/lp0")));
    >
    >
    Now, my problem I don't know how to make it work with printer connected to /dev/lp0 on remote computer.

    I'm able to print using this command:

    ssh guest@123.123.0.1 'echo test123 > /dev/lp0'

    Could someone tell me how to replace "/dev/lp0* from local computer to /dev/lp0 on guest@123.123.0.1 on *FileOutputStream* line?
    Then use one of the Java SSH client libraries to open an InputStream to "/dev/lp0" on guest@123.123.0.1. My SSH library preference is for JSch (Google is your friend) but there are many others.
  • 2. Re: FileOutputStream to remote computer
    gimbal2 Guru
    Currently Being Moderated
    sabre150 wrote:
    My SSH library preference is for JSch (Google is your friend) but there are many others.
    Agreed, although it is lacking documentation. The example programs and the SFTP module of the Apache commons VFS are good sources of copy/paste.
  • 3. Re: FileOutputStream to remote computer
    804881 Newbie
    Currently Being Moderated
    Because I need to catch ClosedChannelException, so that the application will not wait indefinitely when the printer is not ready.


    Thanks for the hint. I will explore it and will update the forum soon.

    Patrick
  • 4. Re: FileOutputStream to remote computer
    804881 Newbie
    Currently Being Moderated
    I'm surprised you needed to be this complicated. Why not just

    out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/dev/lp0")));


    Because I need to catch ClosedChannelException, so that the application will not wait indefinitely when the printer is not ready.


    Then use one of the Java SSH client libraries to open an InputStream to "/dev/lp0" on guest@123.123.0.1. My SSH library preference is for JSch (Google is your friend) but there are many others.


    Thanks for the hint. I will explore it and will update the forum soon.

    Patrick
  • 5. Re: FileOutputStream to remote computer
    EJP Guru
    Currently Being Moderated
    Because I need to catch ClosedChannelException, so that the application will not wait indefinitely when the printer is not ready.
    So you're basing all this on a misconception. ClosedChannelException doesn't tell you that. It only tells you that you've continued to use the Channel after you've closed it yourself. It's no different from IOException: 'socket closed' or SocketException: 'socket closed', whichever it is. It just indicates a programming error on your part, not a connection status.
  • 6. Re: FileOutputStream to remote computer
    804881 Newbie
    Currently Being Moderated
    I have tried turning off the printer and it didn't throw IOException, but it worked with ClosedChannelException. Or maybe you can show me how to catch the Exception when the printer is not ready?

    Thanks in advance,
    Patrick
  • 7. Re: FileOutputStream to remote computer
    EJP Guru
    Currently Being Moderated
    Can you post the stack trace for the ClosedChannelException?
  • 8. Re: FileOutputStream to remote computer
    804881 Newbie
    Currently Being Moderated
    I have made progress, but I'm stuck.

    The following code can produce a print out:
            try {
                sesConnect = jscSSH.getSession(strUser, strIP, intPort);
                config = new Properties();
                config.put("StrictHostKeyChecking", "yes");
                sesConnect.setConfig(config);
                sesConnect.connect(intTimeOut);
    
                sshChannel = sesConnect.openChannel("shell");
    
                String str = "echo Hello InputStream > /dev/lp0 ";
                byte[] bytes;
                bytes = str.getBytes();
                bytes[str.length()-1] = 13;
    
                sshChannel.setInputStream(new ByteArrayInputStream(bytes));
                sshChannel.setOutputStream(System.out); // for obtaining result on standard output
    
                sshChannel.connect(); // according to API, setInputStream or setOutputStream should be called before connect()
    
            } catch(JSchException jscEX) {
                try {
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            JOptionPane.showMessageDialog(ANCESTOR, "JSch Exception in connecting session!", "SSH Exception", JOptionPane.ERROR_MESSAGE);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
    But when I changed the sshChannel output stream like the following, I cannot produce a print out. No error message either.
            try {
                sesConnect = jscSSH.getSession(strUser, strIP, intPort);
                config = new Properties();
                config.put("StrictHostKeyChecking", "yes");
                sesConnect.setConfig(config);
                sesConnect.connect(intTimeOut);
    
                sshChannel = sesConnect.openChannel("shell");
    
                String str = "echo Hello InputStream ";
                byte[] bytes;
                bytes = str.getBytes();
                bytes[str.length()-1] = 13;
    
                sshChannel.setInputStream(new ByteArrayInputStream(bytes));
                sshChannel.setOutputStream(new FileOutputStream("/dev/lp0"));
    
                sshChannel.connect(); // according to API, setInputStream or setOutputStream should be called before connect()
                } catch(IOException ioEx) {
                    try {
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                JOptionPane.showMessageDialog(ANCESTOR, "IO Exception of SSH Channel!", "SSH Exception", JOptionPane.ERROR_MESSAGE);
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
    
            } catch(JSchException jscEX) {
                try {
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            JOptionPane.showMessageDialog(ANCESTOR, "JSch Exception in connecting session!", "SSH Exception", JOptionPane.ERROR_MESSAGE);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
    Can someone give me a hint? Thanks in advance.

    Edited by: Patrick on Oct 30, 2012 10:24 AM
  • 9. Re: FileOutputStream to remote computer
    gimbal2 Guru
    Currently Being Moderated
    Tip: edit your post and put the code again, this time between \
     tags. This makes it actually readable. Don't just add the code tags in your existing post, re-post the formatted code.                                                                                                                                                                                                                                                                                                                                                                                        
  • 10. Re: FileOutputStream to remote computer
    804881 Newbie
    Currently Being Moderated
    Thanks for the tip. I was looking for it but I didn't find the code in the plain text help when I created the message.
  • 11. Re: FileOutputStream to remote computer
    977248 Newbie
    Currently Being Moderated
    Make a ServerSocket on the remote computer:

    Run this code as root user.

    import java.io.*;
    import java.net.*;
    import java.nio.channels.Channels;

    public class PrintServer {

         public static void main(String[] args) throws Exception {
              ServerSocket server = new ServerSocket(6677);
              BufferedWriter printer = new BufferedWriter(new OutputStreamWriter(
                   Channels.newOutputStream((new FileOutputStream("/dev/lp0")).getChannel())));
              while(true) {
                   Socket client = server.accept();
                   if(client != null) {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
                        printer.write(reader.readLine());
                        printer.flush();
                        printer.close();
                        break;
                   }
                   
              }
         }

    }



    Pass your input from another computer through the socket.

    Please Change the the ip address of the socket to where PrintServer
    above is listening.

    import java.io.*;
    import java.net.*;

    public class PrintClient {

         public static void main(String[] args) throws Exception {
              Socket socket = new Socket("127.0.0.1", 6677);
              PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
              writer.println("Some strings... to print.");
              writer.flush();
              writer.close();
         }

    }
  • 12. Re: FileOutputStream to remote computer
    EJP Guru
    Currently Being Moderated
    The uses of Channels.newOutputStream() and getChannel() are just pointless complications which have already been discussed. Your solution ignores the requirement to use an SSH account. Not an answer.
  • 13. Re: FileOutputStream to remote computer
    977248 Newbie
    Currently Being Moderated
    Oh sorry, i'm just trying to give another simple solution without needs to understand the complicated secure shell protocol.
  • 14. Re: FileOutputStream to remote computer
    977248 Newbie
    Currently Being Moderated
    I you still want to use ssh client, please check guest permissions on 123.123.0.1.
    You may not be able to open the OutputStream of / dev/lp0 because the guest account doesn't have enough permissions.

    this is the permissions of /dev/lp0 on my computer:

    root@paxrouter:~# ls /dev/lp0 -l
    crw-rw---- 1 root lp 6, 0 Dec 1 23:02 /dev/lp0

    if you have a same result, you can't use your guest account.

    So, only root and member of group lp that have access of /dev/lp0, otherwise access is denied.

    The solution is use root account or an account member of lp group or try to use my previous post.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points