问题描述
packagecom.test;importjava.io.FileOutputStream;importjava.io.OutputStream;importjava.net.Socket;importorg.jdom.Document;importorg.jdom.Element;importorg.jdom.output.XMLOutputter;publicclassXMLClient{publicstaticvoidmain(String[]args)throwsException{Socketsocket=newSocket("localhost",5000);OutputStreamos=socket.getOutputStream();Documentdocument=newDocument();Elementroot=newElement("message");document.addContent(root);Elemente=newElement("type");e.setText("Login");root.addContent(e);e=newElement("userName");e.setText("Jack");root.addContent(e);XMLOutputteroutput=newXMLOutputter();output.output(document,os);}}
packagecom.test;importjava.io.InputStream;importjava.net.ServerSocket;importjava.net.Socket;importorg.jdom.Document;importorg.jdom.Element;importorg.jdom.input.SAXBuilder;publicclassXMLServer{publicstaticvoidmain(String[]args)throwsException{ServerSocketss=newServerSocket(5000);Socketsocket=ss.accept();InputStreamis=socket.getInputStream();SAXBuilderbuilder=newSAXBuilder();Documentdocument=builder.build(is);Elementroot=document.getRootElement();System.out.println(root.getName());}}
运行后报错。。Exceptioninthread"main"java.net.SocketException:Connectionresetatjava.net.SocketInputStream.read(SocketInputStream.java:168)atjava.net.SocketInputStream.read(SocketInputStream.java:182)atcom.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(XMLEntityManager.java:2932)atcom.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:704)atcom.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)atcom.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)atcom.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)atcom.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)atcom.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)atcom.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)atorg.jdom.input.SAXBuilder.build(SAXBuilder.java:518)atorg.jdom.input.SAXBuilder.build(SAXBuilder.java:865)atcom.test.XMLServer.main(XMLServer.java:20)请高手指教啊!!!!
解决方案
解决方案二:
怎么没人解答啊???知道的说下啊
解决方案三:
XMLClient:os.close();socket.close();XMLServer:socket.close();ss.close();main函数最后面加入上面的语句就可以了
解决方案四:
你的这种写法有点像ActionScript里面对XML的支持,你可以参考一下那边的。。本人不赞成用jdom...它的build方法很容易报错,当它执行第二次时一般会报错
解决方案五:
引用2楼sleepson的回复:
XMLClient:os.close();socket.close();XMLServer:socket.close();ss.close();main函数最后面加入上面的语句就可以了
恩。。是这个问题啊。。如果能解释下为什么就更好了