标签:
三年多以前刚学习QT写的一个局域网聊天工具小项目。
由于是初学QT时写的,代码比较简略,也没时间好好整理项目,仅供大家参考相关TCP以及UDP的连接与传输功能 以及相关控件的基本使用方法。
需要源码学习的留下邮箱~
1. 客户端程序函数说明
//连接服务器: tcpSocket = new QTcpSocket(this); tcpSocket->connectToHost(serverIP,8000); //断开服务器: tcpSocket->disconnectFromHost();
if(tcpServer->listen(QHostAddress::Any, i)){ ui->textEdit->append("listen OK!"); }else{ ui->textEdit->append("listen error!"); }
//发送端: 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;
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; }
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)); } }
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)); } } }
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); }
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(); } } }
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(); } }
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://blog.csdn.net/liukang325/article/details/45483029