日期:2014-05-20  浏览次数:20797 次

为何阻塞了?
我想做一个在客户端里显示服务器端桌面的程序,
可是不知道为什么显示了一次之后,就一直阻塞了,请大家帮帮忙.
Server端:
package   test;

import   java.io.FileOutputStream;
import   java.util.zip.GZIPOutputStream;
import   java.io.BufferedOutputStream;
import   java.io.IOException;
import   java.awt.Robot;
import   java.net.ServerSocket;
import   java.net.Socket;
import   java.awt.Toolkit;
import   java.awt.Rectangle;
import   java.awt.Dimension;
import   javax.imageio.ImageIO;
import   java.awt.image.BufferedImage;

public   class   ObjectServer{
final   static   int   PORT=36547;

public   static   void   main(String[]   args){
ServerSocket   serverSocket=null;
Socket   socket=null;

try{
serverSocket=new   ServerSocket(PORT);
        for(;;){
        try{
        socket=serverSocket.accept();
       
        System.out.println( "Connect   Success ");
       
        new   ServerThread(socket);
        }catch(IOException   ioEx){
        System.out.println(ioEx.toString());
        }
        }
}catch(IOException   ioEx){
System.out.println(ioEx.toString());
}
}
}

class   ServerThread   implements   Runnable{
Socket   socket=null;
BufferedOutputStream   bos=null;
Thread   t=null;

public   ServerThread(Socket   socket)   throws   IOException{
this.socket=socket;
bos=new   BufferedOutputStream(socket.getOutputStream());
t=new   Thread(this, "runt ");
t.start();
}

public   void   run(){
Robot   r=null;
Toolkit   tool=Toolkit.getDefaultToolkit();
try{
r=new   Robot();
}catch(Exception   ex){
System.out.println(ex.toString());
return;
}

Dimension   d=tool.getScreenSize();
Rectangle   rect=new   Rectangle(0,0,500,500);
BufferedImage   bImage=null;
for(;;){
bImage=r.createScreenCapture(rect);
try{
ImageIO.write(bImage, "JPEG ",bos);
//bos.flush();
Thread.sleep(50);
}catch(Exception   ex){
System.out.println(ex.toString());
}
}

}
}

Client端:
package   test;

import   java.io.FileInputStream;
import   java.io.IOException;
import   java.net.InetAddress;
import   java.net.Socket;
import   java.awt.image.BufferedImage;
import   javax.swing.JFrame;
import   javax.swing.JPanel;
import   java.awt.Graphics;
import   java.awt.Image;
import   java.io.BufferedInputStream;
import   javax.imageio.ImageIO;

public   class   ObjectClient   extends   JFrame{
public   ObjectClient()   throws   IOException{
super( "远程桌面 ");

getContentPane().add(new   ShowImagePane());

setSize(640,480);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public   static   void   main(String[]   args)   throws   IOException{
new   ObjectClient();
}
}

class   ShowImagePane   extends   JPanel   implements   Runnable{