//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...