我正在尝试将从客户端捕获的图像发送到服务器,使用机器人类捕获图像并写入客户端套接字。在服务器中,我正在读取缓冲的图像并写入服务器本地存储区域。我希望客户端定期捕获屏幕截图并发送到server.server将图像和存储读取到其存储库中。
public class ServerDemo {
public static void main(String[] args) {
try {
ServerSocket serversocket=new ServerSocket(6666);
System.out.println("server listening..........");
while(true)
{
Thread ts=new Thread( new ServerThread(serversocket.accept()));
ts.start();
System.out.println("server thread started.........");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ServerThread.java
public class ServerThread implements Runnable {
Socket s;
BufferedImage img = null;
String savelocation="d:\\Screenshot\\";
public ServerThread(Socket server) {
this.s=server;
}
@Override
public void run() {
try {
System.out.println("trying to read Image");
img = ImageIO.read(s.getInputStream());
System.out.println("Image Reading successful.....");
} catch (IOException e) {
System.out.println(e);
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File save_path=new File(savelocation);
save_path.mkdirs();
try {
ImageIO.write(img, "JPG",new File(savelocation+"img-"+System.currentTimeMillis()+".jpg"));
System.out.println("Image writing successful......");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e);
e.printStackTrace();
}
}
}
ClientDemo.java
public class ClientDemo {
public static void main(String[] args) throws InterruptedException {
try {
Socket client=new Socket("localhost", 6666);
while(true)
{
System.out.println("Hello");
Thread th=new Thread(new ClientThread(client));
th.start();
System.out.println("Thread started........");
th.sleep(1000*60);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ClientThread.java
public class ClientThread implements Runnable{
Socket c;
public ClientThread(Socket client) {
this.c=client;
}
@Override
public void run() {
try {
System.out.println("client");
//while(true){
Dimension size=Toolkit.getDefaultToolkit().getScreenSize();
Robot robot=new Robot();
BufferedImage img=robot.createScreenCapture(new Rectangle(size));
System.out.println("Going to capture client screen");
ImageIO.write(img, "JPG", c.getOutputStream());
System.out.println("Image capture from client success...!");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
服务器控制台
服务器监听..........
服务器线程已启动.........
尝试阅读图片
图像阅读成功.....
图像写作成功......
客户端控制台 你好
线程开始........
客户端
要捕捉客户端屏幕
客户成功的图像捕捉......!
您好
线程开始........
客户端
要捕获客户端屏幕 你好
线程开始........
客户端
要捕捉客户端屏幕
重复这样。这段代码在失败之后第一次完美运行。每次运行它只捕获图像一次。我必须做什么改变才能定期捕获和写入图像...请帮助我
答案 0 :(得分:2)
Try this in ClientDemo.java
while(true)
{
System.out.println("Hello");
Socket client=new Socket("localhost", 6666);
Thread th=new Thread(new ClientThread(client));
th.start();
System.out.println("Thread started........");
th.sleep(1000*60);
}
And make sure that you close the client socket once the thread(ClientThread.java) is completed may be in finally block or at the end of code.
答案 1 :(得分:1)
对于服务器端,您不需要ImageIO
。只需发送和接收字节:
while ((count = in.read(buffer()) > 0)
{
out.write(buffer, 0, count);
}
答案 2 :(得分:1)
我发现问题出在服务器上。第一次接受来自客户端的连接时,
Thread ts=new Thread( new ServerThread(serversocket.accept()));
Socket client=new Socket("localhost", 6666);