Toon posts:

[Java] XML encoding/decoding over sockets

Pagina: 1
Acties:

Verwijderd

Topicstarter
Sorry dat het in het engels is, maar ik wou het posten op het Sun forum, maar dat werkte weer eens niet :(

Hi there,

We're working on an application that should transfer objects over a network. We encode and decode the objects with the java.beans.XMLEncoder and XMLDecoder classes. The application can transfer the xml data via HTTP and via Sockets, but with the socket tranfer some problems have occured.

First of all, when we tried to XMLEncode object directly onto the clients socket outputstream and XMLDecode from the socket on the server, nothing happened. After a quick look in the source files, we noticed we had to close the XMLEncoder, since otherwise it would not send the </java> end tag. However, closing the XMLEncoder causes the underlying stream to close. Normally this should not be a problem, but closing a sockets outputstream will, to our surprise, also close the socket.

To fix this we kept a refference to the socket outputstream the XMLEncoder writes to and manually printed the </java> end tag on the stream once the object transfer was complete. Although the xml is transferred succesfully using this workaround, the XMLDecoder will not start decoding, probably because it waits for the end of the stream. To get around this one we had to shut down the output on the client socket (default API method).

Once we had fixed this the XMLDecoder on the server did its job, but when server wanted to send back its response to the client, it started complaining that the socket was closed! Evidently shutting down a client sockets output will completely shut down the server socket (the socket used by the server, not a ServerSocket :) ). The only workaround we haven't tried is using some kind of PipedOutput/Input pair around both sockets, so we can close streams without closing the socket. Although this might actually work, it's not a very efficient, elegant or even easy solution.

Does anyone have any idea on how to solve this? And isn't the socket behaviour described above some kind of bug?

Thanks in advance and if you can fix this, you'll defenitely have earned some DukeDollars :)


btw.: we also tried sending a whole bunch of platform dependend End Of Stream characters through the socket to get the XMLDecoder to do something, but to no avail.

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Ik denk dat dit probleem misschien vooral te maken heeft met de Crimson parser in JAXP. De XMLDecoder maakt namelijk gewoon gebruik van SAX en maakt de XMLReader aan via JAXP. In de huidige SDK/JRE zit nog steeds dat Crimson geval en het blijkt dat deze behoorlijk wat problemen heeft met het parsen van XML die nog aan het binnenkomen is.

Kijk maar eens in dit topic op Javahova, wat een grote overeenkomst heeft met jouw probleem:

http://forum.javahova.net/topic.php?id=673

Wellicht dat je probleem dus al een stukje wordt opgelost als je Xerces op je CLASSPATH zet, welke dan gebruikt zal worden als parser door JAXP.

Overigens vind ik het ontwerp van de XMLEncoder/Decoder ontzettend slecht en ze hebben duidelijk niet gedacht aan alle toepassingen, behalve het serializeren van Swing GUIs. Ik zou bijvoorbeeld graag de XMLDecoder willen gebruiken in een SAX chain met behulp van XML Filters en dus direct de ContentHandler willen gebruiken uit de XMLDecoder. Ook is het idioot dat de XMLEncoder alleen maar OutputStream accepteert en geen Writers. Veel beter hadden ze het zelfs zo kunnen ontwerpen dat je een ContentHandler moet meegeven waarheen de XML resultaten verzonden worden. Je kan dan zelf beslissen hoe je de zaak wilt serializeren en jouw probleem zou dan wellicht ook veel makkelijker op te lossen zijn geweest.

Zelf had ik met name de chains zo hard nodig en ik werd zo sacherijnig van die knudde implementatie dat ik gewoon de source geripped en aangepast heb. Helaas kan ik dat alleen nooit in een product gebruiken natuurlijk :( .


Met pure HTTP heb je neem ik aan geen problemen omdat de HTTP implementatie met behulp van de Content-Length het einde van de body bepaald en dus de stream ophoudt?

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


Verwijderd

Topicstarter
Bedankt voor je reply! Volgens mij is dit precies wat ik nodig heb :)

Via HTTP werkt alles idd zoals het hoort. We hadden ook nog wat uitgeprobeerd om via writers/readers de XML door te sturen, maar dankzij de overhead van het aanmaken van string/buffers/andere rommel duurde het overzetten van een gemiddeld XML bestand zo'n 2 minuten :+

nogmaals bedankt :)

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
websjwans: Volgens mij is dit precies wat ik nodig heb :)
Mooi :) .
nogmaals bedankt :)
Graag gedaan, ik vind het leuk dat er hier mensen mee bezig zijn :) .

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment