我目前正在开发一个解析XML输入流中的天气数据的项目。但是,我遇到了与Linux相关的奇怪错误。
当运行在Windows上接收输入流的服务器软件时,一切都像魅力一样。但是,在Linux上运行服务器时,会出现以下错误。正确收到几条消息后,第三条消息将被破坏,因为输入缓冲区的一半将会丢失"。
此问题仅在以下情况下发生:
[Platform] [Origin of XML stream] [Error occurs]
Windows localhost No
Windows remote No
Linux localhost No
Linux remote Yes
我使用以下代码接收XML流。
private Document getXML() throws JDOMException
{
SAXBuilder builder = new SAXBuilder();
try {
// Get the input stream.
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String xmlstream = "";
String line;
while (!(line = in.readLine()).contains("</MEASUREMENT>")){
xmlstream += line;
}
xmlstream += "</MEASUREMENT></WEATHERDATA>";
System.out.println("XML DATA:" + xmlstream);
Document xmlDocument = builder.build(new StringReader(xmlstream));
return xmlDocument;
} catch (IOException | NullPointerException e) {
// Socket closes.
System.out.println("Client disconnected!");
return null;
}
}
以下是收到数据的示例:
XML DATA:<?xml version="1.0"?><WEATHERDATA> <MEASUREMENT> <STN>726030</STN> <DATE>2018-01-27</DATE> <TIME>13:42:01</TIME> <TEMP>-2.8</TEMP> <DEWP>-2.7</DEWP> <STP>1014.1</STP> <SLP>1019.3</SLP> <VISIB>8.2</VISIB> <WDSP>29.0</WDSP> <PRCP>0.02</PRCP> <SNDP>0.0</SNDP><FRSHTT>110000</FRSHTT> <CLDC>77.1</CLDC> <WNDDIR>191</WNDDIR></MEASUREMENT></WEATHERDATA>
XML DATA:<?xml version="1.0"?><WEATHERDATA> <MEASUREMENT> <STN>726030</STN> <DATE>2018-01-27</DATE> <TIME>13:42:02</TIME> <TEMP>-0.7</TEMP> <DEWP>-3.5</DEWP> <STP>1014.2</STP> <SLP>1019.2</SLP> <VISIB>8.3</VISIB> <WDSP>28.9</WDSP> <PRCP>0.02</PRCP> <SNDP>0.0</SNDP><FRSHTT>110000</FRSHTT> <CLDC>77.2</CLDC> <WNDDIR>191</WNDDIR></MEASUREMENT></WEATHERDATA>
XML DATA:/DATE> <TIME>13:42:02</TIME> <TEMP>-9.4</TEMP> <DEWP>-13.5</DEWP> <STP>1005.1</STP> <SLP>1013.2</SLP> <VISIB>22.8</VISIB> <WDSP>12.8</WDSP> <PRCP>0.25</PRCP> <SNDP>8.3</SNDP> <FRSHTT>111000</FRSHTT> <CLDC>50.0</CLDC> <WNDDIR>311</WNDDIR></MEASUREMENT></WEATHERDATA>
正如您所看到的,前两条消息是以它的顺序收到的,但是第三条消息是在流的中间随机开始的。