0112qt

发布时间:2024年01月13日

?.pro:

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h

FORMS += \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

?widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpServer>//服务器类
#include <QMessageBox>//消息对话框类
#include <QTcpSocket>//客户端类

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();


private slots:

    void on_startbtn_clicked();
public slots:
    void newConnection_slot();//newConnection()函数对应的槽函数声明
    void readyread_slot();//readyread_slot()函数对应的槽函数声明

private:
    Ui::Widget *ui;

    QTcpServer *server;//定义一个服务器指针
    QList<QTcpSocket*> socketList;//定义一个存放服务器套接字的容器

};
#endif // WIDGET_H

?widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //给服务器指针实例化空间
    server=new QTcpServer (this);


}

Widget::~Widget()
{
    delete ui;
}


//启动服务器按钮对应的槽函数处理
void Widget::on_startbtn_clicked()
{
    quint16 port=ui->portEdit->text().toUInt();//toUInt的返回值是uint 而listen()函数需要的port是quint16,所以定义一个quint16类型的port来接
    //将服务器设置成监听状态
    //listen()函数的原型:
    //bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
    if(server->listen(QHostAddress::Any,port))
    {
        //到这里监听成功
        //设置提示消息
        QMessageBox::information( this,"提示", "启动服务器成功!");
  }
    else
    {
        //监听失败
        QMessageBox::information( this,"提示", "启动服务器失败!");
    }
    //到这里就能确定有无客户端连接了,有连接的话
    //将服务器自动发送的newConnection()函数与自定义的槽函数连接
    connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
}

//newConnection()函数与自定义的槽函数的实现
void  Widget::newConnection_slot()
{
    //获取最新的客户端套接字
    QTcpSocket *s=server->nextPendingConnection();
    //将客户端套接字放到容器中
    socketList.push_back(s);
    //客户端自动发送一个readyRead()信号
    connect(s,&QTcpSocket::readyRead,this,&Widget::readyread_slot);

}
//readyread()函数与自定义的槽函数的实现
void Widget::readyread_slot()
{
    //循环,刚上线立刻下线的客户端的套接字也会被保存一份,发送时不需要发给这些客户端,移除无效的客户端
    for(int i=0;i<socketList.count();i++)
    {
        //判断是和否符合移除条件
        if(socketList.at(i)->state()==0)
        {
            socketList.removeAt(i);
        }
    }
    //剩下的就是可以发送消息的客户端
    for(int i=0;i<socketList.count();i++)
    {
        //如果不为空,就是此客户端发来了消息
        if(socketList.at(i)->bytesAvailable()!=0)
        {
            //读取客户端发来的消息
           QByteArray msg= socketList.at(i)->readAll();
           //将信息放入ui界面上
           ui->msgWidget->addItem(QString::fromLocal8Bit(msg));
           //将信息发送给所有客户端
           for(int j=0;j<socketList.count();j++)
           {
               socketList.at(j)->write(msg);
           }

        }
    }
}

?

?

?

0112qt - 幕布

文章来源:https://blog.csdn.net/weixin_57731002/article/details/135565547
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。