1. 程式人生 > >JAVA實現遠端控制(JAVA in RemoteControl)

JAVA實現遠端控制(JAVA in RemoteControl)

遠端控制(RemoteControl)

遠端控制( RemoteControl)擁有控制端和被控端雙方。

控制方通過請求,取得對遠端的操作,實現遠端的事件回放功能,同時,應該看得到遠端桌面的畫面。

而被控方必須在建立ServerSocket之後進行對外來請求的鑑聽。如下圖,

利用JAVA.AWT.ROBOT類,實現在遠端的回放功能

例子:// Robot 使用示例

import java.awt.Toolkit;

import java.awt.event.InputEvent;

import java.awt.event.KeyEvent;

public

class RobotTest {

public static void main(String[]args) throws Exception {

// 建立一個機器人物件

java.awt.Robot robot = new java.awt.Robot ();

// 當前螢幕大小

Toolkit tk = java.awt.Toolkit.getDefaultToolkit ();

java.awt.Dimension dm = tk.getScreenSize ();

// 計算螢幕中心點

int x = ( int ) dm.getWidth() / 2;

int y = ( int ) dm.getHeight() / 2;

// 將滑鼠移動到螢幕中心

robot.mouseMove(x, y);

// 按下滑鼠左鍵

robot.mousePress(InputEvent.BUTTON1_MASK );

// 鬆開滑鼠左鍵

robot.mouseRelease(InputEvent.BUTTON1_MASK );

// 模擬按下回車鍵

robot.keyPress(KeyEvent.VK_ENTER );

/ / 模擬放鬆回車鍵

robot.keyRelease(KeyEvent.VK_ENTER );

//

按下 SHIFT

robot.keyPress(KeyEvent.VK_SHIFT );

for ( int i = 0; i < 10; i++) {

// 在螢幕上打字

robot.keyPress('A' + i);

robot.keyRelease('A' + i);

Thread.sleep (500);

}

// 鬆開 SHIFT

robot.keyRelease(KeyEvent.VK_SHIFT );

for ( int i = 0; i < 11; i++) {

// 刪除操作

robot.keyPress(KeyEvent.VK_BACK_SPACE );

robot.keyRelease(KeyEvent.VK_BACK_SPACE );

Thread.sleep (500);

}

}

}

同樣利用ROBOT裡的createScreenCapture() 方法進行畫面的捕獲。

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.RenderingHints;

import java.awt.Toolkit;

import java.awt.image.BufferedImage;

import java.io.FileOutputStream;

import javax.swing.*;

public class ScreenTest {

// 截圖測試

public static void main(String[] args) throws Exception {

//控制檯標題

JFrame jf = new JFrame( " 控制檯 " );

// 控制檯大小

jf.setSize(500, 400);

//imag_lab 用於存放畫面

JLabel imag_lab = new JLabel();

jf.add(imag_lab);

// 設定控制檯可見

jf.setVisible(true );

// 控制檯置頂

jf.setAlwaysOnTop(true );

// 控制檯退出模式

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

java.awt.Dimension d =jf.size ();

java.awt.Graphics g = jf.getGraphics();

// 當前螢幕大小

Toolkit tk = java.awt.Toolkit.getDefaultToolkit ();

java.awt.Dimension dm =tk.getScreenSize();

// 建立 Robot 物件

java.awt.Robot robot = new java.awt.Robot();

for ( int i = 0; i < 1000; i++) {

// 擷取指定大小的螢幕區域

Rectangle rec = new Rectangle(0, 0, ( int ) dm.getWidth(), ( int ) dm

.getHeight());

BufferedImage bimage = robot.createScreenCapture(rec);

// 將圖片轉為小圖片

BufferedImage littleImage =resize (bimage,jf.getWidth(),jf

.getHeight());

// 將圖片儲存到檔案中

FileOutputStream fous =new FileOutputStream( "screenImg” + i+ ”.jpeg" );

// javax.imageio.ImageIO.write(littleImage, "jpeg", fous);

fous.flush();

fous.close();

// 將小圖片顯示到介面上

imag_lab.setIcon(new javax.swing.ImageIcon(littleImage));

Thread.sleep (50);

}

}

private static BufferedImage resize(BufferedImage img, int newW, int newH) {

int w = img.getWidth();

int h = img.getHeight();

BufferedImage dimg = new BufferedImage(newW, newH, img.getType());

Graphics2D g = dimg.createGraphics();

g.setRenderingHint(RenderingHints.KEY_INTERPOLATION ,

RenderingHints.VALUE_INTERPOLATION_BILINEAR );

g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h,null );

g.dispose();

return dimg;

}

}

