Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: need help...

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default need help...

     
    //Server
    package image;
     
    import java.awt.AWTException;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
     
     
    public class Server {
        private ServerSocket Server;
        private Socket socket ;
     
        private Rectangle UkuranKotak;
        private BufferedImage image;
        private Robot robot;
        private OutputStream out;
     
        //constructor
     
        Server () {
     
           bikin_server(777);
           Cek_Ukuran_Desktop();
           Capture_Gambar();
           Kirim_Gambar();
           TutupServer();
           }
     
     
        //end of constructor
     
     
     
        public Rectangle Cek_Ukuran_Desktop(){
     
        UkuranKotak = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());    
     
        return UkuranKotak;
     
        }
     
        public void Capture_Gambar(){
            try {
     
                robot= new Robot();
                image =  robot.createScreenCapture(Cek_Ukuran_Desktop());
     
     
            } catch (AWTException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
     
             }
     
        public void Kirim_Gambar(){
            try {
                out = socket.getOutputStream();       
     
                ImageIO.write(image, "png", out);  //kirim bufferedimage ke client
     
                System.out.println("Sent image!");
     
     
            } catch (IOException ex) {
                System.out.println("eror pada saat mengirim gambar" + ex);
            }
     
     
     
        }
     
     
        public void bikin_server(int port){
            try {
                Server = new ServerSocket(port);
                socket = Server.accept();
                System.out.println("server telah dibangun di port :" +port);
     
            } catch (IOException ex) {
                System.out.println("terdapat kesalahan di bagian bikin server ,"+ex);
            }
     
     
        }
     
        void TutupServer(){
            try {
     
                if(out!=null) out.close();
                if (socket != null)    socket.close();
                System.out.println("selesai mengirim gambar, tutup koneksi");
     
     
            } catch (IOException ex) {
                System.out.println("tidak bisa menutup socket" + ex);
                        } 
     
       }
     
     
     
     
     
        public static void main(String[]args){
     
            Server jalankan = new Server();
     
     
        }
     
    }














    //client
     
    package image;
     
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.*;
     
    public class Client extends JFrame {
     
     
       private Socket socket    =null          ;
       private BufferedImage gambar =null;
       private File out= null;
       private JLabel label1;
     
     
       //constructor
     
     
       public Client(){
       BikinSocketClient("localhost",777);
       DapatkanGambarDariSocket();
       CetakGambarKeFolder();
       TutupClient();
       Bikin_Interface();
     
       }
     
       //end of constructor
     
     
     
     
      public void Bikin_Interface(){
     
       label1 = new JLabel();
       add(label1);
       label1.setIcon(new ImageIcon("c:/berhasilyaaa.png"));
     
            JScrollPane p = new JScrollPane( label1 );
            getContentPane().add( p );
            setDefaultCloseOperation( EXIT_ON_CLOSE );
            setSize( 300, 300 );
            setVisible( true );
     
     
      } 
     
     
     
      public void DapatkanGambarDariSocket(){
            try {
                gambar =ImageIO.read(socket.getInputStream());
     
            } catch (IOException ex) {
                System.out.println("gambar tidak diterima / socket eror"+ex);
            }
     
     
     
      }
     
     
      /* public void ConvertGambarKeByte(){
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImageIO.write(gambar,"png" , baos);
                baos.flush();
                byte[] UkuranImage = baos.toByteArray();
                baos.close();
     
     
            } catch (IOException ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            }
       }
     
       */  -> not implement yet
     
     
     
     
     
     
       void CetakGambarKeFolder(){
     
     
            try {
     
                out = new File("c:/berhasilyaaa.png");
     
                ImageIO.write(gambar,"png",out);
            } catch (IOException ex) {
               System.out.println("gambar tidak ditemukan" + ex);
            }
     
       }
     
     
     
     
        void BikinSocketClient(String serverName, int ServerPort){
            try {
                socket = new Socket(serverName,ServerPort);
            } catch (UnknownHostException ex) {
                System.out.println("host tidak ditemukan" + ex);
            } catch (IOException ex) {
                System.out.println ("port eror" + ex);
            }
        }
     
     
        void TutupClient(){
     
            try {
     
                if (socket != null)    socket.close();
                System.out.println("selesai menerima gambar, tutup koneksi");
     
     
            } catch (IOException ex) {
                System.out.println("tidak bisa menutup socket" + ex);
                        }
        }
     
     
       public static void main(String[]args){
     
          Client jalankan = new Client();    
     
       }
     
     
     
       }



    im trying to make client-server remote desktop application. At first im gonna capture and send desktop image from server to client.

    this already done. But the problem is im confused to send multiple image from server to client. how can this be done?

    i already trying to add a loop in the program but it doesnt work ....

    while(true) {
    Cek_Ukuran_Desktop();
    Capture_Gambar();
    Kirim_Gambar();
    //on server
    }

    while(true) {
       DapatkanGambarDariSocket();
       CetakGambarKeFolder()
     
    // on client
    }




    do i really need to use objectinputstream and objectoutputstream to send multiple image and convert the image to byte first before sending it to network?






    im sorry for my bad english...

  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: need help...

    The loops you added run indefinitely....
    To your question: Try to see the problem as a simple thing. Computer does not see the difference between texts, images, letters or numbers. I sees only a chain of bits between 0 and 1. In other words: Binary, or for the "plain" people: DATA. So the question of object or whatever input/output depends fully on your implementation. DataInputStream or DataOutputStrean is the best to handle every kind of binary streams. By the way, this code will never work:
    out = new File("c:/berhasilyaaa.png");  // Unix path convention "rapes" Window here!

    You should rewrite your loops.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help...

    out = new File("c:/berhasilyaaa.png");  // Unix path convention "rapes" Window here!
    haha.. windows raped... dunno why but yesterday i tried it works...

    it should write c:\....\.... rite..?


    hm... okay then i should rewrite my loop and give a statement to end my loops so it wont go indefinitely....

    let me try first...

    oh yeah and one more... any tips so i can update the jlabel inside that frame after continously sending image?

    or it will be automatically updated when i overwrite the "c:/berhasilyaaa.png" and show it on client's monitor
    Last edited by T_Ro; July 23rd, 2012 at 06:13 AM.

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: need help...

    ...the setIcon(Icon) will do. For Java 1 backslash must be written by 2 backslashes. Also c:\\..\\

  5. The Following User Says Thank You to Voodoo For This Useful Post:

    T_Ro (July 23rd, 2012)

  6. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help...

    package ServerClient;
     
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.*;
     
    /**
     *
     * @author admin
     */
    public class Client extends JFrame {
     
     
       private Socket socket    =null          ;
       private BufferedImage gambar =null;
       private File out= null;
       private JLabel label1;
       private DataInputStream TangkapByte;
       private  ByteArrayOutputStream baos;
       private InputStream inputstream;
       private BufferedImage gambarhasilconvert;
     
       public byte[] UkuranGambarDalamByte;
     
     
    //constructor
     
       public Client(){
       BikinSocketClient("192.168.2.5",777);
       DapatkanGambarDariSocket();
       Bikin_Interface();
       CetakGambarKeFolder();     
     
     
       }
     
     
     
     
     
       //end of constructor
     
     
     
     
      public void Bikin_Interface(){
     
           label1 = new JLabel();
       add(label1);
     
            JScrollPane p = new JScrollPane( label1 );
            getContentPane().add( p );
            setDefaultCloseOperation( EXIT_ON_CLOSE );
            setSize( 300, 300 );
            setVisible( true );
     
     
      } 
     
     
      public void UpdateImage(){
               label1.setIcon(new ImageIcon("c:/berhasilyaaa.png"));  
             }   
     
     
     
     
      public void DapatkanGambarDariSocket(){
            try {
                gambar =ImageIO.read(socket.getInputStream());
     
            } catch (IOException ex) {
                System.out.println("gambar tidak diterima / socket eror"+ex);
            }
     
     
     
      }
     
     
     
       void CetakGambarKeFolder(){
     
     
            try {
     
     
                while(gambar!=null)
                {
     
     
                out = new File("c:/berhasilyaaa.png");
                ImageIO.write(gambar,"png",out);
                UpdateImage();
     
     
                }
     
     
            } catch (IOException ex) {
               System.out.println("gambar tidak ditemukan" + ex);
            }
     
     
            TutupClient2();
     
     
       }
     
     
     
     
     
     
     
        void BikinSocketClient(String serverName, int ServerPort){
            try {
                socket = new Socket(serverName,ServerPort);
            } catch (UnknownHostException ex) {
                System.out.println("host tidak ditemukan" + ex);
            } catch (IOException ex) {
                System.out.println ("port eror" + ex);
            }
        }
     
     
     
     
        void TutupClient2(){
     
     
            try {
     
                inputstream.close();
                TangkapByte.close();
                socket.close();
     
     
     
     
     
            } catch (IOException ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            }
     
     
     
     
     
     
     
     
        }
     
     
     
     
     
     
     
     
     
     
     
       public static void main(String[]args){
     
          Client jalankan = new Client();    
     
     
     
     
     
     
       }
     
     
     
       }







     
     
     
    package image;
     
    import java.awt.AWTException;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
     
     
     
    public class Server {
        private ServerSocket Server;
        private Socket socket ;
     
        private Rectangle UkuranKotak;
        private BufferedImage image;
     
     
        private Robot robot;
     
        private OutputStream out;
       private ByteArrayOutputStream baos;
       public byte[] UkuranGambarDalamByte;
        private BufferedReader br;
        private String text;
        private String text2="stop";
     
        //constructor
     
        Server () {
     
           bikin_server(777);
     
     
         while(!InputText().equals(text2))
         {
         Capture_Gambar();
         Kirim_Gambar();
         }  
     
        TutupServer();
     
     
                    }
     
     
     
     
     
     
     
     
     
        public String InputText(){
     
           br = new BufferedReader(new InputStreamReader(System.in));
            try {
                text = br.readLine();
            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
           return text;
     
     
     
        }
     
     
        //end of constructor
     
     
     
     
        public Rectangle Cek_Ukuran_Desktop(){
     
        UkuranKotak = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());    
     
        return UkuranKotak;
     
        }
     
        public void Capture_Gambar(){
            try {
     
                robot= new Robot();
                image =  robot.createScreenCapture(Cek_Ukuran_Desktop());
     
            } catch (AWTException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
     
     
             }
     
     
     
        public void Kirim_Gambar(){
            try {
                out = socket.getOutputStream();       
     
                ImageIO.write(image, "png", out);  //sent to client
     
                System.out.println("Sent image!");
     
     
            } catch (IOException ex) {
                System.out.println("eror pada saat mengirim gambar" + ex);
            }
     
     
     
        }
     
     
        public void bikin_server(int port){
            try {
                Server = new ServerSocket(port);
                socket = Server.accept();
                System.out.println("server telah dibangun di port :" +port);
     
            } catch (IOException ex) {
                System.out.println("terdapat kesalahan di bagian bikin server ,"+ex);
            }
     
     
        }
     
        void TutupServer(){
            try {
     
                if(out!=null) out.close();
                if (socket != null)    socket.close();
                System.out.println("selesai mengirim gambar, tutup koneksi");
     
     
            } catch (IOException ex) {
                System.out.println("tidak bisa menutup socket" + ex);
                        } 
     
       }
     
     
     
     
     
     
     
     
     
     
     
        public static void main(String[]args){
     
            Server jalankan = new Server();
     
     
        }
     
    }




    hello there sir, i already rewrite my loop.....


    now it can send multiple images from server to client

    when i type anything at the server, the images sent to the client (at the server it shows Sent image! everytime i type anything)

    it loops until i type stop at the server.

    the problem is the client wont update the jlabel inside that jframe... i think i got something missing in my program....

    any idea?

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: need help...

    Quote Originally Posted by Voodoo View Post
    By the way, this code will never work:
    out = new File("c:/berhasilyaaa.png");  // Unix path convention "rapes" Window here!
    Yes, it will work.


    e problem is the client wont update the jlabel inside that jframe... i think i got something missing in my program....
    While it may not directly relate to your problem, you should read up about Swing and the EDT threading model. You are trying to update Swing components from a different thread (the main thread), which is a no-no. Construct a SwingWorker, or update using SwingUtilities.