日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

基于QTcpSocket和QTcpServer的Tcp通讯以及QDataStream序列化数据

發布時間:2025/6/17 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于QTcpSocket和QTcpServer的Tcp通讯以及QDataStream序列化数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

最近要在QT下開發Tcp通訊,發送序列化數據以便于接收。

這里涉及到幾個問題:

?

1.QTcpSocket、QTcpServer的通訊

?

2.QDataStream序列化數據

?

?

多的不說,直接上干貨!!!

?

?

?

客戶端:

?

tcpclient.h

?

1 #ifndef TCPCLIENT_H

2 #define TCPCLIENT_H

3

4 #include <QMainWindow>

5 #include <qt4/Qt/qtcpsocket.h>

6 #include <Qt/qhostinfo.h>

7 #include <QDataStream>

8 #include <QtNetwork>

9

10

11

12 struct Control_Motor

13 {

14 int length;

15 int command;

16 QString data;

17 };

18

19

20 class Motor

21 {

22 public:

23 Motor(){}

24 Motor(int speed,int accele_speed,int p_some)

25 {

26 m_speed = speed;

27 m_accele_speed = accele_speed;

28 m_p_some = p_some;

29 }

30

31

32

33 public:

34 int getV(){return m_speed;}

35 int getA(){return m_accele_speed;}

36 int getP(){return m_p_some;}

37

38 void setV(const int v){m_speed = v;}

39 void setA(const int a){m_accele_speed = a;}

40 void setP(const int p){m_p_some = p;}

41

42

43

44 public:

45 //friend QDataStream & operator <<(QDataStream &out,const Motor &motor);

46 //friend QDataStream & operator >>(QDataStream &in,Motor &motor);

47 /*

48 friend QDataStream & operator <<(QDataStream &out,Motor &motor)

49 {

50 out << motor.m_speed<<motor.m_p_some<<motor.m_accele_speed;

51 qDebug()<< motor.m_speed<<motor.m_p_some<<motor.m_accele_speed<<"Enter in this";

52 return out;

53 }

54 */

55 public:

56 int m_speed;

57 int m_accele_speed;

58 int m_p_some;

59

60 };

61

62

63 namespace Ui {

64 class TcpClient;

65 }

66

67 class TcpClient : public QMainWindow

68 {

69 Q_OBJECT

70

71 public:

72 explicit TcpClient(QWidget *parent = 0);

73 ~TcpClient();

74

75 private:

76 Ui::TcpClient *ui;

77 QTcpSocket *tcpClient;

78

79

80 private slots:

81 void slotConnect();

82 void readMessage();

83 void displayError(QAbstractSocket::SocketError);

84 void sendMessage();

85

86

87

88 public:

89 Control_Motor control_Motor;

90

91 Motor *m_motor;

92

93 };

94

95

96 #endif // TCPCLIENT_H

tcpclient.cpp

?

1 #include "tcpclient.h"

2 #include "ui_tcpclient.h"

3 #include <QFile>

4 #include <QDataStream>

5

6

7 QDataStream & operator<<(QDataStream &out,const Motor &motor)

8 {

9 //qDebug()<< motor.m_speed<<motor.m_p_some<<motor.m_accele_speed<<"Enter in";

10 out << motor.m_speed<<motor.m_p_some<<motor.m_accele_speed;

11 //qDebug()<< motor.m_speed<<motor.m_p_some<<motor.m_accele_speed<<"Enter in this";

12 return out;

13 }

14

15 QDataStream &operator >>(QDataStream &in,Motor &motor)

16 {

17 int speed = 0;

18 int accele_speed =0;

19 int p_some = 0;

20

21 in >> speed >> p_some >> accele_speed;

22

23 motor.setV(speed);

24 motor.setP(p_some);

25 motor.setA(accele_speed);

26

27 return in;

28 }

29 Q_DECLARE_METATYPE(Motor)

30

31

32 TcpClient::TcpClient(QWidget *parent) :

33 QMainWindow(parent),

34 ui(new Ui::TcpClient)

