Qt DAY5 Qt制作简易网络聊天室

服务器

widget.h文件
#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include //向量,函数类模板
#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_openBtn_clicked();

    void newconnection_slot();
    void readyRead_slot();

private:
    Ui::Widget *ui;
    QTcpServer *server;
    QVector clientVector;//客户端容器
};

#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_openBtn_clicked()
{
    quint16 port=ui->portEdit->text().toUInt();

    if(server->listen(QHostAddress::Any,port))
    {
        QMessageBox::information(this,"成功","打开服务器成功");
    }
    else
    {
        QMessageBox::information(this,"失败","打开服务器失败");
        return;
    }

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

void Widget::newconnection_slot()
{
    QTcpSocket *s = server->nextPendingConnection();
    clientVector.push_back(s);
    connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
}

void Widget::readyRead_slot()
{
    //删除无效的客户端
    for (int i=0;istate()==0)
        {
            clientVector.removeAt(i);
        }
    }
    //
    for (int i=0;ibytesAvailable()!=0)
        {
            QByteArray msg= clientVector[i]->readAll();
            ui->msgWidget->addItem(QString::fromLocal8Bit(msg));

            for(int j=0;jwrite(msg);
            }
        }
    }

}

客户端

widget.h文件
#ifndef WIDGET_H
#define WIDGET_H

#include 
#include  //客户端类
#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_connnectBtn_clicked();
    void connected_slot();
    void readyRead_slot();
    void disconnected_slot();

    void on_sendBtn_clicked();

private:
    Ui::Widget *ui;

    QTcpSocket *socket;
    QString userName;
};

#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);
    socket=new QTcpSocket(this);
    connect(socket,&QTcpSocket::connected,this,&Widget::connected_slot);
    connect(socket,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
    connect(socket,&QTcpSocket::disconnected,this,&Widget::disconnected_slot);
}


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

void Widget::on_connnectBtn_clicked()
{

    QString ip=ui->ipEdit->text();
    quint16 port=ui->portEdit->text().toUInt();
    userName=ui->userEdit->text();

    if(ui->connnectBtn->text()=="连接服务器")
    {
        socket->connectToHost(ip,port);

        ui->connnectBtn->setText("服务器断开");
    }
    else
    {

        QString msg=userName+":离开聊天室";
        socket->write(msg.toLocal8Bit());

        socket->disconnectFromHost();

        ui->connnectBtn->setText("连接服务器");

    }
}

void Widget::connected_slot()
{
    QMessageBox::information(this,"成功","连接服务器成功");
    QString msg=userName+":进入聊天室";
    socket->write(msg.toLocal8Bit());
}

void Widget::readyRead_slot()
{
    if(socket->bytesAvailable()!=0)
    {
        QString msg=QString::fromLocal8Bit(socket->readAll());
        ui->msgWidget->addItem(msg);
    }

}

void Widget::disconnected_slot()
{
    QMessageBox::information(this,"信息","服务器已断开");
}

void Widget::on_sendBtn_clicked()
{

    QString msg=userName+":"+ui->msgEdit->text();
    socket->write(msg.toLocal8Bit());
    QMessageBox::information(this,"成功","信息发送成功");
}

Qt DAY5 Qt制作简易网络聊天室_第1张图片

Qt DAY5 Qt制作简易网络聊天室_第2张图片 

 

你可能感兴趣的:(qt,开发语言)