QT DAY4

1.思维导图

 2.手动完成服务器的实现,并具体程序要注释清楚

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMessageBox>
#include <QList>
#include <QDebug>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT
    QPushButton *btn1 = new QPushButton;
    QLabel *lab = new QLabel;
    QLabel *lab2 = new QLabel;
    QLineEdit *edit = new QLineEdit;
    QListWidget *list=new QListWidget;

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

public slots:
    void btn1_clicked();

    void newConnection_slot();  //自定义处理newConnection信号的槽函数

    void readyRead_slot();  //自定义处理readyRead信号的槽函数


private:
    Ui::Widget *ui;

    //定义客户端容器
    QList<QTcpSocket*> socketList;

    //定义服务器指针
    QTcpServer *server;




};
#endif // WIDGET_H

源文件

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

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

    qDebug()<<this->size();
    qDebug()<<this->rect().size();
    qDebug()<<this->geometry().size();
    qDebug()<<this->frameGeometry().size();
    qDebug()<<"width:"<<this->width()<<"  height:"<<this->height();
    qDebug()<<"width:"<<this->size().width()<<"    height:"<<this->size().height();

    //设定窗口指定大小
    this->setFixedSize(500,500);
    //窗口标题
    qDebug()<<this->windowTitle();  //获取窗口标题
    this->setWindowTitle("服务器");

    //设置标签
    lab=new QLabel(this);
    lab->resize(500,400);
    lab->setAlignment(Qt::AlignCenter);  //垂直和水平全部居中

    //设置EditList窗口,显示运行结果
    list=new QListWidget(this);
    list->resize(500,300);

    //端口号标签
    lab2=new QLabel(this);
    lab2->resize(100,50);
    lab2->setText("端口号:");
    lab2->move(90,350);

    //设定端口号
    edit=new QLineEdit(this);
    edit->resize(200,50);
    edit->move(150,350);

    //设定按钮
    btn1=new QPushButton(this);
    btn1->setText("启动服务端");
    btn1->resize(90,50);
    btn1->move(85,420);
}

Widget::~Widget()
{
    delete ui;
}
//启动服务器按钮对应的槽函数
void Widget::btn1_clicked()
{
    //获取端口号
    quint16 port=edit->text().toUInt();

    if(server->listen(QHostAddress::Any,port))
    {
        QMessageBox::information(this,"","服务器启动成功");

    }else
    {
        QMessageBox::information(this,"","服务器启动失败");
    }

    connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
}

//处理newConnection信号的槽函数的实现
void Widget::newConnection_slot()
{
    qDebug()<<"有新客户连接";

    QTcpSocket* s=server->nextPendingConnection();


    //将套接字放入到客户端容器中
    socketList.push_back(s);


    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();

            //将数据展示到list窗口中
            list->addItem(QString::fromLocal8Bit(msg));

            //将数据发送给所有客户端
            for(int j=0;j<socketList.count();j++)
            {
                socketList.at(j)->write(msg);
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值