Transmit XML messages using TCP in QT

Source: Internet
Author: User

Transmit XML messages using TCP in QT


For more information, see http://blog.csdn.net/jdh99, :jdh.


Environment:

HOST: win7

Development Environment: qt5 3.1.2


Note:

Transmit XML messages over TCP.


The Protocol format is as follows:

2-byte identifier (0xc55c, network order) + 2-byte reserved + 4-byte packet content length (Network order) + 4-byte command word (Network order) + packet content


Some protocols:

Command word:
Request duty information:Get_duty_info
Request Message:

<? XML version = "1.0" encoding = "UTF-8"?> <Request> <identifier> unique identifier of the client </identifier> </request>


Reply message:

<? XML version = "1.0" encoding = "UTF-8"?> <Dutyinfo> <version> Version Number </version> // indicates whether the vehicle duty record has changed (such as increase or decrease) <vehiclenum> Number of vehicles </vehiclenum> <vehicle> <dutyofvehicleuid> 32-bit UUID </dutyofvehicleuid> <number> vehicle ID, such as 1, 2, 3, 4 </number> <platenumber> license plate number </platenumber> <image> image URL </image> <liquidlevel> 78 </liquidlevel> // 0 to 100 </vehicle> <vehicle> <dutyofvehicleuid> 32-bit UUID </dutyofvehicleuid> <number> vehicle ID, for example, 1, 2, 3, 4 </number> <platenumber> license plate number </platenumber> <image> image URL </image> < Liquidlevel> 78 </liquidlevel> // 0 to 100 </vehicle>... </Dutyinfo>

Solution:

1. Send:

Call the class for processing XML files in QT to save the information to be sent as an XML file, read the file, convert the file to a byte stream, splice the frame header, and then send


2. Receive

Remove the received byte stream from the frame header, save it as an XML file, and then call the class in QT to process the XML file to read the information.


Source code:

Save as an XML file

// Open the XML command qfile file (file_get_duty_tx) to be sent; // generate the XML file qdomdocument Doc; qdomelement root_elem; qdomelement item; qdomtext text; // XML file header qstring header ("version = \" 1.0 \ "encoding = \" UTF-8 \ "); Doc. appendchild (Doc. createprocessinginstruction ("XML", header); // root element root_elem = Doc. createelement ("request"); Doc. appendchild (root_elem); // element: identifier item = Doc. createelement ("identifier"); text = Doc. createtextnode (qstring (local_id); item. appendchild (text); root_elem.appendchild (item); // create a file and save the file. open (qiodevice: writeonly); qtextstream out (& file); out. setcodec ("UTF-8"); Doc. save (out, 4, qdomnode: encodingfromtextstream); file. close ();

Read the XML file to obtain the byte stream.

// Read the XML file. open (qiodevice: readonly); qtextstream get (& file); // get the XML data data_xml_tx = get. readall (). tolocal8bit (); file. close ();

Sending Function

/*************************************** * Function: send network frames * Number of frames: cmd: frame command * frame: ************************************* * *******************************/void net:: slot_net_tx_frame (INT cmd, qbytearray frame) {qbytearray head; int I = 0; I = 0; // frame header head [I ++] = 0xc5; head [I ++] = 0x5c; // reserved head [I ++] = 0; head [I ++] = 0; // header [I ++] = frame. size ()> 24; head [I ++] = frame. si Ze ()> 16; head [I ++] = frame. size ()> 8; head [I ++] = frame. size (); // command word head [I ++] = cmd> 24; head [I ++] = cmd> 16; head [I ++] = cmd> 8; head [I ++] = cmd; // Composite Frame. prepend (head); // infer whether the current server is connected if (tcp_client-> state () = q1_actsocket: connectedstate) {// connected // send data tcp_client-> write (FRAME); # ifdef debug qdebug () <"Send network frame 1: cmd" <cmd; # endif} else {// frame = frame not connected; // connect to serve R if (tcp_client-> state ()! = Qmeanactsocket: connectingstate) {tcp_client-> connecttohost (server_ip, server_port );}}}

TCP receives the network frame, saves it as an XML file, and then reads the information in the XML file.

/*************************************** ****** **************************************** * **********************/Void get_duty:: deal_frame () {int sum = 0; int I = 0; qsqlquery Q; qstring version; // unlock lock_net = 0; // discard Processing Network Information net_permission = 0; qdebug () <"receive frame: reply duty information"; // infer whether there is duty information if (uint8_t) frame. at (11) = 0) {// with a title, store the data to an XML file // open the XML file qfile file (Fi Le_get_duty_rx); // create a file and save the file. open (qiodevice: writeonly); qtextstream out (& file); out. setcodec ("UTF-8"); out <frame. mid (len_frame_head); file. close (); // open the XML file qdomdocument DOC (file_get_duty_rx); // obtain the file content file. open (qiodevice: readonly); Doc. setcontent (& file); file. close (); // obtain the root node qdomelement root_node = doc.doc umentelement (); // obtain the first subnode: Version qdomnode node = root_node.firstchild (); version = N Ode. toelement (). text (); // infer whether the version number is consistent if (version = version) {return ;}// inconsistent version = version; qdebug () <"version number" <version; // next sub-node: Total number of vehicles node = node. nextsibling (); sum = node. toelement (). text (). toint (); qdebug () <"Total number of vehicles" <sum; // clear information about the first three vehicles car_three_list [0]. clear (); car_three_list [1]. clear (); car_three_list [2]. clear (); // clear the duty information table Q. prepare ("delete from duty"); q.exe C (); // write data to the database for (I = 0; I <Sum; I ++) {node = node. nextsibling (); // insert data Q. prepare ("insert into duty values (?,?,?,?,?,?) "); // Uuid q. bindvalue (0, node. toelement (). childnodes (). at (0 ). toelement (). text (); // number Q. bindvalue (1, node. toelement (). childnodes (). at (1 ). toelement (). text (). toint (); // license plate Q. bindvalue (2, node. toelement (). childnodes (). at (2 ). toelement (). text (); // picture Q. bindvalue (3, node. toelement (). childnodes (). at (3 ). toelement (). text (); // gasoline Q. bindvalue (4, node. toelement (). childnodes (). at (4 ). toelement (). text (). toint (); // Picture Mark Q. bindvalue (5, img_null); q.exe C (); // input the information of the first three vehicles if (I <3) {car_three_list [I] = node. toelement (). childnodes (). at (0 ). toelement (). text () ;}/// print the database // Q. prepare ("select * from duty"); // q.exe C (); // while (Q. next () // {// qdebug () <"UUID" <q. value (0 ). tostring () // <"no." <q. value (1 ). toint () // <"license plate" <q. value (2 ). tostring () // <"image" <q. value (3 ). tostring () // <"Gasoline" <q. value (4 ). tostring () // <"image flag" <q. value (5 ). toint (); //} else {version = "null"; // clear the duty information table Q. prepare ("delete from duty"); q.exe C ();} // receives the duty message emit sig_recv_duty_info (); // clears the receiving cache frame. clear (); len_frame_content = 0 ;}









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.