通过套接字从客户端发送缓冲图像到服务器
Posted
技术标签:
【中文标题】通过套接字从客户端发送缓冲图像到服务器【英文标题】:Sending buffered image over socket from client to server 【发布时间】:2013-07-23 10:24:38 【问题描述】:我正在尝试将从客户端捕获的图像发送到服务器,使用机器人类捕获图像并写入客户端套接字。在服务器中,我正在读取缓冲图像并写入服务器本地存储区域。我希望客户端定期捕获屏幕截图并发送到服务器。服务器读取图像并存储在其存储库中。
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();
服务器控制台
服务器监听…………
服务器线程已启动…………
试图读取图像
图片读取成功.....
图像写入成功......
客户端控制台 你好
线程开始......
客户
要捕获客户端屏幕
客户成功的图像捕获...!
你好
线程开始......
客户
要捕获客户端屏幕 你好
线程开始......
客户
要捕获客户端屏幕
像这样重复。此代码在失败后第一次完美运行。每次运行它只捕获一次图像。我必须进行哪些更改才能定期捕获和写入图像...请帮助我
【问题讨论】:
【参考方案1】: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.
【讨论】:
【参考方案2】:服务器端不需要ImageIO
。只需发送和接收字节:
while ((count = in.read(buffer()) > 0)
out.write(buffer, 0, count);
【讨论】:
【参考方案3】:我发现问题出在服务器上。它第一次接受来自客户端的连接时,
Thread ts=new Thread( new ServerThread(serversocket.accept()));
Socket client=new Socket("localhost", 6666);
【讨论】:
以上是关于通过套接字从客户端发送缓冲图像到服务器的主要内容,如果未能解决你的问题,请参考以下文章