它是一个客户端服务器应用程序。客户端有两个线程,线程1接受屏幕截图,而线程2将它们发送到服务器,服务器将接收图像并将其保存在本地存储上。 步骤是: 线程1
客户端获取屏幕截图
将图像保存在本地存储及其队列中的路径(Q_image) 线程2
将路径出列,从存储中读取图像并逐个发送到服务器。
服务器接收图像,以TRUE响应客户端并将其保存在文件夹中 (异步客户端服务器通信)
我希望客户端获取无限图像,服务器接收无限图像但我得到以下异常
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at javax.imageio.stream.FileCacheImageInputStream.readUntil(Unknown Source)
at javax.imageio.stream.FileCacheImageInputStream.read(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInputData(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at Server.main(Server.java:34)
服务器
public class Server extends Thread
{
static BufferedImage RecievedImage;
static Socket clientSocket = null;
static DataOutputStream dos;
static ServerSocket serversock;
static int flag=0;
public static void main(String args[]) throws IOException, InterruptedException
{
serversock = new ServerSocket(1234);
clientSocket = serversock.accept();
dos = new DataOutputStream(clientSocket.getOutputStream());
try{
while (true) {
RecievedImage = ImageIO.read(clientSocket.getInputStream());
System.out.println("Image RECIEVED successfully");
if (RecievedImage != null) {
dos.writeBoolean(true);
File RecievedImageFile = new File(path+ System.currentTimeMillis() + ".jpg");
dos.writeBoolean(true);
System.out.println("REQUESTED FOR NEW IMAGE ");
ImageIO.write(RecievedImage, "jpg", RecievedImageFile);
System.out.println("Image file written successfully");
} else {
dos = new DataOutputStream(clientSocket.getOutputStream());
dos.writeBoolean(false);
System.out.println("SENT FALSE ");
System.out.println("image is empty ");
}
}
}
finally{
// dos.close();
//clientSocket.close();
// serversock.close();
}
}
}
Cient
abstract class ScreenCapture extends Thread {
static BufferedImage screencapture;
static DataInputStream dis;
static String img_path = null;
static Socket ClientSocket;
static Queue Q_image= new Queue();
static String path1;
static int flag=0;
public static void main(String args[]) throws
AWTException, IOException, InterruptedException {
try {
ClientSocket = new Socket("localhost",1234);
dis = new DataInputStream(ClientSocket.getInputStream());
while(true) {
//First thread that is Taking screenshot
Thread TakeScreenShotthread = new Thread() {
public void run() {
try {
screencapture = new Robot()
.createScreenCapture(new Rectangle(Toolkit
.getDefaultToolkit().getScreenSize()));
path = path+ System.currentTimeMillis() + ".jpg";
File image = new File(path);
ImageIO.write(screencapture, "jpg", image);
Q_image.enqueue(path);
System.out.println("thread1 is running...");
} catch (HeadlessException e) {
e.printStackTrace();
} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
//Thread 2 that is Sending Screenshot to server
Thread sendingScreenShotThread = new Thread() {
public void run() {
BufferedImage img;
//Sending Screen to Server
try {
if (flag == 0) {
img_path = (String) Q_image.dequeue();
img = ImageIO.read(new File(img_path));
ImageIO.write(img, "jpg",ClientSocket.getOutputStream());
System.out.println(" FLAG ZERO .. " +path+img_path);
flag++;
}
if (dis.readBoolean()) {
System.out
.println(" WAITNING FOR SERVER RESPONSE ..");
Thread.sleep(1000);
} else {
img_path = (String) Q_image.dequeue();
img = ImageIO.read(new File(img_path));
ImageIO.write(img, "jpg",
ClientSocket.getOutputStream());
System.out
.println("thread2 is running. RETURNED ..");
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
};
TakeScreenShotthread.start();
TakeScreenShotthread.sleep(200);
sendingScreenShotThread.start();
sendingScreenShotThread.sleep(200);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (flag==3) {
//Closing ClientS
dis.close();
ClientSocket.close();
}
}
}
}