Socket's halfs and XML read/write

A

Alex

I try to make simple Client/Server connection wrapped in XML.
So, I create XML request, open connection to the server and send it.
On the server side XML (as ImputStream) is rterieved by
DocumentBuilder.parse().
It means that to finish parse InputStream should be closed.
That's what I do from Client side. Send XML and do
socket.shutdownOutput();
Only after this DocumentBuilder (on server side) finishes its work and
returns Documnent.
But then I want to create XMl answer and send it back for Client.
But both halfs of the socket are already closed.
Don't know why.
On Client side it says:
isInputShutdown=false isOutputShutdown=true
On server side:
isInputShutdown=false isOutputShutdown=false
Server: send the answer xml
Server: java.net.SocketException: Socket closed

I expect that Input and Output halfs of the socket from both sides work
separately but it looks that when I close or OutputShutdown output it
closes both streams.
Looks strange for me.

Any ideas?
Any other way to communicate Client/Server with sending/receiving XML?

Of course I can read input, store it, wait for end of XML, create
virtual InputStream (not closing socket's one), have Document, create
Document-Answer, write it back and only then close both input/output.
But it doesn't look elegant.

So, any suggestions?

Alex.
 
A

Alex

That's what I'm talking about.
How else DocumentBuilder could know that XML is finished?
It waits for the end of the InputStream. It makes perfect sense.
Alex.
 
J

John C. Bollinger

Alex said:
I try to make simple Client/Server connection wrapped in XML.
So, I create XML request, open connection to the server and send it.
On the server side XML (as ImputStream) is rterieved by
DocumentBuilder.parse().
It means that to finish parse InputStream should be closed.
That's what I do from Client side. Send XML and do
socket.shutdownOutput();
Only after this DocumentBuilder (on server side) finishes its work and
returns Documnent.
But then I want to create XMl answer and send it back for Client.
But both halfs of the socket are already closed.
Don't know why.
On Client side it says:
isInputShutdown=false isOutputShutdown=true
On server side:
isInputShutdown=false isOutputShutdown=false
Server: send the answer xml
Server: java.net.SocketException: Socket closed

I expect that Input and Output halfs of the socket from both sides work
separately but it looks that when I close or OutputShutdown output it
closes both streams.
Looks strange for me.

Any ideas?
Any other way to communicate Client/Server with sending/receiving XML?

Of course I can read input, store it, wait for end of XML, create
virtual InputStream (not closing socket's one), have Document, create
Document-Answer, write it back and only then close both input/output.
But it doesn't look elegant.

So, any suggestions?

Have you looked into Socket.shutdownOutput()? It seems like that would
do what you're looking for. If you're using a URLConnection on the
client side, then try invoking getInputStream() on it before closing its
output stream. If none of that is helpful then you'll need to provide
details about what classes and methods the client is using to
communicate; a complete but minimal example code would be best.
 
A

Alex

Thanks for answer.
I do use shutdown. It's clear from this:
isInputShutdown=false isOutputShutdown=true

Source is not necessary because it's not source question.
After a lot of thinking :) I had undertsood that there is no solution
for my problem.
All frameworks solve it in own way. You have to read whole stream,
understand that it's finished, do work, send answer, then close both
input/output.

It's extra work for me to understand by my own that stream already send
all information even it's not closed. It would be much more convinient
if I could close input or output stream (as mark that it's done) and
still use other one.
Unfortunately it doesn't work. And that was my question - why it
doesn't work and what is regular way to work when it's not working.

Again, after thinking, I understood that it's not solved. Also I didn't
want to create buffer to read and store input and then procceed it.
So, I created my own BreakableInputStream class which extends
InputStream and used it inside but pretend to be closed and send only
EOF when mark of finish had arrived.
I used the same as regular HTTP separator \r\r.
So, when \r\r comes BreakableInputStream pretend to be closed. When
real InputStream is still open. So I can inform in this way
DocumentBuilder that it can proceed parse of XML and finish it's job.
Then I have XML Document, then I send answer by socket output and then
I close both input/output for socket without no harm.
Such a long boring way only because I can't shutdown input/output in
socket separately! What a shame...

Thanks for answer anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top