"Go" Qt Socket simple Communication

Source: Internet
Author: User

Recently to use the socket part of QT, online about this part of the data are more complex, I summed up in this, the main part of the socket out to achieve the TCP and UDP simple communication.

1.UDP communication UDP does not have a specific server side and client side, simply to send a message to a specific IP, so I divide it into the sender and the receiver side. Note: You will not be able to use Qt's network features in the. Pro file if you want to add qt + = Networks。 1.1.UDP Send End

  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 Receiving End

  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 communication TCP Words to complicate points, you must first establish a connection to transfer data, divided into server and client side. 2.1.TCP Client Side

  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 Side

  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 This article from Wuyuan ' s Blog reprint please specify, thank you! Article Address: Http://wuyuans.com/2013/03/qt-socket

"Go" Qt Socket simple Communication

Related Article

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.