#include "tcp_client.h"
#include "tcp_meta.h"
#include <QMessageBox>
#include <QHostInfo>
#include <QByteArray>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("P2P Client");
setGeometry(250,200,850,500);
ContentListWidget = new QListWidget;
myWidget = new QWidget;
UserNameLabel = new QLabel("用户名:");
UserNameLineEdit = new QLineEdit;
UserPasswdLabel = new QLabel("密码:");
UserPasswdLineEdit = new QLineEdit;
ServerIPLabel = new QLabel("服务器IP:");
ServerIPLineEdit = new QLineEdit("192.168.43.98");
PortLabel = new QLabel("端口:");
PortLineEdit = new QLineEdit;
EnterBtn = new QPushButton("登陆");
SignBtn = new QPushButton("注册");
ShareTableWidget = new QTableWidget;
ShareTableWidget->setColumnCount(6);
QStringList tableHead;
tableHead << "文件名" << "大小" << "文件路径" << "IP" << "端口" << "选择";
ShareTableWidget->setHorizontalHeaderLabels(tableHead);
FlushBtn = new QPushButton("我的共享文件");
DisableBtn = new QPushButton("取消选中项共享");
ShareBtn = new QPushButton("上传我的文件");
ResultTableWidget = new QTableWidget;
ResultTableWidget->setColumnCount(7);
tableHead.clear();
tableHead << "文件名" << "大小" << "文件路径" << "拥有者" << "状态" << "IP" << "选择";
ResultTableWidget->setHorizontalHeaderLabels(tableHead);
SearchBtn = new QPushButton("搜索");
SearchLineEdit = new QLineEdit;
ProgressBar = new QProgressBar;
ProgressBar->setRange(0,100);
DownloadBtn = new QPushButton("下载选中项");
timer = new QTimer();
DirLineEdit = new QLineEdit;
SelectDirBtn = new QPushButton("选择保存位置");
layout1 = new QGridLayout();
layout2 = new QGridLayout();
layout3 = new QGridLayout();
mainLayout = new QGridLayout();
layout1->addWidget(ContentListWidget,0,0,1,2);
layout1->addWidget(UserNameLabel,1,0);
layout1->addWidget(UserNameLineEdit,1,1);
layout1->addWidget(UserPasswdLabel,2,0);
layout1->addWidget(UserPasswdLineEdit,2,1);
layout1->addWidget(ServerIPLabel,3,0);
layout1->addWidget(ServerIPLineEdit,3,1);
layout1->addWidget(PortLabel,4,0);
layout1->addWidget(PortLineEdit,4,1);
layout1->addWidget(EnterBtn,5,0,1,2);
layout1->addWidget(SignBtn,6,0,1,2);
layout2->addWidget(ShareTableWidget,0,0,1,3);
layout2->addWidget(FlushBtn,1,0);
layout2->addWidget(DisableBtn,1,1);
layout2->addWidget(ShareBtn,1,2);
layout3->addWidget(ResultTableWidget,0,0,1,2);
layout3->addWidget(SearchLineEdit,1,0);
layout3->addWidget(SearchBtn,1,1);
layout3->addWidget(ProgressBar,2,0,1,2);
layout3->addWidget(DirLineEdit,3,0);
layout3->addWidget(SelectDirBtn,3,1);
layout3->addWidget(DownloadBtn,4,0,1,2);
mainLayout->addLayout(layout1,0,0,2,2);
mainLayout->addLayout(layout2,0,2,1,1);
mainLayout->addLayout(layout3,1,2,1,1);
myWidget->setLayout(mainLayout);
setCentralWidget(myWidget);
status = false;
port = 8010;
PortLineEdit->setText(QString::number(port));
ServerIP = new QHostAddress;
connect(EnterBtn,SIGNAL(clicked()),this,SLOT(slotEnter()));
connect(SignBtn,SIGNAL(clicked()),this,SLOT(slotSignUp()));
connect(ShareBtn,SIGNAL(clicked()),this,SLOT(slotSendMeta()));
connect(FlushBtn,SIGNAL(clicked()),this,SLOT(slotFlushShare()));
connect(DisableBtn,SIGNAL(clicked()),this,SLOT(slotDeleteShare()));
connect(SearchBtn,SIGNAL(clicked()),this,SLOT(slotSearch()));
connect(SelectDirBtn,SIGNAL(clicked()),this,SLOT(slotSelectDir()));
connect(DownloadBtn,SIGNAL(clicked()),this,SLOT(slotDownload()));
connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
ShareBtn->setEnabled(false);
FlushBtn->setEnabled(false);
DisableBtn->setEnabled(false);
SearchBtn->setEnabled(false);
DownloadBtn->setEnabled(false);
tcpSocket = NULL;
p2pserver = NULL;
}
void MainWindow::timeout(){
if(tasks.count() == 0){
timer->stop();
return;
}
ProgressBar->setValue(tasks.at(0)->progress);
if(tasks.at(0)->progress == 100 || tasks.at(0)->status == false){
if(tasks.at(0)->status == false)
ProgressBar->setValue(0);
delete tasks.at(0);
tasks.removeAt(0);
}
}
void MainWindow::slotEnter(){
if(!status){
QString ip = ServerIPLineEdit->text();
if(!ServerIP->setAddress(ip)){
QMessageBox::information(this,"wrong","server ip error");
return ;
}
if(UserNameLineEdit->text() == ""){
QMessageBox::information(this,"wrong","user name error");
return ;
}
UserName = UserNameLineEdit->text();
tcpSocket = new QTcpSocket(this);
connect(tcpSocket,SIGNAL(connected()),this,SLOT(slotConnected()));
connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(slotDisconnected()));
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(DataReceived()));
tcpSocket->connectToHost(*ServerIP,port);
status = true;
}
else{
int length = 0;
QString msg = UserName + ": leave chat room";
FormatMsg(msg, OTHERMSG);
if((length = tcpSocket->write(msg.toLatin1(),msg.length())) != msg.length()){
return;
}
tcpSocket->disconnectFromHost();
status = false;
delete p2pserver;
}
}
void MainWindow::slotSignUp(){
if(!status){
QString ip = ServerIPLineEdit->text();
if(!ServerIP->setAddress(ip)){
QMessageBox::information(this,"wrong","server ip error");
return ;
}
if(UserNameLineEdit->text() == ""){
QMessageBox::information(this,"wrong","user name error");
return ;
}
if(UserPasswdLineEdit->text() == ""){
QMessageBox::information(this,"wrong","pass word error");
return ;
}
UserName = UserNameLineEdit->text();
QString passwd = UserPasswdLineEdit->text();
tcpSocket = new QTcpSocket(this);
connect(tcpSocket,SIGNAL(connected()),this,SLOT(SignConnected()));
connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(SignDisconnected()));
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(SignDataReceived()));
tcpSocket->connectToHost(*ServerIP,port);
status = true;
}
}
void MainWindow::slotSelectDir(){
QString dirname = QFileDialog::getExistingDirectory(this,"选择文件保存位置","/");
DirLineEdit->setText(dirname);
}
void MainWindow::slotDownload(){
QString dirname = DirLineEdit->text();
if(dirname == ""){
return;
}
int rows = ResultTableWidget->rowCount();
bool flag = false;
for(int i = 0; i < rows; ++i){
if(ResultTableWidget->item(i,6)->checkState() == Qt::Checked
&& ResultTableWidget->item(i,4)->text() == "在线"){
tcp_meta tm;
tm.filename = ResultTableWidget->item(i,0)->text();
tm.size = ResultTableWidget->item(i,1)->text().toLong();
tm.filepath = ResultTableWidget->item(i,2)->text();
tm.owner = ResultTableWidget->item(i,3)->text();
tm.ip = ResultTableWidget->item(i,5)->text();
tm.port = port;
tasks.append(new P2P_Download(this, tm));
tasks.last()->start_as_client(dirname);
if(flag == false)
timer->start(100);
flag = true;
}
}
}
void MainWindow::slotSendMeta(){
QString filename = QFileDialog::getOpenFileName(this,"选择要共享的文件","/","files (*)");
QFileInfo info(
没有合适的资源?快使用搜索试试~ 我知道了~
基于QT的P2P共享文件系统
共23个文件
cpp:11个
h:9个
pro:2个
需积分: 50 33 下载量 139 浏览量
2019-12-02
19:49:48
上传
评论 9
收藏 819KB ZIP 举报
温馨提示
基于QT制作的一款P2P共享文件系统,网络架构模仿Napster软件,一个中性化服务器与若干个客户端。软件带有GUI界面,可实现共享文件的上传、下载和删除,使用方法详见我的博客。
资源推荐
资源详情
资源评论
收起资源包目录
P2P.zip (23个子文件)
P2P
server
main.cpp 183B
libmysql.dll 1.52MB
server.h 1KB
server.pro 564B
server.cpp 8KB
tcp_client_socket.cpp 2KB
tcp_server.h 793B
format_packet.h 326B
tcp_server.cpp 3KB
tcp_client_socket.h 649B
format_packet.cpp 754B
tcp_meta.cpp 1KB
tcp_meta.h 333B
client
main.cpp 204B
p2p_server.h 547B
client.pro 497B
tcp_client.h 2KB
p2p_download.h 683B
tcp_client.cpp 18KB
tcp_meta.cpp 1KB
p2p_server.cpp 2KB
tcp_meta.h 333B
p2p_download.cpp 3KB
共 23 条
- 1
资源评论
chegxy
- 粉丝: 50
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功