前言
写了个小工具,把远程计算机的屏幕发送给服务端.
一、客户端
public class Client5{
static String path="d:\\screen\\123.png";
static String ip="";
public static void main(String[] args) throws IOException {
File file = new File("d:\\screen\\iplist.txt");
// File file = new File(".\\iplist.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
ip = br.readLine();
int i=0;
while(true) {
try {
System.out.println(i++);
new NetClient().getdesktop(path);
new NetClient().startClient(ip,path);
//每3秒连接一次服务端发送图片
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class NetClient {
public void getdesktop(String path) throws AWTException, Exception {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
//拷贝屏幕到一个BufferedImage对象screenshot
BufferedImage screenshot = (new Robot()).createScreenCapture(new
Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight()));
//图片保存路径
File f = new File(path);
//将screenshot对象写入图像文件
ImageIO.write(screenshot, "png", f);
}
public void startClient(String ip,String path)
{
try {
//1.连接诶服务器
Socket s = new Socket(ip,9999);
System.out.println("已连接到服务器"+ip+"9999端口,准备传送图片...");
//获取图片字节流
FileInputStream fis = new FileInputStream(path);
//获取输出流
//OutputStream out = s.getOutputStream();
BufferedOutputStream bos=new BufferedOutputStream(s.getOutputStream());
BufferedInputStream bis=new BufferedInputStream(s.getInputStream());
byte[] buf = new byte[1024];
int len = 0;
//2.往输出流里面投放数据
while ((len = fis.read(buf)) != -1)
{
bos.write(buf,0,len);
}
//通知服务端,数据发送完毕
bos.flush();
s.shutdownOutput();//
//3.获取输出流,接受服务器传送过来的消息“上传成功”
//InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = bis.read(bufIn);
System.out.println(new String(bufIn,0,num));
//关闭资源
fis.close();
bos.close();
bis.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
System.out.println("客户端结束");
System.out.println("服务端"+ip+"等待重连中...");
}
}
这里可以读取同目录的iplist.txt文件,设置服务端ip.
二、服务端
public class ThreadServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = null;
int num = 0;
while(true){
System.out.println("服务器端以及启动正在接受数据");
socket = serverSocket.accept();
System.out.println("检测到客户端");
num++;
ServerSocketThread serverSocketThread = new ServerSocketThread(socket,num);
serverSocketThread.start();
// System.out.println("客户端数量为:"+num);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ServerSocketThread extends Thread {
private Socket socket = null;
private int num=0;
public ServerSocketThread (Socket socket,int num){
this.socket = socket;
this.num=num;
}
@Override
public void run(){
try {
String path=".\\screen\\";
String name="456";
InputStream in=socket.getInputStream();
FileOutputStream fos=new FileOutputStream(path+name+".png");
byte[] buf=new byte[1024];
int len=0;
while((len=in.read(buf))!=-1) {
fos.write(buf,0,len);
}
// 关闭相对应的资源
fos.close();
in.close();
out.close();
socket.close();
System.out.println("+++接收来自"+name+"的图片+++");
System.out.println("***************这是接收的第"+num+"张图片");
System.out.println("!!!服务端结束!!!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务端用了多线程技术.连接一个客户端就会开启一个线程.这样速度会更快