This example uses Java to simulate the Socket server. After the client sends the request information to the server, it obtains the XML Information of the server and then parses and processes it.
1. Socket server
Import java.net. *; import java. io. *; public class TCPServer {public static void main (String args []) {try {int serverPort = 7896; ServerSocket listenSocket = new ServerSocket (serverPort); while (true) {Socket clientSocket = listenSocket. accept (); Connection c = new Connection (clientSocket) ;}} catch (IOException e) {System. out. println ("Listen:" + e. getMessage () ;}} class Connection extends Thread {DataInputStream in; DataOu TputStream out; Socket clientSocket; public Connection (Socket aClientSocket) {try {clientSocket = aClientSocket; in = new DataInputStream (clientSocket. getInputStream (); out = new DataOutputStream (clientSocket. getOutputStream (); this. start ();} catch (IOException e) {System. out. println ("Connection:" + e. getMessage () ;}} public void run () {try {String data = in. readUTF (); // out. writeUTF (data); if (data. equals ("ABC") {out. w RiteUTF ("<result> <! -- Code: 0-error, 1-success --> <code> 0 </code> <weight> 111.33 </weight> <unit> kg </unit> <errmsg> </result> ");}} catch (EOFException e) {System. out. println ("EOF:" + e. getMessage ();} catch (IOException e) {System. out. println ("IO:" + e. getMessage ();} finally {try {clientSocket. close ();} catch (IOException e) {/* failed to close */}}}}
2. The client sends a request to the server and obtains the XML string.
// Test TCP communication protected void onBoReadNoTest () throws Exception {Socket soc = null; InputStreamReader isr = null; // define a readable input stream String data = ""; BufferedReader bf = null; // define the reference InetAddress addr = InetAddress for a read of the BufferedReader type. getByName ("127.0.0.1"); int serverPort = 7896; if (addr. isReachable (5000) {System. out. println ("SUCCESS-ping" + addr + "with no interface specified"); try {soc = new Socket (addr, s ErverPort); System. out. println ("Socket Success! "); DataInputStream in = new DataInputStream (soc. getInputStream (); DataOutputStream out = new DataOutputStream (soc. getOutputStream (); out. writeUTF ("ABC"); // BufferedReader brin = new BufferedReader (new InputStreamReader (soc. getInputStream (); System. out. println ("DataInputStream Success! "); Data = in. readUTF (); // System. out. println ("received data:" + brin. readLine (); // isr = new InputStreamReader (soc. getInputStream (); // create a readable input stream from the socket soc // bf = new BufferedReader (isr ); // use the soc readable input stream as the parameter to create a BufferedReader // data = bf. readLine (); // read the data System sent from the client per unit of action. out. println ("received data:" + data); this. DOM (data); // in. close ();} catch (UnknownHostException e) {System. out. println ("Socket Error:" + e. getMessa Ge ();} catch (EOFException e) {System. out. println ("EOF:" + e. getMessage ();} catch (IOException e) {System. out. println ("IO:" + e. getMessage ();} finally {if (soc! = Null) try {soc. close ();} catch (IOException e) {/* close failed */} else {System. out. println ("FAILURE-ping" + addr + "with no interface specified ");}}
3. XML Parsing
// DOM Parsing Method public void DOM (String data) {long lasting = System. currentTimeMillis (); try {// File f = new File ("F:/xmltest. xml "); // FileInputStream FCM = new FileInputStream (" data. xml "); // BufferedInputStream bis = new BufferedInputStream (FCM); // DataInputStream dis = new DataInputStream (bis); byte [] B = data. getBytes (); InputStream indium = new ByteArrayInputStream (B); DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document doc = builder. parse (indium); NodeList nl = doc. getElementsByTagName ("result"); for (int I = 0; I <nl. getLength (); I ++) {System. out. println ("| code: |" + doc. getElementsByTagName ("code "). item (I ). getFirstChild (). getNodeValue (); System. out. println ("| weight: |" + doc. getElementsByTagName ("weight "). item (I ). getFirstChild (). getNodeValue (); System. out. println ("| unit: |" + doc. getElementsByTagName ("unit "). item (I ). getFirstChild (). getNodeValue (); System. out. println ("| errmsg: |" + doc. getElementsByTagName ("errmsg "). item (I ). getFirstChild (). getNodeValue () ;}} catch (Exception e) {e. printStackTrace ();} System. out. println ("dom runtime:" + (System. currentTimeMillis ()-lasting) + "MS ");}
This article was originally written by Xiao Li's column. For more information, see [conversion from: http://blog.csdn.net/softwave/article/details/8947771]