QTday5

作业

QTTCP服务器

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 

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_btn_clicked();
    void connect_slot();
    void ready_Read_slot();

private:
    Ui::Widget *ui;
    QTcpServer *ser;
    QList cli;
};
#endif // WIDGET_H

 源代码

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ser = new QTcpServer(this);

}

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


void Widget::on_btn_clicked()
{
    quint16 port = ui->lab->text().toUInt();
    if(!ser->listen(QHostAddress::Any,port))
    {
        QMessageBox::information(this,"提示","服务器监听失败");
        return;
    }else
    {
        QMessageBox::information(this,"提示","服务器成功监听");
    }
    connect(ser,&QTcpServer::newConnection,this,&Widget::connect_slot);
}

void Widget::connect_slot()
{
    QTcpSocket *c = ser->nextPendingConnection();
    cli.push_back(c);
    connect(c,&QTcpSocket::readyRead,this,&Widget::ready_Read_slot);

}

void Widget::ready_Read_slot()
{
    for(int i = 0 ; i < cli.count() ; i++)
    {
        if(cli[i]->state() == 0)
        {
            cli.removeAt(i);
        }
    }
    for(int i = 0 ; i < cli.count() ; i++)
    {
        if(cli[i]->bytesAvailable() != 0)
        {
            QByteArray msg = cli[i]->readAll();
            ui->listWidget->addItem(QString::fromLocal8Bit(msg));
            for(int j = 0 ; j < cli.count() ; j++)
            {
                cli[j]->write(msg);
            }
        }
    }
}

 客户端

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 

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_connect_btn_clicked();
    void connect_Host_slot();
    void readyRead_slot();

    void on_discon_btn_clicked();

    void on_send_btn_clicked();

    void disconnect_slot();

private:
    Ui::Widget *ui;
    QTcpSocket *cli;
    QString username_ui;

};
#endif // WIDGET_H

 源代码

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    cli = new QTcpSocket(this);
    connect(cli,&QTcpSocket::connected,this,&Widget::connect_Host_slot);
    connect(cli,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
    connect(cli,&QTcpSocket::disconnected,this,&Widget::disconnect_slot);
}

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


void Widget::on_connect_btn_clicked()
{
    username_ui = ui->usr_name_L->text();
    QString IP_ui = ui->IP_L->text();
    quint16 PORT_ui = ui->PORT_L->text().toUInt();

    cli->connectToHost(IP_ui,PORT_ui);
}

void Widget::connect_Host_slot()
{
    QMessageBox::information(this,"提示","连接服务器成功");
    QString msg = username_ui + " 进入聊天室";
    cli->write(msg.toLocal8Bit());
}
void Widget::readyRead_slot()
{
    QByteArray msg = cli->readAll();
    ui->listWidget->addItem(QString::fromLocal8Bit(msg));
}

void Widget::on_discon_btn_clicked()
{
    QString msg = username_ui + " 离开聊天室";
    cli->write(msg.toLocal8Bit());
    cli->disconnectFromHost();
}

void Widget::on_send_btn_clicked()
{
    QString m = ui->msg->text();
    QString msg = username_ui + ":" + m;
    cli->write(msg.toLocal8Bit());
    ui->msg->clear();
}

void Widget::disconnect_slot()
{
    QMessageBox::information(this,"提示","断开成功");
}

QTday5_第1张图片 

你可能感兴趣的:(笔记)