1 、Socket程式設計

利用JAVA Socket程式設計,使控制端與被控端之間建立資料傳輸的資料流通道。

例子:

(遠端)ServerSocketss =new ServerSocket(PORT);

Socket client = ss.accept();

(近端)Socket s=new Socket(IP,PORT);

注:Socket有對應的輸入輸出(InputStream、 OutputStream)流物件。

2 、控制端事件的封裝

將控制端所偵聽到的事件封裝成InputEvent類,用對像輸入出流,往遠端寫出去。

private void sendEventObject (java.awt.event.InputEventevent){

try {

// 將事件物件傳送到被控制端

ous .writeObject(event);

}catch (Exception ef){

ef.printStackTrace();

}

}

3 、畫面的傳輸

在遠端,每隔一段一定的時間,將從桌面捕獲的畫面圖片,用javax.io.ImagIO轉成一個位元組陣列,從而將每一個畫面的資料量大小,以及資料用資料輸出流往控制端傳送。

相關程式碼:

public void run(){

// 截圖,傳送

try {

while ( true ){

byte [] data=createImage();

// 傳送 :

//1. 先寫一個 int , 代表圖片資料長度

dous .writeInt(data. length );

//2. 寫入圖片位元組資料

dous .write(data);

dous .flush();

Thread.sleep (1000/20);

}

}catch (SocketExceptionef){

// ef.printStackTrace();

rs .destroy();

throw new MyException( " 客戶端 SOCKET 已斷開連線,無法傳送圖片資訊。。 " );

} catch (InterruptedException e) {

// TODO Auto-generatedcatch block

rs .destroy();

throw new MyException( " 客戶端已中斷連線,無法傳送圖片資訊。。 " );

} catch (IOException e) {

// TODO Auto-generatedcatch block

rs .destroy();

throw new MyException( " 客戶端輸入輸出流中斷,無法傳送圖片資訊。。 " );

} catch (Exception e) {

// TODO Auto-generatedcatch block

rs .destroy();

throw new MyException( " 客戶端已斷開連線,無法傳送圖片資訊。。 " );

}

}

// 取得一張螢幕圖片 , 轉成位元組陣列返回

private byte [] createImage() throws Exception{

java.awt.Robot robot=new java.awt.Robot();

java.awt.Toolkit tk=java.awt.Toolkit.getDefaultToolkit ();

java.awt.Dimensiondm=tk.getScreenSize();

// 設定區域的大小

Rectangle rt=new Rectangle(0,0,dm. width ,dm. height );

// 取得指定大小的一張圖片

BufferedImage image=robot.createScreenCapture(rt);

// 建立一段記憶體流

java.io.ByteArrayOutputStream temB=new ByteArrayOutputStream ();

// 將圖片資料寫入記憶體流中

javax.imageio.ImageIO.write (image,"jpeg" , temB);

// 做為位元組陣列返回

byte [] data=temB.toByteArray();

return data;

}

在控制端,實現對所有事件(Mouse、Key)的偵聽,將所有事件封裝在InputEvent類,並用對像輸出流ObjectOutputStream傳送到遠端。同時,控制端為一個執行緒,不斷地執行run()方法,將接收到的畫面資料(按遠端轉成位元組資料的逆過程)轉成圖片,並在控制端顯示。

控制端程式碼(在包com.zzw.client裡有三個類,程式碼分別如下)

package com.zzw.client;

importjava.awt.FlowLayout;

import java.awt.event.*;

import javax.swing.*;

importjava.net.InetAddress;

importjava.net.UnknownHostException;

