image.png

广播组

image.png

客户端1

image.png

widget.h

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QUdpSocket> //UDP套接字
  5. namespace Ui {
  6. class Widget;
  7. }
  8. class Widget : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit Widget(QWidget *parent = 0);
  13. ~Widget();
  14. void dealMsg(); // 槽函数,处理对方发送过来的数据
  15. private slots:
  16. void on_buttonSend_clicked();
  17. private:
  18. Ui::Widget *ui;
  19. QUdpSocket *udpSoket; //UDP套接字
  20. };
  21. #endif // WIDGET_H

widget.cpp

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QHostAddress>
  4. Widget::Widget(QWidget *parent) :
  5. QWidget(parent),
  6. ui(new Ui::Widget)
  7. {
  8. ui->setupUi(this);
  9. // 分配空间,指定父对象
  10. udpSoket = new QUdpSocket(this);
  11. // 绑定
  12. // udpSoket->bind(8888);
  13. udpSoket->bind(QHostAddress::AnyIPv4, 8888); // 如果用了组播就要用IPV4
  14. // 加入某个组播 组播的地址是D类地址
  15. udpSoket->joinMulticastGroup(QHostAddress("224.0.0.2"));
  16. this->setWindowTitle("服务器端口为:8888");
  17. // 当对方成功发送过来,自动触发readyRead()
  18. connect(udpSoket, &QUdpSocket::readyRead, this, &Widget::dealMsg);
  19. }
  20. Widget::~Widget()
  21. {
  22. delete ui;
  23. }
  24. void Widget::dealMsg()
  25. {
  26. // 读取对方发送过来的内容
  27. char buf[1024] = {0};
  28. // 对方地址
  29. QHostAddress cliAddr;
  30. // 对方端口
  31. quint16 port;
  32. qint64 len = udpSoket->readDatagram(buf, sizeof(buf), &cliAddr, &port);
  33. if(len > 0)
  34. {
  35. // 格式化 [192.68.2.2:8888]aaa
  36. QString str = QString("[%1:%2] %3")
  37. .arg(cliAddr.toString())
  38. .arg(port)
  39. .arg(buf);
  40. // 给编辑区设置内容
  41. ui->textEdit->setText(str);
  42. }
  43. }
  44. // 发送按钮
  45. void Widget::on_buttonSend_clicked()
  46. {
  47. // 先获取对方的IP和端口
  48. QString ip = ui->lineEditIP->text();
  49. qint16 port = ui->lineEditPort->text().toInt();
  50. // 获取编辑器内容
  51. QString str = ui->textEdit->toPlainText();
  52. // 给指定的IP发送数据
  53. udpSoket->writeDatagram(str.toUtf8(), QHostAddress(ip), port);
  54. }

客户端2

image.png

client.h

  1. #ifndef CLIENT_H
  2. #define CLIENT_H
  3. #include <QWidget>
  4. #include <QUdpSocket>
  5. namespace Ui {
  6. class client;
  7. }
  8. class client : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit client(QWidget *parent = 0);
  13. ~client();
  14. void Mess();
  15. private slots:
  16. void on_buttonSend_clicked();
  17. private:
  18. Ui::client *ui;
  19. QUdpSocket * udpSocket;
  20. };
  21. #endif // CLIENT_H

client.cpp

  1. #include "client.h"
  2. #include "ui_client.h"
  3. #include <QHostAddress>
  4. client::client(QWidget *parent) :
  5. QWidget(parent),
  6. ui(new Ui::client)
  7. {
  8. ui->setupUi(this);
  9. this->setWindowTitle("端口号9991");
  10. udpSocket = new QUdpSocket(this);
  11. udpSocket->bind(9991);
  12. connect(udpSocket, &QUdpSocket::readyRead, this, &client::Mess);
  13. }
  14. client::~client()
  15. {
  16. delete ui;
  17. }
  18. void client::Mess()
  19. {
  20. char buf[1024] = {0};
  21. QHostAddress addre;
  22. quint16 port;
  23. qint64 leng = udpSocket->readDatagram(buf, sizeof(buf), &addre, &port);
  24. if(leng>0) {
  25. QString str = QString("[%1:%2] %3")
  26. .arg(addre.toString())
  27. .arg(port)
  28. .arg(buf);
  29. ui->textEdit->setText(str);
  30. }
  31. }
  32. void client::on_buttonSend_clicked()
  33. {
  34. QString ip = ui->lineEditIP->text();
  35. qint16 port = ui->lineEditPort->text().toInt();
  36. QString text = ui->textEdit->toPlainText();
  37. // 给指定的IP发送数据
  38. udpSocket->writeDatagram(text.toUtf8(), QHostAddress(ip), port);
  39. }

image.png