• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading XML data on socket Stream

 
Dan Corrin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having issues with a SAXreader finding the end of a document in a socket stream.

I have established a socket stream between my client and the server using: (lots of error handling removed)

Server:
ServerSocket serverSocket = new ServerSocket(2222);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
Client:
Socket listenSocket = new Socket("mycomputer", 2222);;
PrintWriter out = new PrintWriter(listenSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(listenSocket.getInputStream()));

I can talk back and forth quite happily, however at some point I want to send some XML data across the stream...

class printXML(PrintWriter out, Document doc) {
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
xout.output(doc, out);

which works fine, and the client reads the XML:
Document readXML (Reader read) {
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(read);

This hangs waiting for more input, presumably the SAXBuilderEngine doesn't know when the XML is finished.
If I close the stream, the reader reads the EOF and constructs the document perfectly, however I have lost the communications channel.
If I try to send a EOF character (ascii 4) the reader complains about whitespace after the end of the docuement.

Is there any way I can tell the SAXbuilder that the document is done? Or failing that close and re-open just the server output side of the stream?

 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it looks like you have it figured out. The explanation is that the SAXBuilder is looking for the end of the stream to signal that the document is finished. And in the socket world, "end of the stream" occurs only when the connection is closed.

 
Dan Corrin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Yes, it looks like you have it figured out. The explanation is that the SAXBuilder is looking for the end of the stream to signal that the document is finished. And in the socket world, "end of the stream" occurs only when the connection is closed.



I was hoping that I had missed something in the SAXbuilder. I believe that I will hand the file transfer off to a separate socket and then use the two like a control/data pair.
 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's not a bad idea. I was trying to think of a way where you could write a special kind of InputStream which would signal end-of-stream when you sent a special character, like EOT as you already tried, but then you would probably wrap it in some kind of buffered stream and lose some of the subsequent data in discarded buffers. Too hard to make that work reliably. So never mind about that, your plan sounds pretty good to me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!