本文博客連接:http://blog.csdn.net/jdh99,做者:jdh,轉載請註明.html
環境:node
主機:WIN7數據庫
開發環境:Qt5 3.1.2緩存
說明:服務器
在tcp上傳輸xml消息.網絡
協議格式以下:app
2字節標識(0xc55c,網絡序)+2字節預留 +4字節報文內容長度(網絡序) + 4字節命令字(網絡序)+報文內容tcp
部分協議:ide
命令字:
請求值班信息:GET_DUTY_INFO
請求報文:函數
- <?xml version="1.0" encoding="UTF-8"?>
- <request >
- <identifier>客戶端的惟一標識符</identifier>
- </request>
回覆報文:
- <?xml version="1.0" encoding="UTF-8"?>
- <dutyinfo>
- <version>版本號</version>//表示車輛值班記錄有無變化(如增減)
- <vehiclenum>車輛數目</vehiclenum>
- <vehicle>
- <dutyofvehicleuid>32位UUID</dutyofvehicleuid>
- <number>車輛編號, 如1,2,3,4</ number>
- <platenumber>車牌號</platenumber>
- <image>圖片url</image>
- <liquidlevel>78</liquidlevel>//0至100
- </vehicle>
- <vehicle>
- <dutyofvehicleuid>32位UUID</dutyofvehicleuid>
- <number>車輛編號, 如1,2,3,4</ number>
- <platenumber>車牌號</platenumber>
- <image>圖片url</image>
- <liquidlevel>78</liquidlevel>//0至100
- </vehicle>
- …
- </dutyinfo>
處理思路:
1.發送:
調用qt中處理xml文件的類將所須要發送的信息保存爲xml文件,而後讀取文件,將文件轉化爲字節流,並拼接幀頭,而後發送
2.接收
將接收的字節流去掉幀頭後保存爲xml文件,而後調用qt中處理xml文件的類讀取其中的信息
源代碼:
保存爲xml文件
- QFile file(FILE_GET_DUTY_TX);
-
- QDomDocument doc;
- QDomElement root_elem;
- QDomElement item;
- QDomText text;
-
-
- QString header("version=\"1.0\" encoding=\"UTF-8\"");
- doc.appendChild(doc.createProcessingInstruction("xml",header));
-
-
- root_elem = doc.createElement("request");
- doc.appendChild(root_elem);
-
-
- item = doc.createElement("identifier");
- text = doc.createTextNode(QString(Local_Id));
- item.appendChild(text);
- root_elem.appendChild(item);
-
-
- file.open(QIODevice::WriteOnly);
- QTextStream out(&file);
- out.setCodec("UTF-8");
- doc.save(out,4,QDomNode::EncodingFromTextStream);
- file.close();
讀取xml文件,獲得字節流
- file.open(QIODevice::ReadOnly);
- QTextStream get(&file);
-
- Data_Xml_Tx = get.readAll().toLocal8Bit();
- file.close();
發送函數
-
- void Net::slot_net_tx_frame(int cmd,QByteArray frame)
- {
- QByteArray head;
- int i = 0;
-
- i = 0;
-
- head[i++] = 0xc5;
- head[i++] = 0x5c;
-
- head[i++] = 0;
- head[i++] = 0;
-
- head[i++] = frame.size() >> 24;
- head[i++] = frame.size() >> 16;
- head[i++] = frame.size() >> 8;
- head[i++] = frame.size();
-
- head[i++] = cmd >> 24;
- head[i++] = cmd >> 16;
- head[i++] = cmd >> 8;
- head[i++] = cmd;
-
- frame.prepend(head);
-
-
- if (tcp_client->state() == QAbstractSocket::ConnectedState)
- {
-
-
- tcp_client->write(frame);
-
- #ifdef DEBUG
- qDebug() << "發送網絡幀1:cmd" << cmd;
- #endif
- }
- else
- {
-
- Frame = frame;
-
- if (tcp_client->state() != QAbstractSocket::ConnectingState)
- {
- tcp_client->connectToHost(Server_Ip,Server_Port);
- }
- }
- }
tcp接收網絡幀,並保存爲xml文件,而後讀取xml文件中的信息
-
- void Get_Duty::deal_frame()
- {
- int sum = 0;
- int i = 0;
- QSqlQuery q;
- QString version;
-
-
- Lock_Net = 0;
-
- Net_Permission = 0;
-
- qDebug() << "接收幀:回覆值班信息";
-
-
- if ((uint8_t)Frame.at(11) == 0)
- {
-
-
- QFile file(FILE_GET_DUTY_RX);
-
- file.open(QIODevice::WriteOnly);
- QTextStream out(&file);
- out.setCodec("UTF-8");
- out << Frame.mid(LEN_FRAME_HEAD);
- file.close();
-
-
- QDomDocument doc(FILE_GET_DUTY_RX);
-
- file.open(QIODevice::ReadOnly);
- doc.setContent(&file);
- file.close();
-
-
- QDomElement root_node = doc.documentElement();
-
- QDomNode node = root_node.firstChild();
- version = node.toElement().text();
-
- if (version == Version)
- {
- return;
- }
-
-
- Version = version;
- qDebug() << "版本號" << Version;
-
-
- node = node.nextSibling();
- sum = node.toElement().text().toInt();
- qDebug() << "車輛總數" << sum;
-
-
- Car_Three_List[0].clear();
- Car_Three_List[1].clear();
- Car_Three_List[2].clear();
-
- q.prepare("DELETE FROM duty");
- q.exec();
-
- for (i = 0;i < sum;i++)
- {
- node = node.nextSibling();
-
-
- q.prepare("INSERT INTO duty VALUES(?,?,?,?,?,?)");
-
- q.bindValue(0,node.toElement().childNodes().at(0).toElement().text());
-
- q.bindValue(1,node.toElement().childNodes().at(1).toElement().text().toInt());
-
- q.bindValue(2,node.toElement().childNodes().at(2).toElement().text());
-
- q.bindValue(3,node.toElement().childNodes().at(3).toElement().text());
-
- q.bindValue(4,node.toElement().childNodes().at(4).toElement().text().toInt());
-
- q.bindValue(5,IMG_NULL);
- q.exec();
-
-
- if (i < 3)
- {
- Car_Three_List[i] = node.toElement().childNodes().at(0).toElement().text();
- }
- }
-
-
- }
- else
- {
- Version = "null";
-
-
- q.prepare("DELETE FROM duty");
- q.exec();
- }
-
-
- emit sig_recv_duty_info();
-
-
- Frame.clear();
- Len_Frame_Content = 0;
- }
http://blog.csdn.net/jdh99/article/details/38677811