public class Client extends JFrame {

/**

* @param args

*/

public JTextField iptf =new JTextField(10);

public JTextField porttf =new JTextField(3);

public JButton jb1 = new JButton("連線");

public JButton jb2 = new JButton("斷開");

public void launch(){

this.setTitle("遠端控制客戶端---連線視窗");

this.setLocation(300, 200);

this.setSize(300, 100);

this.setAlwaysOnTop(true);

JLayeredPane jlp =new JLayeredPane();

jlp.setLayout(new FlowLayout());

jlp.add(new JLabel("遠端IP地址:"));

iptf.setText("127.0.0.1");

porttf.setText("8888");

jlp.add(iptf);

jlp.add(new JLabel("埠:"));

jlp.add(porttf);

jb1.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e) {

try {

ClientDealObject.connect(iptf.getText(),Integer.parseInt(porttf.getText().trim()));

} catch (NumberFormatException e1) {

//埠號格式輸入出錯

JOptionPane.showMessageDialog(null,"IP地址或埠號輸入出錯,無法取得連線。。","提示",JOptionPane.ERROR_MESSAGE);

System.exit(0);

destroy();

//

} catch (MyException e1) {

// JOptionPane.showMessageDialog(null,e1.info,"提示",JOptionPane.ERROR_MESSAGE);

//

}catch(Exception ed){

// "遠端不允許被控,無法取得連線。。

JOptionPane.showMessageDialog(null,"遠端不允許被控,無法取得連線。。","提示",JOptionPane.ERROR_MESSAGE);

System.exit(0);

destroy();

}

//連線成功

}

});

jb2.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e) {

JOptionPane.showMessageDialog(null, "斷開控制端視窗","提示",JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

destroy();

}

});

jlp.add(jb1);

jlp.add(jb2);

this.setLayeredPane(jlp);

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e) {

// 關閉視窗

JOptionPane.showMessageDialog(null, "關閉連線視窗","提示",JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

});

this.setVisible(true);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

// new RemoteControlLoginFrame().getLocalIP();

new Client().launch();

}

public String getLocalIP(){

String ipstr = "";

InetAddress ip = null;

try {

ip = InetAddress.getLocalHost();

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

ipstr = ip.toString();

ipstr = ipstr.substring(ipstr.indexOf("/")+1);

System.out.println(ipstr);

return ipstr;

}

public void destroy(){

this.destroy();

}

}

///////

package com.zzw.client;

import java.awt.Dimension;

import java.awt.Graphics2D;

import java.awt.Image;

importjava.awt.RenderingHints;

import java.awt.Toolkit;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.awt.event.MouseWheelEvent;

import java.awt.event.MouseWheelListener;

import java.awt.image.BufferedImage;

importjava.io.ByteArrayInputStream;

importjava.io.DataInputStream;

importjava.io.ObjectOutputStream;

importjavax.imageio.ImageIO;

importjavax.swing.ImageIcon;

importjavax.swing.JLayeredPane;

public class ClientDealObject extends Thread{

public int x,y;

private java.io.DataInputStream dins;

private java.io.ObjectOutputStream ous;

private javax.swing.JLabel la_image=new javax.swing.JLabel();

public void showUI(){

javax.swing.JFrame frame=new javax.swing.JFrame("遠端控制");

frame.setSize(800,600);

frame.setResizable(false);

la_image.setSize(800, 600);

JLayeredPane jlp = new JLayeredPane();

jlp.add(la_image);

frame.setLayeredPane(jlp);

frame.setVisible(true);

frame.setDefaultCloseOperation(3);

frame.setAlwaysOnTop(true);

frame.addKeyListener(new KeyAdapter(){

public void keyPressed(KeyEvent e) {

sendEventObject(e);

}

@Override

public void keyReleased(KeyEvent e) {

sendEventObject(e);

}

@Override

public void keyTyped(KeyEvent e) {

}

});

frame.addMouseWheelListener(new MouseWheelListener(){

public void mouseWheelMoved(MouseWheelEvent e) {

sendEventObject(e);

}

});

frame.addMouseMotionListener(new MouseMotionListener(){

public void mouseDragged(MouseEvent e) {

sendEventObject(e);

}

public void mouseMoved(MouseEvent e) {

sendEventObject(e);

}

});

frame.addMouseListener(new MouseListener(){

public void mouseClicked(MouseEvent e) {

sendEventObject(e);

}

public void mouseEntered(MouseEvent e) {

sendEventObject(e);

}

public void mouseExited(MouseEvent e) {

sendEventObject(e);

}

public void mousePressed(MouseEvent e) {

sendEventObject(e);

}

public void mouseReleased(MouseEvent e) {

sendEventObject(e);

}

});

}

private void conn2Server(String ip,int port)

throws MyException,Exception{

java.net.Socket sc=new java.net.Socket(ip,port);

dins=newDataInputStream(sc.getInputStream());

ous=newObjectOutputStream(sc.getOutputStream());

if(dins==null||ous==null)throw newMyException("遠端控制不接受或未接受被控制。。。");

}

//傳送事件物件到被控制端

private void sendEventObject(java.awt.event.InputEvent event){

try{

ous.writeObject(event);

}catch(Exception ef){

ef.printStackTrace();

}

}

public void run(){

try{

while(true){

int len=dins.readInt();

byte[] data=new byte[len];

dins.readFully(data);

ByteArrayInputStream bins=newByteArrayInputStream(data);

BufferedImage image= ImageIO.read(bins);

javax.swing.ImageIcon ic=new ImageIcon(image);

Image img = ic.getImage();

Toolkit tk = Toolkit.getDefaultToolkit() ;

Dimension d =tk.getScreenSize();

int w = d.width;

int h =d.height;

BufferedImage bi = resize(img,800,600);

la_image.setIcon(new ImageIcon(bi));

la_image.repaint();//銷掉以前畫的背景

}

}catch(Exception ef){

System.out.println("網路故障:無法讀出遠端圖片資料。。。");

ef.printStackTrace();

}

}

private static BufferedImage resize(Image img, int newW, intnewH) {

int w = img.getWidth(null);

int h = img.getHeight(null);

BufferedImage dimg = new BufferedImage(newW, newH,BufferedImage.TYPE_INT_BGR);

Graphics2D g = dimg.createGraphics();

g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,

RenderingHints.VALUE_INTERPOLATION_BILINEAR);

g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);

