How to pass XML file content through socket without generating XML files

Source: Internet
Author: User

Recently, I encountered a problem in my project. I want to generate xml file content on the client but not create a file on the hard disk (this is not required), which can reduce I/O operations and improve efficiency, send XML content directly. JDOM is quite useful. You can use the following code to complete this task and generate an XML string.

// Omit the XML Generation Code document DOC = new document (Root); xmloutputter xmlout = new xmloutputter (format); // generate an XML string, that is, the content of the XML file, contains many linefeeds: String xmlstring = xmlout. outputstring (DOC); // send the XML string printwriter out = new printwriter (socket. getoutputstream (), true); out. println (xmlstring); out. flush ();

But there is a problem, because we need to maintain a persistent connection, how can we know that the file content has been uploaded on the server end? The code at the beginning of the server side is as follows, and a timeout exception always occurs.

Bufferedreader in = new bufferedreader (New inputstreamreader (processsocket. getinputstream (); string MSG = ""; string temp = ""; //////////////////////////////////////// //// // The block is blocked after reading it, if an exception is thrown, how can I know that the content of the XML file has been uploaded? //////////////////////////////////////// /// // While (temp = in. readline ())! = NULL) {MSG + = temp ;}

In order to solve this problem, I searched for a long time and never found the answer. Then I sent a post to the Forum and asked, and finally understood it. The post address is workshop.

If it is a short connection, the client will directly turn off the socket after sending data, and the server Readline () will accept null, so my program can run normally. However, due to persistent connections, the client socket waits for the data on the server to be blocked. After the client sends data, the server Readline () is blocked because it waits for the data on the other side, wait for each other to form a deadlock, so timeout occurs.

There are two solutions:

1. specify the length of the data before sending data to the client. The server first receives the data size and then creates an array of the corresponding length to receive the data;

2. According to the XML format, when reading XML, the mark indicates that the data has been received.

Due to project cooperation, the second method was used to solve the problem.

PS: when searching for this issue, we also encountered something similar to directly sending data without saving XML files. Some people mentioned that web service and soap are also used for such applications, if you are free, study it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.