QT自制TCP服务器

打印 上一主题 下一主题

主题 554|帖子 554|积分 1662

TCP服务器

TCP服务器实现对TCP客户端的监听,实现对数据的发送与接收。

该TCP服务器入门级别,后面可以继续丰富。
界面

上图就是实现的界面,后面可以自己去丰富一下,上面的按钮,接收数据框,发送数据框,端口号输入框,文字显示等使用的控件为:

  • 文字显示(除去按钮,按钮可以直接写)

  • 端口号输入框
    和后面发送框一样的控件

  • 按钮

  • 发送框

  • 接收框

注意:此控件设置为只读模式,因为不需要我们输入。
逻辑代码

.pro文件

  1. QT       += core gui network//这个地方需要加上这个就可以了
  2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  3. CONFIG += c++11
  4. # The following define makes your compiler emit warnings if you use
  5. # any Qt feature that has been marked deprecated (the exact warnings
  6. # depend on your compiler). Please consult the documentation of the
  7. # deprecated API in order to know how to port your code away from it.
  8. DEFINES += QT_DEPRECATED_WARNINGS
  9. # You can also make your code fail to compile if it uses deprecated APIs.
  10. # In order to do so, uncomment the following line.
  11. # You can also select to disable deprecated APIs only up to a certain version of Qt.
  12. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
  13. SOURCES += \
  14.     main.cpp \
  15.     widget.cpp
  16. HEADERS += \
  17.     widget.h
  18. FORMS += \
  19.     widget.ui
  20. # Default rules for deployment.
  21. qnx: target.path = /tmp/$${TARGET}/bin
  22. else: unix:!android: target.path = /opt/$${TARGET}/bin
  23. !isEmpty(target.path): INSTALLS += target
复制代码
.h文件

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QTcpServer>
  5. #include <QTcpSocket>
  6. #include <QHostAddress>
  7. #include <QtNetwork>//需要的头文件
  8. QT_BEGIN_NAMESPACE
  9. namespace Ui { class Widget; }
  10. QT_END_NAMESPACE
  11. class Widget : public QWidget
  12. {
  13.     Q_OBJECT
  14. public:
  15.     Widget(QWidget *parent = nullptr);
  16.     ~Widget();
  17.     QTcpServer *tcpServer;
  18.     QTcpSocket *tcpSocket;
  19. private slots:
  20.     void newConnection_Slot();
  21.     void readyRead_Slot();
  22.     void disconnected_Slot();
  23.     void on_openbt_clicked();
  24.     void on_closeBt_clicked();
  25.     void on_sendBT_clicked();
  26. private:
  27.     Ui::Widget *ui;
  28. };
  29. #endif // WIDGET_H
复制代码
  此处添加头文件,声明槽函数。
  .cpp文件

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. Widget::Widget(QWidget *parent)
  4.     : QWidget(parent)
  5.     , ui(new Ui::Widget)
  6. {
  7.     ui->setupUi(this);
  8.     tcpServer = new QTcpServer(this);
  9.     tcpSocket = new QTcpSocket(this);
  10. //    tcpServer->listen(QHostAddress::LocalHost, ui->duankouEdit_2->text().toUInt());
  11. //    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection_Slot()));
  12. }
  13. void Widget::newConnection_Slot()
  14. {
  15.     static int i = 0;
  16.     tcpSocket = tcpServer->nextPendingConnection();//创建socket连接
  17.     connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readyRead_Slot()));//创建读数据凹槽
  18.     connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected_Slot()));//创建服务端失联凹槽
  19.     i++;
  20. }
  21. void Widget::readyRead_Slot()
  22. {
  23.     QString buf;
  24.     buf = tcpSocket->readAll();
  25.     ui->receiveEdit->appendPlainText(buf);
  26. }
  27. void Widget::disconnected_Slot()
  28. {
  29. }
  30. Widget::~Widget()
  31. {
  32.     delete ui;
  33. }
  34. void Widget::on_openbt_clicked()
  35. {
  36.     tcpServer->listen(QHostAddress::Any, ui->duankouEdit_2->text().toUInt());
  37.     connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection_Slot()));
  38.     QString localhostName = QHostInfo::localHostName();
  39.     QHostInfo info = QHostInfo::fromName(localhostName);
  40.     ui->receiveEdit->appendPlainText(localhostName);
  41.     ui->receiveEdit->appendPlainText(info.addresses().back().toString());//first:ipv6地址,back:ipv4地址
  42.     foreach(QHostAddress address,info.addresses())//寻找ipv4地址
  43.     {
  44.          if(address.protocol() == QAbstractSocket::IPv4Protocol)
  45.             ui->receiveEdit->appendPlainText(address.toString());
  46.     }
  47.     quint16 port = tcpServer->serverPort();
  48.     QString st = QString::number(port);
  49.     ui->receiveEdit->appendPlainText(st);
  50. }
  51. void Widget::on_closeBt_clicked()
  52. {
  53.     tcpServer->close();
  54.     tcpSocket->close();
  55.     disconnect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection_Slot()));
  56. }
  57. void Widget::on_sendBT_clicked()
  58. {
  59.      tcpSocket->write(ui->sendEdit->text().toLocal8Bit().data());
  60. }
复制代码
打开服务器获取主机IP地址的两种方法:
  1.         QHostInfo info = QHostInfo::fromName(localhostName);
  2.     ui->receiveEdit->appendPlainText(localhostName);
  3.     ui->receiveEdit->appendPlainText(info.addresses().back().toString());
  4.     //first:ipv6地址,back:ipv4地址
  5.     foreach(QHostAddress address,info.addresses())//寻找ipv4地址
  6.     {
  7.          if(address.protocol() == QAbstractSocket::IPv4Protocol)
  8.             ui->receiveEdit->appendPlainText(address.toString());
  9.     }
  10.    
复制代码
项目展示

tcp服务器:

tcp客户端:
打开服务器的时候,服务器端会自动输出当前服务器的IP地址和端口号。
然后将IP地址和端口号输入到客户端里面,打开客户端连接就可以了。
实现了服务器与客户端的通信。
源码资源下载

https://download.csdn.net/download/qq_30255657/85675636

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

莫张周刘王

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表