35 {

36 ui->setupUi(this);

37

38 qRegisterMetaType<Motor>("Motor");

39

40 tcpClient = new QTcpSocket(this);

41 connect(ui->Connect_Btn,SIGNAL(clicked()),this,SLOT(slotConnect()));

42 connect(ui->Send_Btn,SIGNAL(clicked()),this,SLOT(sendMessage()));

43 connect(tcpClient, SIGNAL(readyRead()), this, SLOT(readMessage()));

44 connect(tcpClient, SIGNAL(error(QAbstractSocket::SocketError)), this,

45 SLOT(displayError(QAbstractSocket::SocketError)));

46 }

47

48 TcpClient::~TcpClient()

49 {

50 delete ui;

51 }

52

53 void TcpClient::slotConnect()

54 {

55 //QString stringAddress = ui->ldt_IP->text();

56 //QString stringPort = ui->ldt_Port->text();

57 QString stringAddress = "192.168.154.128";

58 QString stringPort = "8080";

59 int port = stringPort.toInt();

60 QHostAddress address;

61 address.setAddress(stringAddress);

62 tcpClient->abort();

63 tcpClient->connectToHost(address,port);

64 ui->Connect_Btn->setEnabled(false);

65 }

66

67 void TcpClient::readMessage()

68 {

69

70 }

71

72 void TcpClient::displayError(QAbstractSocket::SocketError)

73 {

74 qDebug() << tcpClient->errorString();

75 }

76

77 void TcpClient::sendMessage()

78 {

79 QString stringMotor = ui->ldt_Motor->text();

80 QString stringData = ui->ldt_data->text();

81 control_Motor.command = stringMotor.toInt();

82 int dataLength = stringData.length();

83 control_Motor.length = 8 + dataLength;

84 control_Motor.data = stringData;

85 QString data = stringMotor+stringData;

86

87 m_motor = new Motor(20,40,60);

88

89 //Motor m_motor(20,40,60);

90

91 //用于暫存要發送的數據

92 QByteArray block;

93 //使用數據流寫入數據

94 QDataStream out(&block,QIODevice::WriteOnly);

95 //設置數據流的版本,客戶端和服務器端使用的版本要相同

96 out.setVersion(QDataStream::Qt_4_6);

97 out<<(quint32) 0;

98 //設置發送長度初始值為0

99 //out << control_Motor.length<<control_Motor.command<<control_Motor.data;

100

101 //qDebug() << control_Motor.length<<control_Motor.command<<control_Motor.data;

102 //out

103 out << control_Motor.command;

104 qDebug()<<"Start out"<<endl;

105 out << *m_motor;

106 qDebug()<<"End out"<<endl;

107 qDebug() << control_Motor.command<< m_motor->getA()<<m_motor->getP()<<m_motor->getV();

108 //回到字節流起始位置

109 out.device()->seek(0);

110 //重置字節流長度

111 //out << (quint16) (block.size()-sizeof(quint16));

112 out << (quint32)(block.size()- sizeof(quint32));

113 qDebug() << "block.size()"<<block.size();

114

115 //往套接字緩存中寫入數據,并發送

116 tcpClient->write(block,block.size());

117 tcpClient->disconnectFromHost();

118 tcpClient->waitForDisconnected();

119

120 block.resize(0);

121 this->close();

122 //tcpClient->write(data.toLatin1(),data.size());

123 }

?

服務器端:

?

tcpserver.h

?

1 ifndef TCPSERVER_H

2 #define TCPSERVER_H

3

4 #include <QMainWindow>

5 #include <qt4/Qt/qhostinfo.h>

6 #include "server.h"

7

8

9 namespace Ui {

10 class TcpServer;

11 }

12

13 class TcpServer : public QMainWindow

14 {

15 Q_OBJECT

16

17 public:

18 explicit TcpServer(QWidget *parent = 0);

19 ~TcpServer();

20

21 private:

22 Ui::TcpServer *ui;

23 int port;

24 Server *server;

25

26 protected slots:

27 void slotCreateServer();

28 void updateServer(QString,int);

29

30 };

