1
我写了一个runnable网络类,它在套接字上侦听并取消编组输入。它也可以使用编组对象写入套接字。出现问题是因为套接字保持打开状态(以便稍后允许客户端和主机之间的通信) - 这会导致输入流的解组停止。我已经尝试从发件人一方编写XMLStreamConstants.END_DOCUMENT,但这会导致错误解组而不是挂起。下面是一些对网络类代码:Unmarshalling挂在打开套接字上
@Override
public void update(Observable o, Object arg) {
try {
if(!this.updatedByNetwork){
OutputStream os = socket.getOutputStream();
mh.marshal(this.gm.getBoard(), os);
os.flush();
}
}catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
if (this.ss != null){
this.socket = this.ss.accept();
this.update(this.gm, null);
}
while (true){
try {
InputStream is = socket.getInputStream();
Board b = mh.unmarshal(is);
this.updatedByNetwork = true;
this.gm.updateBoard(b);
} catch (SocketTimeoutException e){
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
下面的代码为我的马歇尔处理程序:
public Board unmarshal(InputStream in) throws JAXBException{
Unmarshaller um = this.jc.createUnmarshaller();
Board b = (Board) um.unmarshal(in);
return b;
}
public void marshal(Board b, OutputStream os) throws JAXBException {
Marshaller m = this.jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(b, os);
}
那么,有没有一种方法来表示文件的解组结束了吗?或者,有没有更好的方法来做到这一点?