线程1正在截屏并设置图像名称。 线程2正在向服务器发送屏幕截图和名称(imageInfo对象)。 它没有将ImaheInfo对象发送到服务器。
客户端
abstract class ScreenCapture extends Thread implements Serializable {
static BufferedImage screencapture;
static ImageData object1;
public static void main(String args[]) throws
AWTException, IOException, InterruptedException {
// Open your connection to a server, at port 1234
final Socket ClientSocket = new Socket("localhost",1234);
final ObjectOutputStream dos= new ObjectOutputStream(ClientSocket.getOutputStream());
//ObjectInputStream in = new ObjectInputStream(ClientSocket.getInputStream() );
try{
//First thread that is Taking screenshot
Thread TakeScreenShotthread = new Thread ()
{
public void run () {
// Capture Screen using BufferedImage Library
try {
ImageData.screencapture = new Robot().createScreenCapture(
new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
System.out.println("thread1 is running...");
object1.ImageName=System.currentTimeMillis();
t++;
} catch (HeadlessException e) {
e.printStackTrace();
} catch (AWTException e) {
e.printStackTrace();
}
}
};
//Thread 2 that is Sending Screenshot to server
Thread sendingScreenShotThread =new Thread () {
public void run () {
//Sending Screen to Server
try {
dos.writeObject(object1.screencapture );
File Rif = new File(System.currentTimeMillis() + ".jpg");
ImageIO.write(object1.screencapture, "jpg", Rif);
System.out.println("thread2 is running...");
} catch (IOException e) {
e.printStackTrace();
}
}
};
TakeScreenShotthread.start();
TakeScreenShotthread.sleep(1000);
sendingScreenShotThread.start();
sendingScreenShotThread.sleep(1000);
TakeScreenShotthread.run();
sendingScreenShotThread.run();
}finally
{
//Closing ClientS
//in.close();
ClientSocket.close();
}
}
}
服务器
static BufferedImage image;
static long name;
public static void main(String args[]) throws IOException
{
ServerSocket serversock = new ServerSocket(1234);
Socket clientSocket = null;
clientSocket = serversock.accept();
ObjectInputStream in=(ObjectInputStream) clientSocket.getInputStream();
OutputStream out = null;
ImageData object= null;
try{
boolean processing=true;
while(processing)
{
try {
object = (ImageData) in.readObject();
//Saving Object instances
name= object.ImageName;
image= object.screencapture;
out = new BufferedOutputStream(new FileOutputStream(path+ object.ImageName + ".jpg"));
ImageIO.write(image, "jpg", out);
// while ((in.read(buffer)) >= 0) {
//out.write(buffer);
System.out.println("Image file written successfully");
}
} catch (Exception e) {
}finally {
processing=false;
if (out != null) out.close();
}
}
}
finally{
out.close();
clientSocket.close();
serversock.close();
}
}
图片信息
public class ImageData {
static BufferedImage screencapture;
static long ImageName;
ImageData()
{
screencapture=null;
ImageName=(Long) null;
}
}
错误
java.net.SocketException: Connection reset by peer: socket write error
答案 0 :(得分:0)
您只是在客户端中读取一个图像然后关闭连接,因此下一个发送会获得此异常。第一个finally
块中的内容应该位于前一个catch块中。下一次阅读会抛出SocketException: socket closed
,但是因为你忽略了所有错过它的例外。
NB Thread.sleep()
是一个静态方法,用于休眠当前线程。