31

32 #endif // TCPSERVER_H

?

tcpserver.cpp

?

1 #include "tcpserver.h"

2 #include "ui_tcpserver.h"

3 #include <QtNetwork/QNetworkInterface>

4

5 TcpServer::TcpServer(QWidget *parent) :

6 QMainWindow(parent),

7 ui(new Ui::TcpServer)

8 {

9 ui->setupUi(this);

10 port = 8080;

11 QString address = QNetworkInterface::allAddresses().first().toString();

12 QList<QHostAddress> list2 = QNetworkInterface::allAddresses();

13 foreach (QHostAddress address, list2)

14 {

15 if(address.protocol() == QAbstractSocket::IPv4Protocol)

16 ui->ldt_IP->setText(address.toString());

17 }

18 ui->ldt_Port->setText(QString::number(port));

19 connect(ui->Connect_Btn,SIGNAL(clicked()),this,SLOT(slotCreateServer()));

20 }

21

22 TcpServer::~TcpServer()

23 {

24 delete ui;

25 }

26

27 void TcpServer::slotCreateServer()

28 {

29 server = new Server(this,port);

30 connect(server,SIGNAL(updateServer(QString,int)),this,SLOT(updateServer(QString,int)));

31 ui->Connect_Btn->setEnabled(false);

32 }

33

34 void TcpServer::updateServer(QString msg, int length)

35 {

36 ui->lwt_Text->addItem(msg.left(length));

37 }

?

server.h

?

1 #ifndef SERVER_H

2 #define SERVER_H

3

4 #include <qt4/Qt/qtcpserver.h>

5 #include <qt4/Qt/qtcpsocket.h>

6

7 struct Control_Motor

8 {

9 int length;

10 int command;

11 QString data;

12 };

13

14

15 class Motor

16 {

17 public:

18 Motor(int speed,int accele_speed,int p_some)

19 {

20 m_speed = speed;

21 m_accele_speed = accele_speed;

22 m_p_some = p_some;

23 }

24

25 Motor(){m_speed = 0;}

26

27

28

29 public:

30 int getV(){return m_speed;}

31 int getA(){return m_accele_speed;}

32 int getP(){return m_p_some;}

33

34 void setV(const int v){m_speed = v;}

35 void setA(const int a){m_accele_speed = a;}

36 void setP(const int p){m_p_some = p;}

37

38

39

40 private:

41 int m_speed;

42 int m_accele_speed;

43 int m_p_some;

44

45 };

46

47

48

49 class Server : public QTcpServer

50 {

51 Q_OBJECT

52

53 public:

54 Server(QObject *parents=0,int port=0);

55 QList<QTcpSocket*>TcpClientSocketList;

56 QTcpSocket *tcpClientSocket;

57 signals:

58 void updateServer(QString,int);

59

60 public slots:

61 void slotUpdateClient(QString,int);

62 void slotDisconnect(int);

63 // void slotnewconnection();

64 protected:

65 void incomingConnection(int socketDescriptor);

66

67 signals:

68 void updateClients(QString,int);

69 void disconnected(int);

70

71 protected slots:

72 void dataReceive();

73 void slotDisconnected();

74

75 public:

76 Control_Motor control_motor;

77 Motor m_mtor;

78 };

79

80 #endif // SERVER_H

?

server.cpp

?

1 #include "server.h"

2

3 QDataStream &operator <<(QDataStream &out,Motor &motor)

4 {

5 out << motor.getV()<<motor.getP()<<motor.getA();

6 return out;

7 }

8

9

10 QDataStream &operator >>(QDataStream &in,Motor &motor)

11 {

12 int speed = motor.getV();

13 int accele_speed =motor.getA();

14 int p_some = motor.getP();

15

16 in >> speed >> p_some >> accele_speed;

17

18 motor.setV(speed);

19 motor.setP(p_some);

20 motor.setA(accele_speed);

21

22 return in;

23 }

24

25

26

27

28 Server::Server(QObject *parent,int port) :

29 QTcpServer(parent)

