【轉】Qt Socket簡單通訊

來源:互聯網
上載者:User

標籤:

最近要用到Qt的Socket部分,網上關於這部分的資料都比較複雜,我在這總結一下,把Socket的主要部分提取出來,實現TCP和UDP的簡單通訊。

 

1.UDP通訊UDP沒有特定的server端和client端,簡單來說就是向特定的ip發送報文,因此我把它分為發送端和接收端。 注意:在.pro檔案中要添加QT += network,否則無法使用Qt的網路功能。1.1.UDP發送端

 

  1. #include <QtNetwork>
  2. QUdpSocket *sender;
  3. sender = new QUdpSocket(this);
  4. QByteArray datagram = “hello world!”;
  5. //UDP廣播
  6. sender->writeDatagram(datagram.data(),datagram.size(),QHostAddress::Broadcast,6665);
  7. //向特定IP發送
  8. QHostAddress serverAddress = QHostAddress("10.21.11.66");
  9. sender->writeDatagram(datagram.data(), datagram.size(),serverAddress, 6665);
  10. /* writeDatagram函數原型,發送成功返回位元組數,否則-1
  11. qint64 writeDatagram(const char *data,qint64 size,const QHostAddress &address,quint16 port)
  12. qint64 writeDatagram(const QByteArray &datagram,const QHostAddress &host,quint16 port)
  13. */

 

1.2.UDP接收端

 

  1. #include <QtNetwork>
  2. QUdpSocket *receiver;
  3. //訊號槽
  4. private slots:
  5. void readPendingDatagrams();
  6. receiver = new QUdpSocket(this);
  7. receiver->bind(QHostAddress::LocalHost, 6665);
  8. connect(receiver, SIGNAL(readyRead()),this, SLOT(readPendingDatagrams()));
  9. void readPendingDatagrams()
  10. {
  11. while (receiver->hasPendingDatagrams()) {
  12. QByteArray datagram;
  13. datagram.resize(receiver->pendingDatagramSize());
  14. receiver->readDatagram(datagram.data(), datagram.size());
  15. //資料接收在datagram裡
  16. /* readDatagram 函數原型
  17. qint64 readDatagram(char *data,qint64 maxSize,QHostAddress *address=0,quint16 *port=0)
  18. */
  19. }
  20. }

 

2.TCP通訊TCP的話要複雜點,必須先建立串連才能傳輸資料,分為server端和client端。2.1.TCP client端

 

  1. #include <QtNetwork>
  2. QTcpSocket *client;
  3. char *data="hello qt!";
  4. client = new QTcpSocket(this);
  5. client->connectToHost(QHostAddress("10.21.11.66"), 6665);
  6. client->write(data);

 

2.2.TCP server端

 

  1. #include <QtNetwork>
  2. QTcpServer *server;
  3. QTcpSocket *clientConnection;
  4. server = new QTcpServer();
  5. server->listen(QHostAddress::Any, 6665);
  6. connect(server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
  7. void acceptConnection()
  8. {
  9. clientConnection = server->nextPendingConnection();
  10. connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readClient()));
  11. }
  12. void readClient()
  13. {
  14. QString str = clientConnection->readAll();
  15. //或者
  16. char buf[1024];
  17. clientConnection->read(buf,1024);
  18. }
wuyuan 本文來自Wuyuan‘s Blog 轉載請註明,謝謝! 文章地址: http://wuyuans.com/2013/03/qt-socket

【轉】Qt Socket簡單通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.