socket\KeepSocketOpen.java - Apache Xerves Socket Sample

Q

What is the socket\KeepSocketOpen.java provided in the Apache Xerces package? I have Apache Xerces 2.11.0 installed.

✍: FYIcenter

A

socket\KeepSocketOpen.java provided in the Apache Xerces package provides a solution to the problem of 1) sending multiple XML documents over a single socket connection or 2) sending other types of data after the XML document without closing the socket connection.

The first situation is a problem because the XML specification does not allow a document to contain multiple root elements. Therefore a document stream must end (or at least appear to end) for the XML parser to accept it as the end of the document.

The second situation is a problem because the XML parser buffers the input stream in specified block sizes for performance reasons. This could cause the parser to accidentally read additional bytes of data beyond the end of the document. This actually relates to the first problem if the documents are encoding in two different international encodings.

The solution that this sample introduces wraps both the input and output stream on both ends of the socket. The stream wrappers introduce a protocol that allows arbitrary length data to be sent as separate, localized input streams. While the socket stream remains open, a separate input stream is created to "wrap" an incoming document and make it appear as if it were a standalone input stream.

Here is how to run socket\DelayedInput.java, if you have Apache Xerces 2.11.0 installed:

\fyicenter\xerces-2_11_0\samples>\fyicenter\jdk-1.8.0\bin\javac 
   -cp ..\xercesImpl.jar;..\xercesSamples.jar socket\KeepSocketOpen.java
Note: socket\KeepSocketOpen.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

C:\fyicenter\xerces-2_11_0\samples>\fyicenter\jdk-1.8.0\bin\java 
   -cp .;..\xercesImpl.jar;..\xercesSamples.jar 
   socket.KeepSocketOpen User.xml UserDTD.xml UserXSD.xml
Server: Created.
Client: Created.
Server: Running.
Client: Running.
Server: Client connected.
Server: Opening file "User.xml"
Server: Wrote 226 byte(s) total.
Server: Opening file "UserDTD.xml"
Server: Wrote 411 byte(s) total.
Server: Opening file "UserXSD.xml"
Server: Wrote 411 byte(s) total.
Server: Exiting.
Client: Read 226 byte(s) total.
Client: 2 ms (5 elems, 0 attrs, 0 spaces, 58 chars)
Client: Read 411 byte(s) total.
Client: 6 ms (5 elems, 0 attrs, 21 spaces, 37 chars)
Client: Read 411 byte(s) total.
Client: 2 ms (5 elems, 3 attrs, 0 spaces, 62 chars)
Client: Exiting.

 

Using Apache Xerces Socket Sample Programs

⇒⇒FAQ for Apache Xerces XML Parser

2017-09-20, 285👍, 0💬