30 {

31 this->listen(QHostAddress::Any,port);

32 }

33

34 void Server::incomingConnection(int socketDescriptor)

35 {

36 tcpClientSocket = new QTcpSocket(this);

37 //tcpClientSocket = this->nextPendingConnection();

38 tcpClientSocket->setSocketDescriptor(socketDescriptor);

39 TcpClientSocketList.append(tcpClientSocket);

40 //connect(this,SIGNAL(updateClients(QString,int)),this,SLOT(slotUpdateClient(QString,int)));

41 //connect(this,SIGNAL(updateClients(QString,int)),this,SLOT(slotUpdateClient(QString,int)));

42 connect(this,SIGNAL(disconnected(int)),this,SLOT(slotDisconnect(int)));

43 connect(tcpClientSocket,SIGNAL(readyRead()),this,SLOT(dataReceive()));

44 connect(tcpClientSocket,SIGNAL(disconnected()),this,SLOT(slotDisconnected()));

45 //connect(tcpClientSocket,SIGNAL(disconnected()),tcpClientSocket,SLOT(deleteLater()));

46

47 }

48 void Server::slotUpdateClient(QString msg,int length)

49 {

50 emit updateServer(msg,length);

51 for (int i=0;i<TcpClientSocketList.count();i++)

52 {

53 QTcpSocket *item=TcpClientSocketList.at(i);

54 if (item->write(msg.toLatin1(),length)!=length)

55 continue;

56 }

57 }

58 void Server::slotDisconnect(int socketDescriptor)

59 {

60 qDebug()<<__FILE__<<__FUNCTION__<<__LINE__<<endl;

61 for (int i=0;i<TcpClientSocketList.count();i++)

62 if (TcpClientSocketList.at(i)->socketDescriptor()==socketDescriptor)

63 {

64 TcpClientSocketList.removeAt(i);

65 return;

66 }

67 }

68

69 void Server::dataReceive()

70 {

71 qDebug()<<"QQWQW11111111111111";

72 //quint32 size = 0;

73 quint32 nextBlockSize = 0;

74 qDebug()<<"TcpClientSocketList.count()"<<TcpClientSocketList.count();

75 //for(int i = 0; i < TcpClientSocketList.count();i++)

76 //{

77 QDataStream in(tcpClientSocket);

78 in.setVersion(QDataStream::Qt_4_6);

79 if(nextBlockSize == 0)

80 {

81 if(tcpClientSocket->bytesAvailable()<sizeof(quint32))

82 {

83 //break;

84 return;

85 }

86 in>>nextBlockSize;

87 qDebug()<<nextBlockSize;

88 }

89 if(nextBlockSize==0xFFFF)

90 {

91 //break;

92 return;

93 }

94 if(tcpClientSocket->bytesAvailable()<nextBlockSize)

95 {

96 //break;

97 return;

98 }

99

100 //in>>control_motor.length>>control_motor.command>>control_motor.data;

101

102 //qDebug()<<control_motor.length<<control_motor.command<<control_motor.data;

103 in >>control_motor.command >> m_mtor;

104 qDebug()<<control_motor.command<< m_mtor.getA()<<m_mtor.getP()<<m_mtor.getV();

105

106 //ui->SN_lineEdit_2->setText(QString("%1").arg(message_rev.SN));

107 //ui->IP_lineEdit->setText(message_rev.IP);

108 //ui->STATE_lineEdit_3->setText(message_rev.Condition);

109 //}

110 }

111

112 void Server::slotDisconnected()

113 {

114 qDebug()<<__FILE__<<__FUNCTION__<<__LINE__<<endl;

115 emit disconnected(tcpClientSocket->socketDescriptor());

116 }

?

?

在這里要特別說明一下,在此遇到的幾個問題,希望能幫到大家,也提醒一下自己。

?

1.在TcpClient.pro,TcpServer.pro里一定要注意加上QT += network,不然編譯的時候QNetworkInterface、QHostAddress這些地方會報錯誤。

?

