[jdom-interest] Building from Socket
Sebastian Robertsson
sebastian.robertsson at spray.se
Mon Nov 17 17:01:23 PST 2003
Hi everyone!
I know this is an old topic - the problem of building a JDOM-doc read from a socket without closing it. I've
just tried this solution:
http://www.servlets.com/archive/servlet/ReadMsg?msgId=340949&listName=jdom-interest
But, I can't make it work. It seems like the parser still hangs, and when I terminate the thread on the
sender side, I get:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at server.ConnectedClient.getRequestData(ConnectedClient.java:189)
at server.ConnectedClient.<init>(ConnectedClient.java:49)
at server.Server.run(Server.java:86)
at java.lang.Thread.run(Unknown Source)
On the "sender-side" I use a BufferedOutputStream, and I flush the stream after sendning. On the recieving
side, this is the code:
byte[] data = getRequestData(clientSocket.getInputStream());
InputStream in = new ByteArrayInputStream(data);
Document d = getXMLDocument(in);
private byte[] getRequestData(InputStream stream) throws IOException
{
byte[] data = null;
// Content length not specified.
// => Read content stream until an EOF is encountered.
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
int l;
byte[] xfer = new byte[1024];
while ((l = stream.read(xfer)) != -1)
{
baos.write(xfer, 0, l);
}
data = baos.toByteArray();
return data;
}
I get everything to work when I close the stream on the sending side, but when I'm not closing it it seems
like this code doesn't work after all. Though, as far as I can tell, the XML-doc that I'm sending is alright (it
gets parsed correcty when using the close stream-trick, and I can write it to a file and open without
problems). Can somebody help me with this, please. Any other solutions, or have I missed something in
Laurent Bihanic:s solution?
Regards,
Sebastian
____________________________________________________________
Vad står det om dig på nätet? - http://www.lycos.se/
More information about the jdom-interest
mailing list