当前位置:   article > 正文

qt 下UDP消息的接收和发送和TCP发送文件_qt中udp聊天室可以发送文件

qt中udp聊天室可以发送文件
UDP发送消息实现代码
  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QLabel>
  5. #include <QPushButton>
  6. #include <QLineEdit>
  7. #include <QUdpSocket>
  8. class Widget : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. Widget(QWidget *parent = 0);
  13. ~Widget();
  14. private:
  15. QLabel *label1, *label2, *label3, *label4;
  16. QLineEdit *edit1, *edit2, *edit3, *edit4;
  17. QPushButton *btn1, *btn2;
  18. QUdpSocket *udpsocket;
  19. private slots:
  20. void on_clicked1();
  21. void on_clicked2();
  22. void dataReceived();
  23. };
  24. #endif // WIDGET_H


  1. #include "widget.h"
  2. #include <QHBoxLayout>
  3. #include <QMessageBox>
  4. #include <QList>
  5. Widget::Widget(QWidget *parent)
  6. : QWidget(parent)
  7. {
  8. udpsocket = new QUdpSocket(this);
  9. setWindowTitle(tr("udp聊天"));
  10. label1 = new QLabel;
  11. label1->setText("请输入bind端口号");
  12. label2 = new QLabel;
  13. label2->setText("请输入目的IP地址");
  14. label3 = new QLabel;
  15. label3->setText("请输入目的端口号");
  16. label4 = new QLabel;
  17. label4->setText("请输入发送的消息");
  18. edit1 = new QLineEdit;
  19. edit2 = new QLineEdit;
  20. edit3 = new QLineEdit;
  21. edit4 = new QLineEdit;
  22. btn1 = new QPushButton;
  23. btn1->setText(tr("bind"));
  24. btn2 = new QPushButton;
  25. btn2->setText(tr("发送"));
  26. QGridLayout *layout1 = new QGridLayout(this);
  27. layout1->addWidget(label1, 0, 0);
  28. layout1->addWidget(edit1, 0, 1);
  29. layout1->addWidget(btn1, 1, 0);
  30. layout1->addWidget(label2, 2, 0);
  31. layout1->addWidget(edit2, 2, 1);
  32. layout1->addWidget(label3, 3, 0);
  33. layout1->addWidget(edit3, 3, 1);
  34. layout1->addWidget(label4, 4, 0);
  35. layout1->addWidget(edit4, 4, 1);
  36. layout1->addWidget(btn2, 5, 0);
  37. connect(btn1, SIGNAL(clicked()), this, SLOT(on_clicked1()));
  38. connect(btn2, SIGNAL(clicked()), this, SLOT(on_clicked2()));
  39. connect(udpsocket, SIGNAL(readyRead()), this, SLOT(dataReceived()));
  40. }
  41. Widget::~Widget()
  42. {
  43. }
  44. void Widget::on_clicked1()
  45. {
  46. udpsocket->close();
  47. udpsocket->bind(edit1->text().toInt());
  48. }
  49. void Widget::on_clicked2()
  50. {
  51. QHostAddress addr;
  52. addr.setAddress(edit2->text());
  53. int port = edit3->text().toInt();
  54. udpsocket->writeDatagram(edit4->text().toUtf8(), addr, port);
  55. }
  56. void Widget::dataReceived()
  57. {
  58. QHostAddress addr;
  59. char buf[1024];
  60. while (udpsocket->hasPendingDatagrams())
  61. {
  62. memset(buf, 0, sizeof(buf));
  63. udpsocket->readDatagram(buf, sizeof(buf), &addr);
  64. QMessageBox::information(this, tr("消息"), tr("来自") + addr.toString() + tr("的消息:\n") + buf);
  65. }
  66. }

