读书人

为何阻塞了?解决方法

发布时间: 2012-01-18 00:23:26 作者: rapoo

为何阻塞了?
我想做一个在客户端里显示服务器端桌面的程序,
可是不知道为什么显示了一次之后,就一直阻塞了,请大家帮帮忙.
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{


Socket socket=null;
InetAddress address=InetAddress.getByName( "localhost ");
final static int PORT=36547;
BufferedImage bimage=null;
Image image=null;
Thread t=null;
BufferedInputStream bis=null;

public ShowImagePane() throws IOException{
socket=new Socket(address,PORT);
bis=new BufferedInputStream(socket.getInputStream());
t=new Thread(this, "runt ");
t.start();
}

public void run(){
try{
bimage=ImageIO.read(bis);
repaint();
System.out.println( "1 ");
Thread.sleep(50);
}catch(Exception ex){
System.out.println(ex.toString());
return;
}

}

public void paint(Graphics g){
if(bimage!=null){
g.clearRect(0,0,640,480);
g.drawImage(bimage,0,0,this);
}
}
}


[解决办法]
楼主刚好可以练习一下调试技巧。

读书人网 >J2SE开发

热点推荐