嗨,我已经完成了一个通过套接字发送屏幕截图的程序。好吧,如果我在mac中截屏并将其发送到windows pc(7,xp不工作),它就可以工作,但是如果我想在windows中截屏并将其发送到mac,它只需发送一部分文件...这是客户端
public class Enviar {
private static int port = 3460; /* port to connect to */
private static String host = "192.168.1.135"; /* host to connect to */
private BufferedReader in = null;
private static PrintWriter outr = null;
private static Socket server;
private static OutputStream outFile;
private static InputStream inFile;
public static void main (String[] args) throws IOException, InterruptedException {
Socket server = null;
InetAddress ina = InetAddress.getByName(host);
while (true) {
try {
server = new Socket(ina, port);
if (server != null) {
break;
}
} catch (IOException e) {
//System.exit(1);
Thread.sleep(1000);
}
}
PrintWriter out = new PrintWriter(server.getOutputStream(), true);
outFile = server.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
inFile = server.getInputStream();
outr = new PrintWriter(server.getOutputStream(), true);
outr.flush();
String msg;
while (true){
try {
while ((msg = in.readLine()) != null) {
System.out.println(msg + "2");
if (msg.equals("screen")) {
outr.println("screenon");
outr.flush();
try
{
server();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
} catch (IOException e) {
System.exit(0);
}
}
}
static void server() throws Exception {
captureScreen("screencapture.png");
InputStream inFile = new FileInputStream("screencapture.png");
copy(inFile, outFile);
inFile.close();
System.out.println("Mandado");
}
static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
static public void captureScreen(String fileName) throws Exception {
String OSz = System.getProperty("os.name");
if (OSz.equals("Mac OS X")) {
System.setProperty("apple.awt.UIElement", "True");
}
Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "png", new File(fileName));
}
}
和服务器
public class Recibir {
private static Socket client;
private static BufferedReader in ;
private static PrintWriter out;
private static OutputStream outFile;
private static InputStream inFile;
static int port = 3460;
static ServerSocket server;
public static void main(String args[]) throws InterruptedException, IOException {
try {
server = new ServerSocket(port);
server.setSoTimeout(1000);
} catch (IOException e) {
System.out.println("Could not listen on port:" + port);
}
System.out.println("bitch2");
while (true){
try {
client = server.accept();
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
inFile = client.getInputStream();
out = new PrintWriter(client.getOutputStream(), true);
String msg;
out.println("screen");
if ((msg = in.readLine()) != null) {
if (msg.equals("screenon")){
client();
}
}
} catch (IOException e) {
}
}
}
static void client() throws IOException {
OutputStream outFile = new FileOutputStream("copied.png",false);
copyScreen(inFile, outFile);
outFile.close();
//inFile.close();
System.out.println("Guardado");
}
static void copyScreen(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len = 0;
int bytesRead = 0, counter = 0;
while (bytesRead >= 0) {
bytesRead = in.read(buf);
if (bytesRead >= 0) {
out.write(buf, 0, bytesRead);
counter += bytesRead;
System.out.println("total bytes read: " +
counter);
}
if (bytesRead < 1024) {
out.flush();
break;
}
}
}
}