如何通过套接字 InputStream 发送 XML 数据 [英] How to send XML data through socket InputStream
问题描述
我正在尝试使用基于 XML 的协议用 Java 编写客户端-服务器应用程序.但是我有一个很大的问题!
I'm trying to write a client-server application in Java with an XML-based protocol. But I have a great problem!
查看这部分客户端代码:
See this part of client code:
InputStream incoming = skt.getInputStream(); //I get Stream from Socket.
OutputStream out = skt.getOutputStream();
[...]
XMLSerializer serializer = new XMLSerializer();
//This create an XML document.
tosend = WUTPClientWriter.createMessage100(projectid, cpuclock, cpunumber);
serializer.setOutputByteStream(out);
serializer.serialize(tosend);
此时服务器陷入僵局.它等待 EOF 但我无法发送它,因为如果我使用
At this point server fall in deadlock. It wait for EOF but I can't send it because if I use
out.close();
或
skt.shutdownOutput();
我关闭了 Socket,我必须保持这个连接处于活动状态.
I close the Socket and I must keep this connection alive.
我无法发送\0",因为我在服务器中遇到解析错误.
I can't send '\0' becouse I get Parse Error in the server.
我该怎么做?我可以在不关闭套接字的情况下关闭"输出流吗?
How can I do it? Can I "close" the output stream without closing the socket?
已解决我使用高级流手势创建了新类 XMLStreamOutput 和 XMLStreamInput.
RESOLVED I've created new class XMLStreamOutput and XMLStreamInput with advanced Stream gesture.
推荐答案
我已经解决了这四个类:
I've resolved with this four class:
1)
public class XMLOutputStream extends ByteArrayOutputStream {
private DataOutputStream outchannel;
public XMLOutputStream(OutputStream outchannel) {
super();
this.outchannel = new DataOutputStream(outchannel);
}
public void send() throws IOException {
byte[] data = toByteArray();
outchannel.writeInt(data.length);
outchannel.write(data);
reset();
}
}
2)
public class XMLSender {
public static void send(Document tosend, OutputStream channel) throws TransformerConfigurationException, IOException {
XMLOutputStream out = new XMLOutputStream(channel);
StreamResult sr = new StreamResult(out);
DOMSource ds = new DOMSource(tosend);
Transformer tf = TransformerFactory.newInstance().newTransformer();
try {
tf.transform(ds, sr);
} catch (TransformerException ex) {
Logger.getLogger(XMLSender.class.getName()).log(Level.SEVERE, null, ex);
}
out.send();
}
}
3)
public class XMLInputStream extends ByteArrayInputStream {
private DataInputStream inchannel;
public XMLInputStream(InputStream inchannel) {
super(new byte[2]);
this.inchannel = new DataInputStream(inchannel);
}
public void recive() throws IOException {
int i = inchannel.readInt();
byte[] data = new byte[i];
inchannel.read(data, 0, i);
this.buf = data;
this.count = i;
this.mark = 0;
this.pos = 0;
}
}
4)
public class XMLReceiver {
public static Document receive(InputStream channel) throws ParserConfigurationException, TransformerConfigurationException, IOException, SAXException {
DocumentBuilderFactory docBuilderFact = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFact.newDocumentBuilder();
Document request = null;
XMLInputStream xmlin = new XMLInputStream(channel);
xmlin.recive();
request = docBuilder.parse(xmlin);
return request;
}
}
这篇关于如何通过套接字 InputStream 发送 XML 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!