include "recive.h"
#include "ui_recive.h"
recive::recive(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::recive)
{
ui->setupUi(this);
}
recive::~recive()
{
delete ui;
}
void recive::on_pushButton_clicked()
{
socket = new QTcpSocket;
QString ip = ui->lineEdit->text();
quint16 port = ui->lineEdit_2->text().toUShort();
socket->connectToHost( QHostAddress(ip),port);
connect(socket,SIGNAL(connected()),this,SLOT(is_connected()));
connect(socket,SIGNAL(readyRead()),this,SLOT(recv_Read()));
recive_size = 0;
}
void recive::is_connected()
{
ui->textEdit->append("和服务器连接成功,请选择要保存的文件夹路径.......");
}
void recive::recv_Read()
{
//开始接收头解析
QByteArray head;
QString str_size;
head = socket->readAll();
filename = QString(head).section("##",0,0);
size = QString(head).section("##",1,1).toULongLong();
ui->textEdit->append("文件名:"+filename+" "+"文件大小:"+str_size.setNum(size)+"kb");
socket->write("66");
//=======打开文件=========
file.setFileName(filePath+"/"+filename);
if( false== file.open(QFile::WriteOnly))
{
ui->textEdit->append("文件打开有误....");
}
else
{
ui->textEdit->append("文件打开写成功....");
}
disconnect(socket,SIGNAL(readyRead()),this,SLOT(recv_Read()));
connect(socket,SIGNAL(readyRead()),this,SLOT(recv_Read1()));
my_timer = new QTimer;
connect(my_timer,SIGNAL(timeout()),this,SLOT(timer_hand()));
my_timer->start(1000);
}
void recive::timer_hand()
{
quint32 speed_size;
static quint64 last_size = 0,count = 0;
speed_size = recive_size-last_size;
if(speed_size == 0)
{
count++;
}
if(count>5) { this->~recive(); }
last_size =recive_size;
QString str;
QString str1;
str1.setNum(speed_size);
if(recive_size<1024)
{
str.setNum(recive_size);
ui->lineEdit_3->setText(&quot;接受数据:&quot;+str+&quot;Kb&quot;+&quot;网速:&quot;+str1+&quot;KB&quot;);
return ;
}
if(recive_size/1024>0 && (recive_size/1024)<1024 )
{
str.setNum(recive_size/1024);
ui->lineEdit_3->setText(&quot;接受数据:&quot;+str+&quot;Mb&quot;+&quot;网速:&quot;+str1+&quot;KB&quot;);
return ;
}
if((recive_size/1024)/1024 >0 )
{
str.setNum(recive_size/1024/1024);
ui->lineEdit_3->setText(&quot;接受数据:&quot;+str+&quot;Gb&quot;+&quot;网速:&quot;+str1+&quot;KB&quot;);
}
}
void recive::recv_Read1() //正式接受文本内容
{
QString str;
QByteArray text = socket->readAll();
file.write(text);
recive_size++;
}
void recive::on_pushButton_2_clicked()
{
filePath = QFileDialog::getExistingDirectory(this, &quot;choose src Directory&quot;,&quot;/&quot;);
if(filePath.isEmpty() == false )
{
ui->textEdit->append(&quot;文件路径:&quot;+filePath);
socket->write(&quot;55&quot;);
}
else
{
ui->textEdit->append(&quot;文件选择路径有误....&quot;);
}
}
// ====================
#include &quot;send.h&quot;
#include &quot;ui_send.h&quot;
send::send(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::send)
{
ui->setupUi(this);
ui->pushButton_2->setEnabled(false);
ui->pushButton_3->setEnabled(false);
}
send::~send()
{
delete ui;
}
void send::on_pushButton_clicked()
{
server = new QTcpServer;
QString ip = ui->lineEdit_2->text();
quint16 port = ui->lineEdit->text().toUShort();
if(true == server->listen(QHostAddress(ip),port))
{
ui->textEdit->append(&quot;此id监听成功....&quot;);
}
else
{
ui->textEdit->append(&quot;此id监听失败....&quot;);
}
connect(server,SIGNAL(newConnection()),this,SLOT(handconnect()));
}
void send::handconnect() //一个新的连接
{ static bool sever_falg = 0;
if(sever_falg == 0) //阻止其他客户端连接
{
sever_falg = 1;
ui->pushButton->setEnabled(false);
ui->pushButton_2->setEnabled(true);
ui->pushButton_3->setEnabled(true);
socket = new QTcpSocket; //这个socket 为 读写通信socket
socket = server->nextPendingConnection();
QString ip_str1 = socket->peerAddress().toString();
QString ip_str2 = ip_str1.section(':',3);
ui->textEdit->append (&quot;接受端IP:&quot;+ip_str1 );
// connect(socket,SIGNAL(readyRead()),this,SLOT(send_read()));
}
else
{
sever_falg = 1;
return ;
}
}
void send::send_read()
{
QByteArray ackbuff;
ackbuff = socket->read(2);
if(ackbuff.toShort() == 66 ) ui->textEdit->append(&quot;文件头发送成功....&quot;);
my_timer = new QTimer;
connect(my_timer,SIGNAL(timeout()),this,SLOT(timer_hand()));
my_timer->start(1);
ui->textEdit->append(&quot;开始发送文本内容........&quot;);
// emit write_flag();
}
void send::send_text() //空函数
{}
void send::timer_hand() //===发送文本内容
{
static quint32 count = 0;
qint64 len = 0;
char buf[1024*5];
len = file.read(buf,sizeof(buf));
socket->write(buf,len);
if(len <= 0)
{
ui->textEdit->append(&quot;文件发送成功........&quot;);
if(my_timer->isActive() == true )
{
my_timer->stop();
this->~send();
}
return ;
}
count++;
}
void send::on_pushButton_3_clicked() //发送文件
{
QByteArray ackbuff;
ackbuff = socket->read(2);
if(ackbuff.toShort() == 55 )
{
ui->textEdit->append(&quot;连接成功....&quot;);
ui->textEdit->append(&quot;开始发送文件头....&quot;);
QString head = QString(&quot;%1##%2&quot;).arg(filename).arg(size/1024);
socket->write(head.toUtf8());
connect(socket,SIGNAL(readyRead()),this,SLOT(send_read()));
// connect(this,SIGNAL(write_flag()),this,SLOT(send_text())); //连接信号和曹
}
else
{
ui->textEdit->append(&quot;等待客户端连接,请再次发送....&quot;);
}
}
void send::on_pushButton_2_clicked() //选择文件
{
QString filePath = QFileDialog::getOpenFileName(this,&quot;open&quot;,&quot;../&quot;);
if(filePath.isEmpty() == false )
{
ui->textEdit->append(&quot;文件路径:&quot;+filePath);
QFileInfo info(filePath);
filename = info.fileName();
size = info.size();
file.setFileName(filePath);
if( false== file.open(QIODevice::ReadOnly))
{
ui->textEdit->append(&quot;文件打开路径有误....&quot;);
}
else
{
ui->textEdit->append(&quot;文件打开成功....&quot;);
}
}
else
{
ui->textEdit->append(&quot;文件选择路径有误....&quot;);
}
} |
|