SV: [jdom-interest] Building from Socket

Per Norrman pernorrman at telia.com
Mon Nov 17 23:01:58 PST 2003


Hi,

The problem is that the parser never sees an EOF, it tries reading
chunks of bytes and will block when there are not enough available.
If you must have the client socket open, then your only choice is to
introduce some sort of 'protocol', albeit a very simple one will
suffice:

Writer:
	byte[] data = ... // Read XML source into byte array
	DataOutputStream out = new
DataOutputStream(socket.getOutputStream());
	out.writeInt(data.length);
	out.write(data);
	...

Reader:
	DataInputStream in = new
DataInputStream(socket.getInputStream());
	int length = in.readInt();
	byte data[] = new byte[length];
	in.readFully(data);

	Document doc = new SAXBuilder().build(new
ByteArrayInputStream(data));

Hope this helps

/pmn
	

-----Ursprungligt meddelande-----
Från: jdom-interest-admin at jdom.org [mailto:jdom-interest-admin at jdom.org]
För Sebastian Robertsson 
Skickat: den 18 november 2003 02:01
Till: jdom-interest at jdom.org
Ämne: [jdom-interest] Building from Socket


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=jd
om-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