QT实现局域网聊天工具(带UDP文件传输)

三年多以前刚学习QT写的一个局域网聊天工具小项目。

由于是初学QT时写的,代码比较简略,也没时间好好整理项目,仅供大家参考相关TCP以及UDP的连接与传输功能   以及相关控件的基本使用方法。


1. 客户端程序函数说明
   

       //连接服务器:
        tcpSocket = new QTcpSocket(this);
        tcpSocket->connectToHost(serverIP,8000);


      //断开服务器:
        tcpSocket->disconnectFromHost();



2. 服务器端程序说明

            if(tcpServer->listen(QHostAddress::Any, i)){
              ui->textEdit->append("listen OK!");
             }else{
              ui->textEdit->append("listen error!");
             }



3.  文件传输模块
     //发送端:
	udpSocket = new QUdpSocket(this);
   	udpSocket->bind(QHostAddress::Any, 7758);
    	connect(udpSocket, SIGNAL(readyRead()), this,SLOT(readPendingDatagrams()));


    //接收端:
	connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
    	file.setFileName( filee );
    	if (!file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Unbuffered))
       	   return;



4 程序界面

QT实现局域网聊天工具(带UDP文件传输)_第1张图片


QT实现局域网聊天工具(带UDP文件传输)_第2张图片


QT实现局域网聊天工具(带UDP文件传输)_第3张图片


QT实现局域网聊天工具(带UDP文件传输)_第4张图片


5. 主要函数


5.1 获取本机IP地址
  QString FeiQ_3::getIP()  //获取ip地址
{
    QList list =  QnetworkInterface::allAddresses();
    foreach (QHostAddress address, list){
       if(address.protocol() == QAbstractSocket::IPv4Protocol){
           if (address.toString().contains("127.0.")){
               continue;}
           return address.toString();
       }
    }
    return 0;
}

5.2 获取在线用户列表
void FeiQ_3::on_pushButton_in_clicked() //连接服务器按键
{
    QString serverIP = ui->lineEdit_server->text();
    connectToServer(serverIP);
    ipname.append("%");
    ipname.append(ip);
    ipname.append("%");
    ipname.append(ui->lineEdit_name->text());
    tcpSocket->write(ipname);
    connect(tcpSocket,SIGNAL(readyRead()),this, SLOT(readMessage()));
}
if(tmp.contains("   @") ){
        ui->listWidget->clear();
        tm = tmp.split("   @");
        for(int i = 0; i < tm.count(); i ++){
                ui->listWidget->addItem(tm.at(i));
        }
}


5.3 私聊模块
else if(tmp.startsWith("&&")){
        sm = tmp.split("&&");
        if(sm.at(3) == ip ){
            bbb->chuanMessage( "[自已]" + sm.at(3) + ":
" + sm.at(2)); if(sm.at(1) == ip){ bbb->chuanMessage("[对方]" + sm.at(3) + ":
" + sm.at(2)); } } if(sm.at(1) == ip ){ if(sm.at(3) != ip){ bbb->show(); bbb->chuanIP(sm.at(3),ui->lineEdit_server->text()); bbb->chuanMessage("[对方]" + sm.at(3) +":
" + sm.at(2)); } } }


5.4 选择文件功能
fDialog = new QFileDialog(this);
    fDialog->setFileMode(QFileDialog::ExistingFiles);
    connect(fDialog,SIGNAL(fileSelected ( const QString & )),this,SLOT(fs(const QString & )));
    fDialog->hide();

void Dialog::fs(const QString & fs){
    //选中文件点击open后会出发该信号 至在打开单一文件时出发
    qDebug() <<"fs"<

5.5 文件传输发送端
void Dialog::sendData()
{
    if (!file.atEnd()) {
        QByteArray line = file.read(8000);
        udpSocket->writeDatagram(line,QHostAddress(ipRec),7755);
        i++;
        qDebug()<< "send over!"<< i << line.size();
        if(line.size() <8000){
            QByteArray yes;
            yes.append("*^*文件接收完成!");
            tcpSocket->write(yes);
            QMessageBox::warning(this,tr("通知"),tr("发送成功!"),QMessageBox::Yes);
        }
    }
}
void Dialog::readPendingDatagrams()
{
    while (udpSocket->hasPendingDatagrams()) {
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;
        udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
        qDebug() << datagram;
        if(datagram == "1"){
            qDebug() << "send OK!";
            sendData();
        }
    }
}


5.6 文件传输接收端
void DialogRec::initSocket()
{
    connect(udpSocket, SIGNAL(readyRead()),
            this, SLOT(readPendingDatagrams()));
    file.setFileName( filee );
    if (!file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Unbuffered))
        return;
    filee.clear();
}
void DialogRec::readPendingDatagrams()
{
    int i = 0;        //receive
    while (udpSocket->hasPendingDatagrams()) {
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;
        udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
        file.write(datagram.data(),datagram.size());
        i++;
        udpSocket->writeDatagram("1",1, sender ,7758);
        qDebug() << i << datagram.size();
    }
}

5.7 窗口美化(在私聊窗口下加入“春”、“夏”、“秋”、“冬”四种皮肤。
void Dialog::on_comboBox_currentIndexChanged(int index)
{
    if(index==0){
        map.load("./icon/1.jpg");
        map = map.scaled(QSize(390,330));
        ui->label_back->setPixmap(map);}
    if(index==1){
        map.load("./icon/2.jpg");
        map = map.scaled(QSize(390,330));
        ui->label_back->setPixmap(map);}
    if(index==2){
        map.load("./icon/3.jpg");
        map = map.scaled(QSize(390,330));
        ui->label_back->setPixmap(map);}
    if(index==3){
        map.load("./icon/4.jpg");
        map = map.scaled(QSize(390,330));
        ui->label_back->setPixmap(map);}
}

源码下载: http://download.csdn.net/download/liukang325/8876633

你可能感兴趣的:(QT,聊天工具,局域网,QT,C++)