2.在QDataStream重載自定義的類的時候,一開始把重載寫在.h文件里編譯的時候總是報Tcp_DataStream/TcpServer/server.h:49: error: multiple definition of `operator<<(QDataStream&, Motor&)'這個錯誤,說實話這個問題真的困擾了我很久,然后把重載函數放到.cpp文件里,編譯通過,簡直是要命

?

3.QDataStream的nextBlock讀取到的長度為發送的數據的長度(而不是整個包的長度out << (quint32)(block.size()- sizeof(quint32));),如果直接寫out << (quint32)(block.size()),讀的時候數據將會不正常。

?

4.重載的時候一定要正確。

?

?

?

?

源代碼連接:http://download.csdn.net/detail/cmp15845953187/8800409

轉載于:https://my.oschina.net/qtplus/blog/1942299

總結

以上是生活随笔為你收集整理的基于QTcpSocket和QTcpServer的Tcp通讯以及QDataStream序列化数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产精品一区二区麻豆 | 亚洲人一区| 亚洲成人av | 97caocao| 麻豆传谋在线观看免费mv | 九一精品一区 | 中文在线资源 | 99在线观看免费视频 | 亚洲精品www久久久久久 | 亚洲人成在线观看 | 亚洲男人天堂电影 | 无码人妻精品一区二区三区不卡 | 国产精品久久久久久久久久免费看 | 哺乳期给上司喂奶hd | 伊伊综合网 | 老熟女毛茸茸浓毛 | 午夜在线视频免费 | 久久男人的天堂 | 国产毛片毛片毛片 | 三级视频黄色 | 国产欧美在线观看视频 | 午夜看毛片| 99热这里只有精品9 日韩综合在线 | 黄色免费网站在线看 | 狼人色综合 | 国产精品久久久久久久午夜 | 91麻豆成人 | 精品久久久久久无码国产 | 国产成人一区二区三区 | 色哥网| 日韩欧美一区二区三区免费观看 | 色窝窝无码一区二区三区 | 精品久久久中文字幕人妻 | 人妻久久一区二区 | 欧美日韩黄色一级片 | 欧美少妇色图 | 国产精品久久久国产盗摄 | 少妇三级 | 免费国产在线观看 | 狠狠干美女 | 欧美夫妇交换xxx | 看黄色一级片 | 天堂av片| 国产精品久久一区二区三区 | 丰满尤物白嫩啪啪少妇 | 四虎成人影视 | 日本久热 | 免费观看在线视频 | 日日夜夜操av | 黄色一级国产 | 全黄一级男人和女人 | 亚洲欧美视频一区二区 | 精品一区在线播放 | 91视频在| 国产精品99精品无码视 | 欧美中文字幕一区二区三区 | 欢乐谷在线观看免费播放高清 | 我们的2018在线观看免费高清 | 91精品久久久久久久久久 | 三上悠亚一区二区在线观看 | 国产精品一区二区三区免费视频 | 91av视频在线 | 日美韩av| 日韩黄色精品 | 在线免费观看视频网站 | 臭脚猛1s民工调教奴粗口视频 | 国产网站在线看 | 免费性片 | 国产淫语 | 狠操av | 中文字幕亚洲乱码熟女一区二区 | 中文字幕不卡视频 | 日韩精品在线电影 | 国产又粗又猛又爽又黄91 | 娇妻第一次尝试交换的后果 | 国产成人区 | 国产欧美精品久久久 | 99re6热在线精品视频播放 | 成人久久一区 | 欧美日韩在线免费 | 中文字幕――色哟哟 | 密桃成熟时在线观看 | 国产精品网站免费 | 97射射| 欧美sm视频 | 一区二区网 | 欧美人与性动交α欧美精品 | 爱爱视频网 | wwwwxxxx国产 | 欧美jizz19性欧美 | 伊人激情视频 | 亚洲 另类 春色 国产 | 91色交| 麻豆系列| 亚洲精品国产精品乱码不99热 | 九色视频在线观看 | 一区二区三区蜜桃 | 国产又黄又爽视频 | 在线观看国产网站 |