【发布时间】:2014-04-28 17:33:25
【问题描述】:
我编写了一个可运行的网络类,它在套接字上侦听并解组输入。它还可以使用编组对象写入套接字。由于套接字保持打开状态(以便以后在客户端和主机之间进行通信)而出现问题 - 这会导致输入流的解组挂起。我已经尝试从发送方编写 XMLStreamConstants.END_DOCUMENT 但这会导致错误解组而不是挂起。下面是网络类的一些代码:
@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();
}
}
这是我的 marshall 处理程序的代码:
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);
}
那么,有没有办法为解组器表示文件结束?或者,有没有更好的方法来做到这一点?
【问题讨论】:
标签: java multithreading sockets marshalling unmarshalling