我正在尝试使用UDP制作客户端 - 服务器程序。 我能够从服务器序列化消息,客户端也能够接收消息。但我不知道为什么,客户端无法反序列化消息。
在服务器端,我正在向其他节点发送一个List。
List<Message> messageToSend = new ArrayList<Message>();
messageToSend.add(message);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(messageToSend);
byte dbByteSend[] = bos.toByteArray();
out.close();
bos.close();
for(Entry<Integer,String> entry: test.getNeighbourList().entrySet()){
String IPaddress = entry.getValue();
Integer portIP = entry.getKey();
InetSocketAddress IPAddress = new InetSocketAddress(InetAddress.getByName(IPaddress), portIP);
DatagramPacket sendPacketFirst = new DatagramPacket(dbByteSend,dbByteSend.length, IPAddress);
Main.serverSocket.send(sendPacketFirst);
}
在客户端,我收到使用此代码的消息:
clientSocket.receive(client.receivePacket);
byte[] b = client.receivePacket.getData();
List<Message> incomingMessages = gossip.serialization.deserializeByte(b);
这里是反序列化代码:
public List<Message> deserializeByte (byte[] data){
List<Message> mapSer = null;
try {
ByteArrayInputStream filee = new ByteArrayInputStream (data);
ObjectInputStream in = new ObjectInputStream(filee);
mapSer = (List<Message>) in.readObject();
in.close();
filee.close();
} catch (Exception e) {
e.printStackTrace();
}
return mapSer;
}
这里是byte []数据值:
[-84, -19, 0, 5, 115, 114, 0, 19, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 65, 114, 114, 97, 121, 76, 105, 115, 116, 120, -127, -46, 29, -103, -57, 97, -99, 3, 0, 1, 73, 0, 4, 115, 105, 122, 101, 120, 112, 0, 0, 0, 1, 119, 4, 0, 0, 0, 1, 115, 114, 0, 14, 71, 111, 115, 115, 105, 112, 46, 77, 101, 115, 115, 97, 103, 101, -26, -73, 19, -11, -46, 85, 53, 114, 2, 0, 4, 76, 0, 9, 98, 108, 97, 99, 107, 108, 105, 115, 116, 116, 0, 16, 76, 106, 97, 118, 97, 47, 117, 116, 105, 108, 47, 76, 105, 115, 116, 59, 76, 0, 14, 109, 101, 115, 115, 97, 103, 101, 67, 111, 110, 116, 101, 110, 116, 116, 0, 19, 76, 106, 97, 118, 97, 47, 117, 116, 105, 108, 47, 84, 114, 101, 101, 77, 97, 112, 59, 76, 0, 16, 110, 101, 119, 78, 101, 105, 103, 104, 98, 111, 117, 114, 76, 105, 115, 116, 116, 0, 36, 76, 99, 111, 109, 47, 103, 111, 111, 103, 108, 101, 47, 99, 111, 109, 109, 111, 110, 47, 99, 111, 108, 108, 101, 99, 116, 47, 77, 117, 108, 116, 105, 109, 97, 112, 59, 76, 0, 4, 116, 121, 112, 101, 116, 0, 28, 76, 71, 111, 115, 115, 105, 112, 47, 77, 101, 115, 115, 97, 103, 101, 36, 109, 101, 115, 115, 97, 103, 101, 84, 121, 112, 101, 59, 120, 112, 115, 113, 0, 126, 0, 0, 0, 0, 0, 1, 119, 4, 0, 0, 0, 1, 115, 114, 0, 17, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 73, 110, 116, 101, 103, 101, 114, 18, -30, -96, -92, -9, -127, -121, 56, 2, 0, 1, 73, 0, 5, 118, 97, 108, 117, 101, 120, 114, 0, 16, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 78, 117, 109, 98, 101, 114, -122, -84, -107, 29, 11, -108, -32, -117, 2, 0, 0, 120, 112, 0, 0, 0, 9, 120, 115, 114, 0, 17, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 84, 114, 101, 101, 77, 97, 112, 12, -63, -10, 62, 45, 37, 106, -26, 3, 0, 1, 76, 0, 10, 99, 111, 109, 112, 97, 114, 97, 116, 111, 114, 116, 0, 22, 76, 106, 97, 118, 97, 47, 117, 116, 105, 108, 47, 67, 111, 109, 112, 97, 114, 97, 116, 111, 114, 59, 120, 112, 112, 119, 4, 0, 0, 0, 1, 113, 0, 126, 0, 11, 116, 0, 5, 100, 97, 116, 97, 57, 120, 115, 114, 0, 43, 99, 111, 109, 46, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 109, 111, 110, 46, 99, 111, 108, 108, 101, 99, 116, 46, 65, 114, 114, 97, 121, 76, 105, 115, 116, 77, 117, 108, 116, 105, 109, 97, 112, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 120, 114, 0, 46, 99, 111, 109, 46, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 109, 111, 110, 46, 99, 111, 108, 108, 101, 99, 116, 46, 65, 98, 115, 116, 114, 97, 99, 116, 76, 105, 115, 116, 77, 117, 108, 116, 105, 109, 97, 112, 91, 110, -123, -4, 93, 54, 46, -91, 2, 0, 0, 120, 114, 0, 50, 99, 111, 109, 46, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 109, 111, 110, 46, 99, 111, 108, 108, 101, 99, 116, 46, 65, 98, 115, 116, 114, 97, 99, 116, 77, 97, 112, 66, 97, 115, 101, 100, 77, 117, 108, 116, 105, 109, 97, 112, 33, -9, 102, -79, -11, 104, -56, 29, 2, 0, 0, 120, 112, 119, 8, 0, 0, 0, 3, 0, 0, 0, 5, 115, 113, 0, 126, 0, 9, 0, 0, 8, 57, 119, 4, 0, 0, 0, 4, 115, 114, 0, 16, 71, 111, 115, 115, 105, 112, 46, 78, 101, 105, 103, 104, 98, 111, 117, 114, 75, 81, 2, 112, -125, -57, 76, 114, 2, 0, 6, 73, 0, 4, 112, 111, 114, 116, 73, 0, 10, 116, 97, 114, 103, 101, 116, 80, 111, 114, 116, 76, 0, 13, 78, 101, 105, 103, 104, 98, 111, 117, 114, 76, 105, 115, 116, 116, 0, 19, 76, 106, 97, 118, 97, 47, 117, 116, 105, 108, 47, 72, 97, 115, 104, 77, 97, 112, 59, 76, 0, 9, 105, 80, 65, 100, 100, 114, 101, 115, 115, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 76, 0, 16, 110, 101, 119, 78, 101, 105, 103, 104, 98, 111, 117, 114, 76, 105, 115, 116, 113, 0, 126, 0, 5, 76, 0, 15, 116, 97, 114, 103, 101, 116, 73, 80, 65, 100, 100, 114, 101, 115, 115, 113, 0, 126, 0, 23, 120, 112, 0, 0, 0, 0, 0, 0, 4, 81, 112, 112, 115, 113, 0, 126, 0, 16, 119, 8, 0, 0, 0, 3, 0, 0, 0, 0, 120, 116, 0, 12, 49, 57, 50, 46, 49, 54, 56, 46, 48, 46, 49, 48, 115, 113, 0, 126, 0, 21, 0, 0, 0, 0, 0, 0, 4, 80, 112, 112, 115, 113, 0, 126, 0, 16, 119, 8, 0, 0, 0, 3, 0, 0, 0, 0, 120, 113, 0, 126, 0, 26, 115, 113, 0, 126, 0, 21, 0, 0, 0, 0, 0, 0, 31, -87, 112, 112, 115, 113, 0, 126, 0, 16, 119, 8, 0, 0, 0, 3, 0, 0, 0, 0, 120, 113, 0, 126, 0, 26, 115, 113, 0, 126, 0, 21, 0, 0, 0, 0, 0, 0, 4, 83, 112, 112, 115, 113, 0, 126, 0, 16, 119, 8, 0, 0, 0, 3, 0, 0, 0, 0, 120, 113, 0, 126, 0, 26, 115, 113, 0, 126, 0, 9, 0, 0, 4, 81, 119, 4, 0, 0, 0, 4, 115, 113, 0, 126, 0, 21, 0, 0, 0, 0, 0, 0, 8, 57, 112, 112, 115, 113, 0, 126, 0, 16, 119, 8, 0, 0, 0, 3]
运行mapSer =(List)in.readObject();它显示错误
StreamCorruptedException: unexpected EOF in middle of data block
任何人都可以帮助我并告诉我哪里出错了吗?
我尝试使用相同的代码反序列化服务器中的消息,并且它可以工作,但我不知道为什么它不能在客户端工作。
答案 0 :(得分:0)
用于构建接收DatagramPacket
的byte []数组太小。