XmlBeans and the problem with Socket.getOutputStream()

Discussion in 'Java' started by Gordon Beaton, Dec 5, 2005.

  1. It sounds to me like you have conflicting requirements. If parse()
    needs to read to EOF, then your client needs to close the OutputStream
    or at least call socket.shutdownOutput() before that can occur. On the
    other hand if you need to send more data on the *same* stream, then
    EOF is the wrong condition for parse() to be waiting for.

    An alternative is to have the client open a new connection for each
    request, and use socket.shutdownOutput() to signal EOF before reading
    the response.

    Another is to have the server read from the incoming stream to
    intermediate storage, then create (for example) a
    ByteArrayInputStream() that you can pass to parse().

    Gordon Beaton, Dec 5, 2005
    1. Advertisements

  2. Hello,

    I am using XmlBeans v2 for a project where I need to send an xml file
    through the network. I have a server that waits for incoming requests,
    and a
    client that builds the xml, sends it to the server, and then it waits
    for the
    response xml.

    So I'm using on the client something like this :

    MyXmlDoc xmlDoc = MyXmlDoc.Factory.newInstance();
    // here I create & populate the xml with data

    Socket socket = new Socket( host, port );
    xmlDoc.save( socket.getOutputStream() );

    MyResponseXmlDoc responseDoc = MyResponseXmlDoc.Factory.parse(
    socket.getInputStream() );

    And on the server :

    ServerSocket serverSocket = new ServerSocket( port );
    Socket clientSocket = socket.accept();

    MyXmlDoc xmlDoc = MyXmlDoc.Factory.parse( clientSocket.getInputStream()
    // process the request xml

    MyResponseXmlDoc responseDoc = MyResponseXmlDoc.Factory.newInstance();
    // create the response xml

    responseDoc.save( clientSocket.getOutputStream() );

    Now, the problem that I'm having is on the server. The parse() method
    doesn't stop reading
    data from inputStream after the xml was completely sent to it. So it
    keeps waiting for data, even if the object on the client has finished
    sending data.

    I tried sending, to the server, a '\0' character, after the call to

    xmlDoc.save( socket.getOutputStream() );
    hoping that maybe this way I could signal an end of file to the reader
    on the server. But it worked only partially, because the reader indeed
    finished reading file, but then the xml parser cried out, saying that
    0x0 character was not part of the xml file.

    I mention that I also tried to flush() the streams, after sending the
    xmls, but this was again a useless action.

    This problem is really nasty to me, since I want to be able to send
    more data on the outputstream, from the client, after the xml was sent.
    So, to close the outputstream after the xml was sent, it doesn't
    really help me (and, moreover, if I close the outputstream, I get a new
    exception when I try to close the socket or when I try to read from the
    InputStream, which I think is natural).

    I hope the provided information regarding my problem is enough.

    Best regards,
    bogdan.mocanu, Dec 5, 2005
    1. Advertisements

  3. Thanks Gordon for you quick answer.

    Yes indeed, the only option that I can think of now is the third
    approach (create a secondary stream, read data to it and
    then pass this stream to XmlBeans).

    What I find strange about this is that my problem is not a very
    complicated one, and I thought there is another way to solve it,
    something related to XmlBeans. I mean it is natural to think that
    an xml created with XmlBeans should be easily sent through the
    network and the parsed at the other end of the cable.

    Anyway, thanks for the answer.
    bogdan.mocanu, Dec 5, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.