国庆day5

 QT实现TCP服务器客户端搭建的代码 

ser.h

#ifndef SER_H
#define SER_H
 
#include 
#include    
#include    
#include   
#include         
 
QT_BEGIN_NAMESPACE
namespace Ui { class Ser; }
QT_END_NAMESPACE
 
class Ser : public QWidget
{
    Q_OBJECT
 
public:
    Ser(QWidget *parent = nullptr);
    ~Ser();
 
private slots:
    void on_connectbtn_clicked();
 
    void newConnection_slot();  
    void readyRead_slot();      
 
private:
    Ui::Ser *ui;
 
    QTcpServer *server;             
    QList clientList; 
};
#endif // SER_H

ser.cpp

#include "ser.h"
#include "ui_ser.h"
 
Ser::Ser(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Ser)
{
    ui->setupUi(this);
 
    server=new QTcpServer(this);    
 
    connect(server,&QTcpServer::newConnection,this,&Ser::newConnection_slot);
}
 
Ser::~Ser()
{
    delete ui;
}
 

void Ser::on_connectbtn_clicked()
{
    quint16 port=ui->portbtn->text().toUInt();
    if(server->listen(QHostAddress::Any,port)){
        QMessageBox::information(this,"成功","服务器启动成功");
    }
    else{
        QMessageBox::information(this,"失败","服务器启动失败");
        return;
    }
}

void Ser::newConnection_slot(){
    QTcpSocket *s=server->nextPendingConnection(); 
 
    clientList.push_back(s);    
 
    connect(s,&QTcpSocket::readyRead,this,&Ser::readyRead_slot);
}

void Ser::readyRead_slot(){
    for(int i=0;istate()==0){
            clientList.removeAt(i);
        }
    }
 
    for(int i=0;ibytesAvailable()!=0){
            QByteArray msg=clientList[i]->readAll();
            ui->msgwidget->addItem(QString::fromLocal8Bit(msg));
 
            for(int j=0;jwrite(msg);
            }
        }
    }

cli.h

#ifndef CLI_H
#define CLI_H
 
#include 
#include    
#include   
 
QT_BEGIN_NAMESPACE
namespace Ui { class cli; }
QT_END_NAMESPACE
 
class cli : public QWidget
{
    Q_OBJECT
 
public:
    cli(QWidget *parent = nullptr);
    ~cli();
 
private slots:
    void on_connectbtn_clicked();
    void on_sendbtn_clicked();
    void on_disconnectbtn_clicked();
 
    void connected_slot();  
    void readyRead_slot();  
    void disconnected_slot();   
 
 
private:
    Ui::cli *ui;
 
    QTcpSocket *socket; 
 
    QString username;   
};
#endif // CLI_H

cli.cpp


#include "cli.h"
#include "ui_cli.h"
 
cli::cli(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::cli)
{
    ui->setupUi(this);
 
    socket=new QTcpSocket(this);   
 
    ui->disconnectbtn->setEnabled(false);
    ui->sendbtn->setEnabled(false);
    ui->msgedit->setEnabled(false);
 
    connect(socket,&QTcpSocket::connected,this,&cli::connected_slot);
 
    connect(socket,&QTcpSocket::readyRead,this,&cli::readyRead_slot);
 
    connect(socket, &QTcpSocket::disconnected, this, &cli::disconnected_slot);
}
 
cli::~cli()
{
    delete ui;
}

void cli::on_connectbtn_clicked()
{
    username=ui->usernameedit->text();
    QString ip=ui->ipedit->text();
    quint16 port=ui->portedit->text().toUInt();
 
    socket->connectToHost(ip,port);
}

void cli::connected_slot(){
    ui->disconnectbtn->setEnabled(true);
    ui->sendbtn->setEnabled(true);
    ui->msgedit->setEnabled(true);
    ui->usernameedit->setEnabled(false);
    ui->ipedit->setEnabled(false);
    ui->portedit->setEnabled(false);
    ui->connectbtn->setEnabled(false);
    QMessageBox::information(this,"成功","成功进入聊天室");
    QString msg=username+": 进入聊天室";
 
    socket->write(msg.toLocal8Bit());
}

void cli::readyRead_slot(){
    QByteArray msg=socket->readAll();
 
    ui->msgwidget->addItem(QString::fromLocal8Bit(msg));
}

void cli::on_sendbtn_clicked()
{
    QString msg=username+": "+ui->msgedit->text();
 
    socket->write(msg.toLocal8Bit());
 
    ui->msgedit->clear();
}

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

void cli::disconnected_slot(){
    ui->disconnectbtn->setEnabled(false);
    ui->sendbtn->setEnabled(false);
    ui->msgedit->setEnabled(false);
    ui->usernameedit->setEnabled(true);
    ui->ipedit->setEnabled(true);
    ui->portedit->setEnabled(true);
    ui->connectbtn->setEnabled(true);
    QMessageBox::information(this,"退出","退出成功");
}

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