Stream images through socket

Posted on 2004-11-28
Last Modified: 2008-02-01
hey all.

Okay, here's what I'm trying to achieve:

I am going to install two applications. One on the Client machine, and one on the Server machine.
I want to the user on the server machine to be able to view everything happening on the Client machine (like a video recording of it, and streamed over to the server).

I need to come up with the best method of doing this. I've come up with probably the most logical technique:

Keep printing screen on the client machine, and stream the BufferedImage's to the Server. The server then keeps updating the recieved BufferedImage's to the application (which is a JWindow, that takes up the whole screen).

Would you lot agree that this is the best/only technique of achieving this? If not, I'm open to any other suggestions.

If you agree however, that that's the best technique, then I have a couple of queries:

 1)  On the server machine, would the best method of constantly outputting the frames to the app be by using a JLabel ? (ie: JLabel jl_output = new JLabel ( [bufferedImage] );).
 2)  If I use a JLabel to store the frames, what would be the best method of updating it with the new BufferedImage? For example, I don't think that this way updates the display: jl_output = new JLabel ( [newBufferedImage] ); .. Would I need to use: repaint(); to update the display??
 3)  How would I write the BufferedImages to the Socket? Would I need to generate a byte[] array ? And write that to the Socket, then turn the Byte[] array (on the server side) into the BufferedImage ?? If so, anyone got any example code to do this???

I'm open to any and all comments/suggestions.

Thanks in advance! :-)
Question by:DrWarezz
    LVL 9

    Accepted Solution

    It sounds like this would require alot of bandwidth.
    Anyway, you should use the javax.imageio.ImageIO class. It comes standard with J2SE 1.4.2 and up.
    In this class you can write an image to Input/Output stream.
    The java.awt.Robot class will supply you with a simple screen capture method.
    Heres an example...

    Rectangle screenSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    Robot robot = new Robot();
    BufferedImage screenImage = robot.createScreenCapture(screenSize);

    ImageIO.write(screenImage, "jpeg", socket.getOutputStream());

    BufferedImage receivedImage =;

    If you re-instantiate the JLabel, specifying an ImageIcon containing the buffered image in the constructor, the component should repaint itself.

    I hope this helps.
    LVL 9

    Author Comment

    Hey OBCT,
    Thanks for that. This is an app that I will be running on my LAN, so I have a fair bit of bandwidth :-)

    Thanks alot for the code, I shall give it a go later when I'm home.

    Thanks again,
    LVL 9

    Author Comment

    I tried the following (to test the update of the image in the JLabel):


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class ScaleTest extends JFrame implements ActionListener {
        JLabel jl_testLabel = null;
        public ScaleTest () {
            super( "Scale Test" );
            setBounds( 100, 100, 200, 200 );
            setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            JPanel pane = new JPanel ();
            setContentPane ( pane );
            jl_testLabel = new JLabel( new ImageIcon( "imageName1.jpg" ) );
            JButton jb_change = new JButton ( "Change" );
            jb_change.addActionListener ( this );
            pane.add ( jb_change );
            pane.add ( jl_testLabel );
            setVisible ( true );
        public static void main ( String [] args ) {
            new ScaleTest().pack();
        public void actionPerformed ( ActionEvent evt ) {
            jl_testLabel = new JLabel( new ImageIcon( "imageName2.jpg" ) );

    It doesn't work ... it should change the image when I click on the button "Change" ... what's up with that?!? :o\ lol

    Have I done something wrong with the code? If not, do you have any other ideas? :o\

    LVL 9

    Expert Comment

    Sorry DrWarez, it turns out I was wrong.
    Reconstructing the Label doesn't repaint it!!! Seems a bit stupid to me but anyway,
    try this....

    in the actionPerformed(ActionEvent) method, replace this...
    jl_testLabel = new JLabel( new ImageIcon( "imageName2.jpg" ) );

    with this...
    jl_testLabel.setIcon(new ImageIcon("imageName2.jpg"));

    Theres no need for repainting because the setIcon method takes care of it all.


    LVL 9

    Author Comment

    Hey OBCT,
    Thanks alot for that. It works :-)

    Your help is much appreciated.
    LVL 9

    Expert Comment

    Always glad to help :-)



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    Suggested Solutions

    Title # Comments Views Activity
    tenRun challenge 28 67
    scoresAverage challenge 8 58
    sumDigits  challenge 7 40
    json example 39 78
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    737 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

    17 Experts available now in Live!

    Get 1:1 Help Now