在java中通过UDP发送到客户端后序列化对象具有空值
这个问题让我陷入困境。这是我目前正在开发的一个非常简单的在线多人游戏。
我目前能够通过 udp 向我的客户端发送数据包,并且他们似乎可以很好地接收它们。但是,当我将序列化对象发送到客户端并在另一端反序列化时,当我尝试访问所需的值时,我会收到 NullPointerExceptions。我已经验证该对象在服务器端被正确序列化(反序列化并检查数据),所以我 99% 确定我在发送数据包的代码中做了一些非常错误的事情。
以下是从服务器序列化和发送“Datagram”对象的代码:
DatagramPacket sendPacket = null;
byte[] buf = null;
//Serialize the datagram object to send as a UDP packet
try {
// Serialize to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(data);
buf = bos.toByteArray();
out.close();
bos.close();
} catch (IOException e) {
}
try {
sendPacket = new DatagramPacket( buf, buf.length,
InetAddress.getLocalHost(), 4004);
} catch (UnknownHostException e){}
try {
DatagramSocket sendSocket = new DatagramSocket();
sendSocket.send( sendPacket );
changed = true;
}catch (IOException e) {}
正在序列化的“data”对象充满了正确的值;我确信这一点。
另一个相关的代码块是客户端的接收块:
public Datagram readDatagram() {
byte[] buff = new byte[20000];
DatagramPacket packet = new DatagramPacket(buff, buff.length);
DatagramSocket receiver = null;
try {
receiver = new DatagramSocket(4004);
receiver.receive(packet);
} catch (IOException e) {
System.out.println("ERROR2");
}
Datagram data = null;// = new Datagram();
try {
// Deserialize from a byte array
ByteArrayInputStream bis = new ByteArrayInputStream(buff);
ObjectInput in = new ObjectInputStream(bis);
data = (Datagram) in.readObject();
bis.close();
in.close();
} catch (ClassNotFoundException e) {
} catch (IOException e) {
System.out.println("ERROR3");
}
for (int i = 0; i < 35; i++) {
System.out.print(data.getLevel()[i]);
}
receiver.close();
return data;
}
当我尝试在反序列化之后读取任何值时,我得到 NullPointerException。如果有人能指出我正确的方向,我会非常高兴。
哦,我现在故意发送到 localHost 只是为了测试一下。我的客户端和服务器都在我的机器上运行。
This problem is driving me up the wall. This is for a very simple online multiplayer game that I am currently working on.
I am currently able to send packets via udp to my client(s), and they seem to receive them fine. However, when I send a serialized object to my client and deserialize at the other end, I'm getting NullPointerExceptions when I try to access the values I need. I have verified that the object is being correctly serialized on the server side (deserialized it and checked the data), so I am 99% sure I am doing something very wrong with my code for sending the packet.
Here is the code for serializing and sending the "Datagram" object from the server:
DatagramPacket sendPacket = null;
byte[] buf = null;
//Serialize the datagram object to send as a UDP packet
try {
// Serialize to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(data);
buf = bos.toByteArray();
out.close();
bos.close();
} catch (IOException e) {
}
try {
sendPacket = new DatagramPacket( buf, buf.length,
InetAddress.getLocalHost(), 4004);
} catch (UnknownHostException e){}
try {
DatagramSocket sendSocket = new DatagramSocket();
sendSocket.send( sendPacket );
changed = true;
}catch (IOException e) {}
The "data" object being serialized is full of correct values; I am sure of this.
The other relevant code block is the receiving block on the client side:
public Datagram readDatagram() {
byte[] buff = new byte[20000];
DatagramPacket packet = new DatagramPacket(buff, buff.length);
DatagramSocket receiver = null;
try {
receiver = new DatagramSocket(4004);
receiver.receive(packet);
} catch (IOException e) {
System.out.println("ERROR2");
}
Datagram data = null;// = new Datagram();
try {
// Deserialize from a byte array
ByteArrayInputStream bis = new ByteArrayInputStream(buff);
ObjectInput in = new ObjectInputStream(bis);
data = (Datagram) in.readObject();
bis.close();
in.close();
} catch (ClassNotFoundException e) {
} catch (IOException e) {
System.out.println("ERROR3");
}
for (int i = 0; i < 35; i++) {
System.out.print(data.getLevel()[i]);
}
receiver.close();
return data;
}
When I attempt to read any values after this deserialization, I get the NullPointerException. If someone can point me in the right direction I will be extremely happy.
Oh, and I am sending to localHost right now intentionally just to test things out. My client and server are both running on my machine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在发送端和接收端,您都在捕获和压缩异常。这很可能隐藏了可以帮助您诊断问题的证据。即使情况并非如此,压制这样的异常也是危险的做法。
我敢打赌,接收端代码中会引发
ClassNotFoundException
。这会导致data == null
,然后在下面的循环中导致 NPE。On both the sending and receiving ends, you are catching and squashing exceptions. There is a good chance that this is hiding evidence that would help you diagnose the problem. Even if this is not the case, squashing exceptions like that is dangerous practice.
My bet is that a
ClassNotFoundException
is being thrown in the receiving end code. This would leave you withdata == null
, and that would then lead to an NPE in the following loop.您的代码可能存在的一个问题是您在关闭
ObjectOutputStream
之前调用toByteArray()
:如果序列化数据的某些部分在
close( )
,在这种情况下他们会丢失。尝试在关闭流后调用toByteArray()
。One possible problem with your code is that you call
toByteArray()
before closing theObjectOutputStream
:If some parts of serialized data are not written into output stream until
close()
, they would be lost in this case. Try to calltoByteArray()
after closing the streams.