我有多个客户端和一个服务器。服务器在一个线程中处理每个客户。客户必须向客户发送客户对象。我检查了this和this,其中谈到了错误java.io.StreamCorruptedException: invalid type code: AC
,这正是我所拥有的。重复写入ObjectInputStream
但我不明白提出的解决方案。他继承了ObjectOutputStream,并且简单的不会在第二次和后面的时间内写入头部对象必须被发送。这不适合我。
是否有另一种解决方案通过TCP套接字发送自定义对象?我的客户每隔10秒收集一次数据并重新创建发送的对象。
对不起,如果我重复,我正在阅读很多类似的问题,但没有找到我的方案的答案。
预先感谢
EDIT
发送方法(在客户端)
public void TCPEchoClientSend(MonitoredData _mData) throws IOException {
if (_mData == null)
throw new IllegalArgumentException("Parameter: <Monitored Data> empty.");
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
// Send the encoded object to the server
oos.writeObject(_mData);
oos.close();
System.out.println("Client sent the monitored data package.");
}
接收
public static void handleEchoClient(Socket client, Logger logger) {
try {
MonitoredData mdata;
// Get the input and output I/O streams from socket
ObjectInputStream ois = new ObjectInputStream(client
.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(client
.getOutputStream());
// Receive until client closes connection, indicated by -1;
while ((mdata = (MonitoredData) ois.readObject()) != null) {
System.out.println("Got received data. Ready to save.");
hdb.saveOrUpdate(mdata);
System.out.println("Monitored Data arrived at home.");
}
// logger.info("Client " + _clntSock.getRemoteSocketAddress()+
// ", echoed " + totalBytesEchoed + " bytes.");
} catch (IOException ex) {
logger.log(Level.WARNING, "Exception in echo protocol", ex);
} catch (ClassNotFoundException e) {
logger.log(Level.WARNING, "Exception in echo protocol", e);
} finally {
try {
client.close();
} catch (IOException e) {
}
}
}
所以我应该在我的发送/接收方法之外实例化它们?比如在构造函数中,例如? – 2012-07-18 23:31:50
那么,显然它工作:)预先感谢,我仍然不得不继续学习这些流 – 2012-07-18 23:43:44
@PedroDusso做得好。实际上,为了缓冲的原因以及经济上的努力,您应始终在套接字的整个生命周期中使用相同的流/读者/作者,而不管它们是什么类型。 – EJP 2012-07-18 23:53:25