我正在尝试用 Java 编写 RemoteDesktop 的实现。我在套接字上使用 ObjectOutputStream 和 ObjectInputStream 来发送数据。为了发送数据,我使用了我创建的一个名为“Packet”的类:
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.swing.ImageIcon;
/**
* Holds data to send over network connection
*/
class Packet<T extends Serializable> implements Serializable {
private T payload;
public Packet() {
super();
}
public Packet(T data) {
super();
setPayload(data);
}
public T getPayload() {
return payload;
}
public void setPayload(T payload) {
this.payload = payload;
}
public static void send(String message, ObjectOutputStream out) throws IOException {
out.writeObject(new Packet<>(message));
out.flush();
}
public static void send(Integer value, ObjectOutputStream out) throws IOException {
out.writeObject(new Packet<>(value));
out.flush();
}
public static void send(Block block, ObjectOutputStream out) throws IOException {
out.writeObject(new Packet<>(block));
out.flush();
}
public static void send(BufferedImage[][] images, ObjectOutputStream out) throws IOException {
//convert to ImageIcon
ImageIcon icons[][] = new ImageIcon[images.length][images[0].length];
for (int x = 0; x < images.length; x++) {
for (int y = 0; y < images[0].length; y++) {
icons[x][y] = new ImageIcon(images[x][y]);
}
}
out.writeObject(new Packet<>(icons));
out.flush();
}
}
首次建立连接时,服务器会发送一个二维 ImageIcon 数组,其中包含屏幕的不同“块”。屏幕被分成这组块。然后,服务器会定期截取屏幕截图,并将屏幕的每个“块”与最后一个进行比较,看看它是否发生了变化。如果有变化,那么服务器将在一个名为“Block”的类中发送新的屏幕区域,该类包含该块的 x 和 y 坐标以及 ImageIcon:
import java.awt.image.BufferedImage;
import java.io.Serializable;
import javax.swing.ImageIcon;
/**
* Holds an image and its x and y coordinates on the screen
*/
class Block implements Serializable {
private ImageIcon img;
private int x;
private int y;
public Block() {
super();
}
public Block(BufferedImage image, int x, int y) {
img = new ImageIcon(image);
this.x = x;
this.y = y;
}
public Block(ImageIcon image, int x, int y) {
img = image;
this.x = x;
this.y = y;
}
public int getx() {
return x;
}
public int gety() {
return y;
}
public ImageIcon getImage() {
return img;
}
}
这是服务器的主要代码:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String args[]) throws IOException {
ServerSocket s = new ServerSocket(5000);
do {
Socket c;
ObjectOutputStream out;
ObjectInputStream in;
//start listening
echo("Listening on port: 5000");
//accept connection
c = s.accept();
echo("Connected to client at address " + c.getInetAddress().getHostAddress());
//open IO streams
out = new ObjectOutputStream(c.getOutputStream());
in = new ObjectInputStream(c.getInputStream());
ServerSession rdsession = new ServerSession(in, out); //start session
} while (true);
}
private static void err(String message) {
//prints error message and exits
System.err.println(message);
System.exit(1);
}
private static void echo(String message) {
//prints message
System.out.println(message);
}
}
因此,当建立连接时,服务器会创建一个“ServerSession”类的实例。此类通过确定何时在“块”对象中发送新图像来处理 RD 会话。当它需要向客户端更新一个块时,它使用 Packet.send(Block, ObjectOutputStream) 方法。
该对象使用以下代码从流中读取对象:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.ImageIcon;
public class Client {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Socket c;
ObjectOutputStream out;
ObjectInputStream in;
String serverIP = "127.0.0.1"; //replace with server IP
int port = 5000;
//open connection and streams
c = new Socket(serverIP, port);
out = new ObjectOutputStream(c.getOutputStream());
in = new ObjectInputStream(c.getInputStream());
ClientSession cs = null;
do {
Packet<?> p;
Object o = in.readObject();
if (o instanceof Packet) {
p = (Packet<?>) o;
} else {
continue;
}
if (p.getPayload() instanceof String) { //check if string
echo("Server>" + p.getPayload());
} else if (p.getPayload() instanceof Block) { //check if block
Block b = (Block) p.getPayload();
if (cs != null) {
cs.setImage(b.getImage(), b.getx(), b.gety());
}
} else if (p.getPayload() instanceof ImageIcon[][]) { //check if 2-D image array
cs = new ClientSession(in, out, (ImageIcon[][]) p.getPayload()); //create session with image array
}
} while (true);
}
private static void err(String message) {
System.err.println(message);
System.exit(1);
}
private static void echo(String message) {
System.out.println(message);
}
}
ClientSession 类只存储屏幕图像并处理 GUI。
当我一起运行服务器和客户端时,在发送初始 ImageIcon[][] 数组后,每次客户端尝试读取“块”实例时,都会出现无数错误。这些是经常抛出的错误:
1:
Exception in thread "main" java.io.InvalidClassException: Block; invalid descriptor for field
at java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:710)
at java.io.ObjectInputStream.readClassDescriptor(ObjectInputStream.java:828)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1599)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1515)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1989)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1913)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at Client.main(Client.java:46)
Caused by: java.lang.IllegalArgumentException: illegal signature
at java.io.ObjectStreamField.<init>(ObjectStreamField.java:122)
at java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:708)
... 11 more
2:
Exception in thread "main" java.io.InvalidClassException: Block; invalid descriptor for field sq ~ sq
at java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:710)
at java.io.ObjectInputStream.readClassDescriptor(ObjectInputStream.java:828)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1599)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1515)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1989)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1913)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1989)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1913)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1989)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1913)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at Client.main(Client.java:25)
Caused by: java.lang.IllegalArgumentException: illegal signature
at java.io.ObjectStreamField.<init>(ObjectStreamField.java:122)
at java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:708)
... 19 more
3:
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readInt(DataInputStream.java:392)
at java.io.ObjectInputStream$BlockDataInputStream.readInt(ObjectInputStream.java:2818)
at java.io.ObjectInputStream.readInt(ObjectInputStream.java:969)
at javax.swing.ImageIcon.readObject(ImageIcon.java:481)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1017)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1891)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1989)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1913)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1989)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1913)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at Client.main(Client.java:25)
4:
Exception in thread "main" java.io.StreamCorruptedException: invalid type code: 73
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1989)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1913)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1989)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1913)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1796)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at Client.main(Client.java:25)
感谢您花时间阅读这篇文章。任何建议将不胜感激。谢谢!