三年多以前刚学习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 程序界面
5. 主要函数
5.1 获取本机IP地址
QString FeiQ_3::getIP() //获取ip地址{QList<QHostAddress> 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( "<font color=blue>[自已]</font>" + sm.at(3) + ":<br>" + sm.at(2));if(sm.at(1) == ip){bbb->chuanMessage("<font color=red>[对方]</font>" + sm.at(3) + ":<br>" + 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("<font color=red>[对方]</font>" + sm.at(3) +":<br>" + 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"<<fs;files.clear();files.append(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; //receivewhile (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);}}
源码下载:/download/liukang325/8876633