资源简介

用Qt写的基于tcp协议的聊天室,含客户端和服务器,支持发信息,支持一方发文件,另一方收文件。

资源截图

代码片段和文件信息

#include “clientwidget.h“
#include “ui_clientwidget.h“
#include
#include
#include

ClientWidget::ClientWidget(QWidget *parent) :
    QWidget(parent)
    ui(new Ui::ClientWidget)
{
    ui->setupUi(this);
    this->setWindowtitle(clientName);
    ui->lineEditIP->setText(“127.0.0.1“);
    ui->lineEditPort->setText(“8888“);
    tcpSocket = NULL;
    tcpSocket = new QTcpSocket(this);

    connect(tcpSocket&QTcpSocket::connected
            [=](){
        ui->textEditRead->setText(“已经连接到服务器端“);
    }
            );
    isStart = true;
    connect(tcpSocket&QTcpSocket::readyRead
            [=](){

        //接受信息
//        QByteArray readStr = tcpSocket->readAll();
//        ui->textEditRead->append(serverName+readStr);
        QByteArray readStr = tcpSocket->readAll();
        if(true == isStart){
            //接收头
            isStart = false;
            //解析头部信息 buf = “..##..“
            //初始化
            fileName = QString(readStr).section(“##“00);
            fileSize = QString(readStr).section(“##“11).toInt();
            recivedSize = 0;
            qDebug()<<“头部“;
            QMessageBox::information(this“文件““文件传输:“+fileName+“ “+fileSize/1024);
        }else{ // 文件信息
            //打开文件
            file = new QFile(fileName);
            bool isOk = file->open(QIODevice::WriteOnly);

            if(false == isOk){
                qDebug()<<“writeOnly error“;

            }else{
                qint64 len = file->write(readStr);
                recivedSize+=len;
                if(recivedSize == fileSize){
                    file->close();
                    QMessageBox::information(this“完成““文件接收完成“);

                    tcpSocket->disconnectFromHost();
                    tcpSocket->close();
                }
            }
        }
    }

            );
}

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

void ClientWidget::on_buttonConnect_clicked()
{
    QString IP = ui->lineEditIP->text();
    quint16 port = ui->lineEditPort->text().toInt();
    tcpSocket->connectToHost(QHostAddress(IP)port);

}

void ClientWidget::on_buttonSend_clicked()
{
    if(NULL == tcpSocket){
        return;
    }
    QString str = ui->textEditWrite->toPlainText();
     if(NULL != str){
    tcpSocket->write(str.toUtf8().data());
    ui->textEditRead->append(clientName+str);
     }
    ui->textEditWrite->clear();
}

void ClientWidget::on_buttonClose_clicked()
{
    if(NULL == tcpSocket){
        return;
    }
    tcpSocket->disconnectFromHost();
    tcpSocket->close();
    tcpSocket = NULL;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-08-14 10:19  TCP\
     文件        2773  2018-08-03 14:47  TCP\clientwidget.cpp
     文件         781  2018-08-03 11:36  TCP\clientwidget.h
     文件        3103  2018-08-03 10:58  TCP\clientwidget.ui
     文件         249  2018-08-02 17:27  TCP\main.cpp
     文件        4837  2018-08-03 14:20  TCP\serverwidget.cpp
     文件        1008  2018-08-03 10:46  TCP\serverwidget.h
     文件        2222  2018-08-03 09:15  TCP\serverwidget.ui
     文件        1157  2018-08-02 17:07  TCP\TCP.pro
     文件       23826  2018-08-03 14:52  TCP\TCP.pro.user

评论

共有 条评论