我正在尝试用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();
}
}
首次建立连接时,服务器会发送一个包含屏幕不同“块”的2-D 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。
当我一起运行服务器和客户端时,每次客户端尝试读取“Block”实例时,在发送初始ImageIcon [] []数组后,我会收到大量错误。这些是经常抛出的错误:
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
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
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)
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)
感谢您抽出宝贵时间阅读这篇文章。任何建议将不胜感激。谢谢!
答案 0 :(得分:3)
在花了很多时间浏览我的代码之后,我确定问题与我在服务器上实现多线程的方式有关。线程将尝试同时写入输出流,这导致流被破坏。谢谢大家的意见。
答案 1 :(得分:2)
您的序列化&amp;数据传输/交换已经不同步 - 客户端不再对齐并读取服务器发送(序列化)有效对象的位置。
这可能就是为什么你得到InvalidClassException:Block;字段和StreamCorruptedException的无效描述符。
您的数据传输代码主要取决于序列化和的ImageIcon。
并非这种方法是错误的,但有许多必要的假设才能实现:
尝试剥离部分(比如ImageIcon的东西)并查看是否可以保持链接同步&amp;秩序井然。一旦确定了导致链接失去同步的原因,您就可以调查原因。
答案 2 :(得分:1)
您的Block.class文件有问题。我会重新编译,重新部署到两端,并重新测试。
EOFException
是预期的:您试图读取ObjectInputStream
的结尾,即当对等方已关闭其连接结束时。您需要单独捕获它以退出do/while (true)
。
注意:您应该在已启动线程的ObjectOutputStream
方法中创建服务器端ObjectInputStream
和run()
。目前,您正在accept()
循环中创建它们,并且两者都执行可以阻止接受线程接受其他客户端的I / O.