TCP发送文件实现代码,
客户端代码
  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include <QDialog>
  4. #include <QLabel>
  5. #include <QPushButton>
  6. #include <QLineEdit>
  7. #include <QTcpServer>
  8. class Dialog : public QDialog
  9. {
  10. Q_OBJECT
  11. public:
  12. Dialog(QWidget *parent = 0);
  13. ~Dialog();
  14. private:
  15. QLabel *label1, *label2;
  16. QLineEdit *edit1, *edit2;
  17. QPushButton *btn1;
  18. QTcpSocket *tcpsocket;
  19. QString filename;
  20. private slots:
  21. void on_clicked1();
  22. void dataReceived();
  23. };
  24. #endif // DIALOG_H

  1. #include "dialog.h"
  2. #include <QGridLayout>
  3. #include <QFileDialog>
  4. #include <QTcpSocket>
  5. #define BUFSIZE 262144 // 256K
  6. Dialog::Dialog(QWidget *parent)
  7. : QDialog(parent)
  8. {
  9. tcpsocket = new QTcpSocket(this);
  10. setWindowTitle(tr("tcp文件发送客户端"));
  11. label1 = new QLabel;
  12. label1->setText(tr("请输入目的IP地址"));
  13. label2 = new QLabel;
  14. label2->setText(tr("请输入目的端口号"));
  15. edit1 = new QLineEdit;
  16. edit2 = new QLineEdit;
  17. btn1 = new QPushButton;
  18. btn1->setText(tr("发送文件"));
  19. QGridLayout *layout = new QGridLayout(this);
  20. layout->addWidget(label1, 0, 0);
  21. layout->addWidget(edit1, 0, 1);
  22. layout->addWidget(label2, 1, 0);
  23. layout->addWidget(edit2, 1, 1);
  24. layout->addWidget(btn1, 2, 0);
  25. connect(btn1, SIGNAL(clicked()), this, SLOT(on_clicked1()));
  26. connect(tcpsocket, SIGNAL(readyRead()), this, SLOT(dataReceived()));
  27. }
  28. Dialog::~Dialog()
  29. {
  30. }
  31. void getfilename(const char *filename, char *name)
  32. {
  33. int len = strlen(filename);
  34. int i;
  35. for (i = (len - 1); i >= 0; i--)
  36. {
  37. if ((filename[i] == '\\') || (filename[i] == '/'))
  38. {
  39. break;
  40. }
  41. }
  42. strcpy(name, &filename[i + 1]);
  43. }
  44. void Dialog::on_clicked1()
  45. {
  46. filename = QFileDialog::getOpenFileName(this, tr("打开文件"));
  47. if (!filename.isEmpty())
  48. {
  49. tcpsocket->close();
  50. QHostAddress serverIP;
  51. serverIP.setAddress(edit1->text());
  52. tcpsocket->connectToHost(serverIP, edit2->text().toInt());
  53. char buf[1024];
  54. memset(buf, 0, sizeof(buf));
  55. getfilename(filename.toStdString().data(), buf);
  56. tcpsocket->write(buf, strlen(buf));
  57. }
  58. }
  59. void Dialog::dataReceived()
  60. {
  61. char *buf = (char *)malloc(BUFSIZE);
  62. while (tcpsocket->bytesAvailable() > 0)
  63. {
  64. memset(buf, 0, BUFSIZE);
  65. tcpsocket->read(buf, BUFSIZE);
  66. }
  67. if (strncmp(buf, "OK", 2) == 0)// 服务端回复统一接受文件
  68. {
  69. FILE *fd = fopen(filename.toStdString().data(), "rb");// 只读方式打开
  70. if (fd)
  71. {
  72. while (1)
  73. {
  74. memset(buf, 0, BUFSIZE);
  75. size_t rc = fread(buf, 1, BUFSIZE, fd);//循环读取文件,知道读到文件尾
  76. if (rc <= 0)
  77. {
  78. break;
  79. }
  80. else
  81. {
  82. tcpsocket->write(buf, rc);
  83. if (rc <= 0)
  84. {
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. fclose(fd);
  91. }
  92. tcpsocket->close();
  93. free(buf);
  94. }

服务器代码参看前边的TCP文件传送博文

                
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/305208
推荐阅读
相关标签
  

闽ICP备14008679号