g.dispose();

return dimg;

}

public static void main(String[] args)

throws Exception{

}

public static void connect(String ip,intport) throws Exception{

ClientDealObject ct=newClientDealObject();

ct.showUI();

ct.conn2Server(ip,port);

ct.start();

}

}

package com.zzw.client;

import javax.swing.JOptionPane;

public class MyException extends RuntimeException {

public String info= "";

public MyException(String info){

this.info = info;

System.out .println(info);

JOptionPane.showMessageDialog (null, this.info,"提示 ",JOptionPane.ERROR_MESSAGE );

}

}

在被控制端用兩個執行緒分別處理事件的回放與畫面資料的傳送功能。每隔1000/20 秒,遠端向控制端傳送一個畫面。同時不斷用事件處理執行緒對控制端事件的回放處理。

被控制端程式碼(在包com.zzw.server裡有四個類,程式碼分別如下)

package com.zzw.server;

importjava.awt.FlowLayout;

import java.awt.event.KeyAdapter;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

importjava.io.DataOutputStream;

importjava.io.IOException;

importjava.io.ObjectInputStream;

importjava.net.InetAddress;

importjava.net.UnknownHostException;

importjavax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

importjavax.swing.JLayeredPane;

importjavax.swing.JOptionPane;

importjavax.swing.JTextField;

import com.zzw.client.MyException;

/**

* @author Administrator

*/

