javaについての質問です。
現在javaでリモートデスクトップソフトを作っています。当初はアンドロイドでPCを遠隔操作しようと考えていたのですがうまくいかなくてPC間でやろうと試行錯誤しています。
lang
1import java.awt.AWTException; 2import java.awt.Dimension; 3import java.awt.Rectangle; 4import java.awt.Robot; 5import java.awt.Toolkit; 6import java.awt.image.BufferedImage; 7import java.io.BufferedOutputStream; 8import java.io.BufferedReader; 9import java.io.IOException; 10import java.io.InputStream; 11import java.io.InputStreamReader; 12import java.io.OutputStream; 13import java.net.Socket; 14 15import javax.imageio.ImageIO; 16 17public class ScshotClient { 18public static void main(String[] args) { 19new Connect(); 20} 21} 22class Connect { 23static int PORT=54123; 24static String hostName="owner-PC"; 25public Connect() { 26OutputStream os=null; 27BufferedOutputStream bos=null; 28InputStream is=null; 29BufferedReader irStr = null; 30Socket soc=null; 31while(true){ 32try { 33soc = new Socket(hostName, PORT); 34//soc.setSoTimeout(200); 35os = soc.getOutputStream(); 36bos = new BufferedOutputStream(os); 37is= soc.getInputStream(); 38irStr = new BufferedReader( 39new InputStreamReader(is)); 40while(true){ 41ImageIO.write(Screenshot(), "png", bos); 42bos.flush(); 43if(irStr.readLine().equals("ok")); 44else break; 45Thread.sleep(500); 46} 47} catch (IOException e) { 48e.printStackTrace(); 49} catch (AWTException e) { 50e.printStackTrace(); 51} catch (InterruptedException e) { 52e.printStackTrace(); 53} 54try { 55if(irStr!=null){irStr.close();irStr=null;} 56if(bos!=null){bos.close();bos=null;} 57if(soc!=null){soc.close();soc=null;} 58} catch (IOException e) { 59e.printStackTrace(); 60} 61 62try { 63Thread.sleep(500); 64} catch (InterruptedException e) { 65e.printStackTrace(); 66} 67} 68} 69public static BufferedImage Screenshot() throws AWTException, IOException { 70Robot robot = new Robot(); 71Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 72BufferedImage image = robot.createScreenCapture( 73new Rectangle(0, 0, screenSize.width, screenSize.height)); 74return image; 75} 76}
lang
1import java.awt.Graphics; 2import java.awt.Point; 3import java.awt.event.MouseEvent; 4import java.awt.event.MouseListener; 5import java.awt.event.WindowAdapter; 6import java.awt.event.WindowEvent; 7import java.awt.image.BufferedImage; 8import java.io.BufferedInputStream; 9import java.io.IOException; 10import java.io.InputStream; 11import java.io.OutputStream; 12import java.io.PrintWriter; 13import java.net.ServerSocket; 14import java.net.Socket; 15 16import javax.imageio.ImageIO; 17import javax.swing.JFrame; 18import javax.swing.JPanel; 19 20 21public class ScshotServer { 22 23 public static void main(String[] args) { 24 try { 25 ServerSocket serverSoc = new ServerSocket(54123); 26 while(true){ 27 Socket socket=null; 28 socket = serverSoc.accept();//接続待ち 29 new Server(socket).start(); 30 } 31 }catch (IOException e) { 32 e.printStackTrace(); 33 } 34 } 35 } 36 class Server extends Thread implements MouseListener{ 37 Socket soc=null; 38 InputStream is=null; 39 BufferedInputStream bis = null; 40 OutputStream os=null; 41 PrintWriter osStr =null; 42 static int bor=0; 43 public Server(Socket sc){ 44 soc=sc; 45 bor++; 46 } 47 public void run(){ 48 JFrame frame = new JFrame(); 49 Window window = new Window(); 50 frame.add(window); 51 frame.addMouseListener(this); 52 frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 53 frame.addWindowListener(new WindowAdapter(){ 54 public void windowClosing(WindowEvent we){ 55 try { 56 if(osStr!=null){osStr.close();} 57 if(bis!=null){bis.close();} 58 if(soc!=null){soc.close();} 59 } catch (IOException e) { 60 e.printStackTrace(); 61 } 62 } 63 }); 64 frame.setBounds(bor*10, 10, 700, 500); 65 frame.setTitle("title"); 66 frame.setVisible(true); 67 try { 68 // soc.setSoTimeout(200); 69 BufferedImage img; 70 is= soc.getInputStream(); 71 bis = new BufferedInputStream(is); 72 os = soc.getOutputStream(); 73 osStr = new PrintWriter(os, true); 74 while(true){ 75 img=ImageIO.read(bis); 76 window.set_image(img); 77 osStr.println("ok"); 78 osStr.flush(); 79 } 80 } catch (IOException e) { 81 e.printStackTrace(); 82 }finally{ 83 try { 84 if(osStr!=null){osStr.close();} 85 if(bis!=null){bis.close();} 86 if(soc!=null){soc.close();} 87 } catch (IOException e) { 88 e.printStackTrace(); 89 } 90 } 91 frame.setVisible(false); 92 } 93 @Override//クリック 94 public void mouseClicked(MouseEvent e) { 95 Point point = e.getPoint(); 96 System.out.println("x:" + point.x + ",y:" + point.y); 97 } 98 @Override//領域に入ってきたとき 99 public void mouseEntered(MouseEvent arg0) { 100 } 101 @Override//領域から出て行ったとき 102 public void mouseExited(MouseEvent arg0) { 103 } 104 @Override//ボタンが押されたとき 105 public void mousePressed(MouseEvent arg0) { 106 } 107 @Override//ボタンが離されたとき 108 public void mouseReleased(MouseEvent arg0) { 109 } 110 } 111 class Window extends JPanel { 112 BufferedImage bimg=null; 113 public void set_image(BufferedImage img){ 114 bimg=img; 115 repaint(); 116 } 117 public void paintComponent(Graphics g){ 118 g.drawImage(bimg, 0,0,getWidth(),getHeight(),this); 119 // TODO 自動生成されたメソッド・スタブ 120 121 } 122 123}
インデントバラバラで見辛いかもしれませんがすいません。
クライアントとサーバーでソケットを使いTCP通信をしています。サーバー側にクライアントの画面をキャプチャーした画像を送っています。サーバー側でクライアントのPCを遠隔操作する形になります。(普通は逆だと思うけど)
これからが本題でまずサーバーを起動し、待ち状態にします。次にクライアントを起動したとき通信先のアカウント名とポート番号を指定するためのウインドウを出したいと考えています。ウインドウにはテキストボックス2つと接続を開始しウインドウを閉じるボタンを一つ作ります。自分でコードを書いてみたのですがウインドウは出てこずエラーを吐き続ける状態になりました。
lang
1import java.awt.AWTException; 2import java.awt.Dimension; 3import java.awt.EventQueue; 4import java.awt.Rectangle; 5import java.awt.Robot; 6import java.awt.Toolkit; 7 8import javax.imageio.ImageIO; 9import javax.swing.JFrame; 10import javax.swing.JPanel; 11import javax.swing.border.EmptyBorder; 12import javax.swing.JTextField; 13import javax.swing.JButton; 14import javax.swing.AbstractAction; 15 16import java.awt.event.ActionEvent; 17import java.awt.image.BufferedImage; 18import java.io.BufferedOutputStream; 19import java.io.BufferedReader; 20import java.io.IOException; 21import java.io.InputStream; 22import java.io.InputStreamReader; 23import java.io.OutputStream; 24import java.net.Socket; 25 26import javax.swing.Action; 27 28 29public class ScshotClient extends JFrame { 30 31 private JPanel contentPane; 32 public final JTextField textField = new JTextField(5); 33 public final JTextField textField_1 = new JTextField(20); 34 public final JButton btnNewButton = new JButton("New button"); 35 public final Action action = new SwingAction(); 36 static int port; 37 static String name; 38 39 /** 40 * Launch the application. 41 */ 42 public static void main(String[] args) { 43 EventQueue.invokeLater(new Runnable() { 44 public void run() { 45 try { 46 ScshotClient frame = new ScshotClient(); 47 frame.setVisible(true); 48 } catch (Exception e) { 49 e.printStackTrace(); 50 } 51 } 52 }); 53 new Connect(); 54 } 55 56 /** 57 * Create the frame. 58 */ 59 public ScshotClient() { 60 textField_1.setBounds(217, 57, 155, 19); 61 textField_1.setColumns(10); 62 textField.setBounds(63, 57, 96, 19); 63 textField.setColumns(10); 64 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 65 setBounds(100, 100, 450, 300); 66 contentPane = new JPanel(); 67 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 68 setContentPane(contentPane); 69 contentPane.setLayout(null); 70 71 contentPane.add(textField); 72 73 contentPane.add(textField_1); 74 btnNewButton.setAction(action); 75 btnNewButton.setBounds(143, 158, 91, 21); 76 77 contentPane.add(btnNewButton); 78 } 79 80 private class SwingAction extends AbstractAction { 81 public SwingAction() { 82 putValue(NAME, "接続"); 83 putValue(SHORT_DESCRIPTION, "Some short description"); 84 port = Integer.parseInt(textField.getText()); 85 name = textField_1.getText(); 86 setDefaultCloseOperation(EXIT_ON_CLOSE); 87 } 88 public void actionPerformed(ActionEvent e) { 89 } 90 } 91 static class Connect {// TODO 自動生成されたコンストラクター・スタブ 92 public Connect() { 93 OutputStream os=null; 94 BufferedOutputStream bos=null; 95 InputStream is=null; 96 BufferedReader irStr = null; 97 Socket soc=null; 98 while(true){ 99 try { 100 soc = new Socket(name, port); 101 //soc.setSoTimeout(200); 102 os = soc.getOutputStream(); 103 bos = new BufferedOutputStream(os); 104 is= soc.getInputStream(); 105 irStr = new BufferedReader(new InputStreamReader(is)); 106 while(true){ 107 ImageIO.write(Screenshot(), "png", bos); 108 bos.flush(); 109 if(irStr.readLine().equals("ok")); 110 else break; 111 Thread.sleep(500);//500ミリ秒停止 112 } 113 } catch (IOException e) { 114 e.printStackTrace(); 115 } catch (AWTException e) { 116 e.printStackTrace(); 117 } catch (InterruptedException e) { 118 e.printStackTrace(); 119 } 120 try { 121 if(irStr!=null){irStr.close();irStr=null;} 122 if(bos!=null){bos.close();bos=null;} 123 if(soc!=null){soc.close();soc=null;} 124 } catch (IOException e) { 125 e.printStackTrace(); 126 } 127 128 try { 129 Thread.sleep(500); 130 } catch (InterruptedException e) { 131 e.printStackTrace(); 132 } 133 } 134 } 135 public BufferedImage Screenshot() throws AWTException, IOException { 136 Robot robot = new Robot(); 137 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 138 BufferedImage image = robot.createScreenCapture( 139 new Rectangle(0, 0, screenSize.width, screenSize.height)); 140 return image; 141// TODO 自動生成されたメソッド・スタブ 142 143 } 144} 145}
というふうに書いてみましたがコード上ではエラーが無くて困っています。
また遠隔操作するときにマウス、キーボードの動きをクライアント側でも同じようにするにはどうしたらいいのでしょうか?
教えてください。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/01/25 14:55