public class RemoteServer extends JFrame{

//啟動伺服器

static ScreenThread ct=null;

static RobotThread rt= null;

static RemoteServer rs= null;

static java.net.Socket client= null;

static java.net.ServerSocket sc= null;

static java.io.InputStream ins = null;

static java.io.ObjectInputStream oins = null;

static java.io.OutputStream ous = null;

static java.io.DataOutputStream dous= null;

public void setupServer(int port)

throws MyException,Exception{

sc = new java.net.ServerSocket(port);

JOptionPane.showMessageDialog(null, "允許被控!!","提示",JOptionPane.INFORMATION_MESSAGE);

// while(true){

client = sc.accept();

ins =client.getInputStream();

// 物件輸入流,讀取事件物件

oins=newObjectInputStream(ins);

ous =client.getOutputStream();

//資料輸出流,用以傳送圖片資料 1個int圖片資料長度 圖片的位元組

dous = new DataOutputStream(ous);

//啟動二個處理執行緒

if(ins==null||oins==null||ous==null||dous==null)thrownew MyException("控制客戶端已關閉連線");

try{

ct = new ScreenThread(dous,this);

ct.start();

rt = newRobotThread(oins,this);

rt.start();

}catch(Exception e){

destroy();

}

// }

}

/**

* @param args

*/

public static void main(String[] args)

throws Exception{

}

public static void accept(int port)throws Exception{

rs = new RemoteServer();

rs.setupServer(port);

}

public void destroy(){

RemoteServer.deconnect();

}

public static void deconnect(){

if(ins!=null){

try {

ins.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(oins!=null){

try {

oins.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(sc!=null){

try {

sc.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

/////

package com.zzw.server;

import java.awt.Dimension;

import java.awt.Image;

import java.awt.Robot;

import java.awt.Toolkit;

import java.awt.event.InputEvent;

import java.awt.event.KeyEvent;

import java.awt.event.MouseEvent;

import java.awt.event.MouseWheelEvent;

import java.io.ObjectInputStream;

//讀取控制端發來的事件物件,並進行回放執行緒

public class RobotThread extends Thread{

publicRemoteServer rs = null;

private java.io.ObjectInputStream oins;

public RobotThread(ObjectInputStream oins,RemoteServer rs){

this.oins=oins;

this.rs = rs;

}

// public void destroy(){

// if(rs!=null)rs = null;

// if(oins!=null)oins= null;

// }

public void run(){

try{

while(true){

//讀取一個事件物件

Object event=oins.readObject();

java.awt.event.InputEvent e=(InputEvent)event;

//處理這個事件

actionEvent(e);

}

}catch(Exception ef){

rs.destroy();

// ef.printStackTrace();

}

}

//回放事件的方法

public voidactionEvent(InputEvent event)throws Exception{

Robot robot =new Robot();

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension d = tk.getScreenSize();

int sw = 800;

int sh = 600;

int w =(int)d.getWidth();

int h =(int) d.getHeight();

int x,y;

MouseEvent mevent=null;//Mouse事件

MouseWheelEventmwevent=null;//Mouse滾輪事件

KeyEventkevent=null; //按鍵事件

intmousebuttonmask=-100;

//根據事件ID判斷為何種事件

switch(event.getID()){

case MouseEvent.MOUSE_MOVED://Mouse移動事件

mevent=(MouseEvent)event;

x =w*mevent.getX()/800;

y =h*(mevent.getY()-30)/(600-30);

robot.mouseMove(x,y);

break;

caseMouseEvent.MOUSE_PRESSED : //Mouse按鍵按下事件

mevent = ( MouseEvent ) event;

mousebuttonmask = getMouseClick( mevent.getButton() );

robot.mousePress(mousebuttonmask);

break;

caseMouseEvent.MOUSE_RELEASED://Mouse按鍵放開事件

mevent=(MouseEvent)event;

mousebuttonmask =getMouseClick( mevent.getButton() );

robot.mouseRelease(mousebuttonmask);

break;

caseMouseEvent.MOUSE_WHEEL: //Mouse滾動事件

mwevent=(MouseWheelEvent)event;

robot.mouseWheel(mwevent.getWheelRotation());

break;

caseMouseEvent.MOUSE_DRAGGED: //Mouse拖動事件

mevent=(MouseEvent)event;

x =w*mevent.getX()/800;

y =h*(mevent.getY()-30)/(600-30);

robot.mouseMove(x,y);

break;

case KeyEvent.KEY_PRESSED://按鍵按下事件

kevent=(KeyEvent)event;

robot.keyPress(kevent.getKeyCode());

break;

caseKeyEvent.KEY_RELEASED://按鍵放開事件

kevent=(KeyEvent)event;

robot.keyRelease(kevent.getKeyCode());

break;

default:

System.out.println("unknowevent:"+event.getID());

break;

}

}

//根據傳送事的Mouse事件物件,轉變為通用的Mouse按鍵程式碼

private int getMouseClick(int button){

if(button==MouseEvent.BUTTON1){

returnInputEvent.BUTTON1_MASK;

}if(button==MouseEvent.BUTTON2){

return InputEvent.BUTTON2_MASK;

} if(button==MouseEvent.BUTTON3){

return InputEvent.BUTTON3_MASK;

}

return -1;

}

}

//////

package com.zzw.server;

import java.awt.Color;

import java.awt.Desktop;

import java.awt.Graphics;

import java.awt.Rectangle;

import java.awt.SplashScreen;

import java.awt.Toolkit;

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.SocketException;

import com.zzw.client.MyException;

// 傳送螢幕圖的執行緒

public class ScreenThread extends Thread {

public RemoteServer rs = null;

private java.io.DataOutputStream dous;

public ScreenThread(DataOutputStreamdous,RemoteServer rs){

this.dous=dous;

this.rs = rs;

}

public void run(){

//截圖,傳送

try{

while(true){

byte[] data=createImage();

//傳送 :

//1.先寫一個 int , 代表圖片資料長度

dous.writeInt(data.length);

//2.寫入圖片位元組資料

dous.write(data);

dous.flush();

Thread.sleep (1000/20);

}

}catch(SocketExceptionef){

// ef.printStackTrace();

rs.destroy();

throw new MyException("客戶端 SOCKET 已斷開連線,無法傳送圖片資訊。。 ");

} catch(InterruptedException e) {

// TODO Auto-generated catch block

rs.destroy();

thrownew MyException("客戶端已中斷連線,無法傳送圖片資訊。。 ");

} catch (IOException e) {

// TODO Auto-generated catch block

rs.destroy();

throw new MyException("客戶端輸入輸出流中斷,無法傳送圖片資訊。。 ");

} catch (Exception e) {

// TODO Auto-generated catch block

rs.destroy();

throw new MyException("客戶端已斷開連線,無法傳送圖片資訊。。 ");

}

}

//取得一張螢幕圖片 , 轉成位元組陣列返回

private byte[] createImage()throwsException{

java.awt.Robot robot=newjava.awt.Robot();

java.awt.Toolkittk=java.awt.Toolkit.getDefaultToolkit ();

java.awt.Dimension dm=tk.getScreenSize();

//設定區域的大小

Rectangle rt=new Rectangle(0,0,dm.width,dm.height);

//取得指定大小的一張圖片

BufferedImageimage=robot.createScreenCapture(rt);

//建立一段記憶體流

java.io.ByteArrayOutputStreamtemB=newByteArrayOutputStream();

//將圖片資料寫入記憶體流中

javax.imageio.ImageIO.write (image,"jpeg", temB);

//做為位元組陣列返回

byte[] data=temB.toByteArray();

return data;

}

}

package com.zzw.server;

importjava.awt.FlowLayout;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.net.InetAddress;

import java.net.UnknownHostException;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JLayeredPane;

importjavax.swing.JOptionPane;

importjavax.swing.JTextField;

import com.zzw.client.MyException;

public class Server extends JFrame {

JButton jb1 = newJButton("接受被控");

JButton jb2 = newJButton("斷開被控");

JTextField jtf = newJTextField("8888");

public void launch(){

this.setTitle("遠端端-IP:"+getLocalIP());

this.setLocation(300, 200);

this.setSize(300, 80);

this.setAlwaysOnTop(true);

this.setResizable(false);

JLayeredPane jlp= new JLayeredPane();

jlp.setLayout(new FlowLayout());

jlp.add(new JLabel("埠:"));

jlp.add(jtf);

jlp.add(jb1);

jb1.addMouseListener(new MouseAdapter(){

@Override

public void mouseClicked(MouseEvent e) {

try {

RemoteServer.accept(Integer.parseInt(jtf.getText().trim()));

} catch (NumberFormatException e1){

//

// e1.printStackTrace();

} catch (MyException e1) {

//

JOptionPane.showMessageDialog(null,e1.info,"提示",JOptionPane.ERROR_MESSAGE);

}catch (Exception ed) {

//

// ed.printStackTrace();

}

}

});

jlp.add(jb2);

jb2.addMouseListener(new MouseAdapter(){

@Override

public void mouseClicked(MouseEvent e) {

JOptionPane.showMessageDialog(null,"斷開被控服務端視窗","提示",JOptionPane.INFORMATION_MESSAGE);

ScreenThread st =RemoteServer.rs.ct;

RobotThread rt =RemoteServer.rs.rt;

if(st!=null)st.destroy();

if(rt!=null)rt.destroy();

RemoteServer.deconnect();

System.exit(0);

}

});

this.setLayeredPane(jlp);

this.setDefaultCloseOperation(3);

this.setVisible(true);

}

public static void main(String arg[]){

new Server().launch();

}

public String getLocalIP(){

String ipstr = "";

InetAddress ip = null;

try {

ip = InetAddress.getLocalHost();

} catch (UnknownHostException e) {

//

throw new MyException("主機號沒找到。。");

}

ipstr = ip.toString();

ipstr =ipstr.substring(ipstr.indexOf("/")+1);

System.out.println(ipstr);

return ipstr;

}

}

---------------developed it in the year of 2010 by zzw--僅供學習使用--------------------------------------------------