Skip to content

Commit 9ad1ac8

Browse files
committed
these are source files
1 parent 32dfb7c commit 9ad1ac8

20 files changed

+1803
-0
lines changed

chat.pro

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#-------------------------------------------------
2+
#
3+
# Project created by QtCreator 2013-03-05T00:41:57
4+
#
5+
#-------------------------------------------------
6+
7+
QT += core gui
8+
QT += network
9+
10+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
11+
12+
TARGET = chat
13+
TEMPLATE = app
14+
15+
16+
SOURCES += main.cpp\
17+
widget.cpp \
18+
tcpserver.cpp \
19+
tcpclient.cpp
20+
21+
HEADERS += widget.h \
22+
tcpserver.h \
23+
tcpclient.h
24+
25+
FORMS += widget.ui \
26+
tcpserver.ui \
27+
tcpclient.ui
28+
29+
RESOURCES += \
30+
imgs.qrc

chat.pro.user

+239
Large diffs are not rendered by default.

imgs.qrc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<RCC>
2+
<qresource prefix="/imgs">
3+
<file>imgs/bold.ico</file>
4+
<file>imgs/clear.ico</file>
5+
<file>imgs/color.ico</file>
6+
<file>imgs/italic.ico</file>
7+
<file>imgs/save.ico</file>
8+
<file>imgs/send.ico</file>
9+
<file>imgs/underline.ico</file>
10+
</qresource>
11+
</RCC>

imgs/bold.ico

9.44 KB
Binary file not shown.

imgs/clear.ico

9.44 KB
Binary file not shown.

imgs/color.ico

9.44 KB
Binary file not shown.

imgs/italic.ico

9.44 KB
Binary file not shown.

imgs/save.ico

9.44 KB
Binary file not shown.

imgs/send.ico

9.44 KB
Binary file not shown.

imgs/underline.ico

9.44 KB
Binary file not shown.

main.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "widget.h"
2+
#include <QApplication>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
QApplication a(argc, argv);
7+
Widget w;
8+
w.show();
9+
10+
return a.exec();
11+
}

tcpclient.cpp

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include "tcpclient.h"
2+
#include "ui_tcpclient.h"
3+
#include <QFileDialog>
4+
#include <QMessageBox>
5+
6+
TcpClient::TcpClient(QWidget *parent) :
7+
QDialog(parent),
8+
ui(new Ui::TcpClient)
9+
{
10+
ui->setupUi(this);
11+
this->setFixedSize(350,180);
12+
13+
TotalBytes = 0;
14+
bytesReceived = 0;
15+
fileNameSize = 0;
16+
17+
tcpClient = new QTcpSocket(this);
18+
tcpPort = 6666;
19+
connect(tcpClient,SIGNAL(readyRead()),this,SLOT(readMessage()));
20+
connect(tcpClient,SIGNAL(error(QAbstractSocket::SocketError)),this,
21+
SLOT(displayError(QAbstractSocket::SocketError)));
22+
23+
}
24+
25+
TcpClient::~TcpClient()
26+
{
27+
delete ui;
28+
}
29+
30+
void TcpClient::changeEvent(QEvent *e)
31+
{
32+
QDialog::changeEvent(e);
33+
switch (e->type()) {
34+
case QEvent::LanguageChange:
35+
ui->retranslateUi(this);
36+
break;
37+
default:
38+
break;
39+
}
40+
}
41+
42+
void TcpClient::setHostAddress(QHostAddress address)
43+
{
44+
hostAddress = address;
45+
newConnect();
46+
}
47+
void TcpClient::newConnect()
48+
{
49+
blockSize = 0;
50+
tcpClient->abort();
51+
tcpClient->connectToHost(hostAddress,tcpPort);
52+
time.start();
53+
}
54+
55+
void TcpClient::readMessage()
56+
{
57+
QDataStream in(tcpClient);
58+
in.setVersion(QDataStream::Qt_5_0);
59+
60+
float useTime = time.elapsed();
61+
if(bytesReceived <= sizeof(qint64)*2){
62+
if((tcpClient->bytesAvailable() >= sizeof(qint64)*2) && (fileNameSize == 0)){
63+
in>>TotalBytes>>fileNameSize;
64+
bytesReceived += sizeof(qint64)*2;
65+
}
66+
if((tcpClient->bytesAvailable() >= fileNameSize) && (fileNameSize != 0)){
67+
in>>fileName;
68+
bytesReceived +=fileNameSize;
69+
70+
if(!localFile->open(QFile::WriteOnly)){
71+
QMessageBox::warning(this,tr("应用程序"),tr("无法读取文件 %1:\n%2.").arg(fileName).arg(localFile->errorString()));
72+
return;
73+
}
74+
}else{
75+
return;
76+
}
77+
}
78+
if(bytesReceived < TotalBytes){
79+
bytesReceived += tcpClient->bytesAvailable();
80+
inBlock = tcpClient->readAll();
81+
localFile->write(inBlock);
82+
inBlock.resize(0);
83+
}
84+
ui->progressBar->setMaximum(TotalBytes);
85+
ui->progressBar->setValue(bytesReceived);
86+
qDebug()<<bytesReceived<<"received"<<TotalBytes;
87+
88+
double speed = bytesReceived / useTime;
89+
ui->tcpClientStatusLabel->setText(tr("已接收 %1MB (%2MB/s) \n共%3MB 已用时:%4秒\n估计剩余时间:%5秒")
90+
.arg(bytesReceived / (1024*1024))
91+
.arg(speed*1000/(1024*1024),0,'f',2)
92+
.arg(TotalBytes / (1024 * 1024))
93+
.arg(useTime/1000,0,'f',0)
94+
.arg(TotalBytes/speed/1000 - useTime/1000,0,'f',0));
95+
96+
if(bytesReceived == TotalBytes)
97+
{
98+
tcpClient->close();
99+
ui->tcpClientStatusLabel->setText(tr("接收文件 %1 完毕").arg(fileName));
100+
localFile->close();
101+
}
102+
}
103+
104+
105+
void TcpClient::displayError(QAbstractSocket::SocketError socketError)
106+
{
107+
switch(socketError)
108+
{
109+
case QAbstractSocket::RemoteHostClosedError : break;
110+
default : qDebug() << tcpClient->errorString();
111+
}
112+
}
113+
114+
115+
116+
117+
void TcpClient::on_tcpClientCloseBtn_clicked()
118+
{
119+
tcpClient->abort();
120+
this->close();
121+
}
122+
123+
124+
void TcpClient::closeEvent(QCloseEvent *)
125+
{
126+
on_tcpClientCloseBtn_clicked();
127+
}
128+
129+
void TcpClient::on_tcpClientCancelBtn_clicked()
130+
{
131+
tcpClient->abort();
132+
}
133+
134+

tcpclient.h

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#ifndef TCPCLIENT_H
2+
#define TCPCLIENT_H
3+
4+
#include <QDialog>
5+
#include <QTcpSocket>
6+
#include <QHostAddress>
7+
#include <QFile>
8+
#include <QTime>
9+
10+
namespace Ui {
11+
class TcpClient;
12+
}
13+
14+
class TcpClient : public QDialog
15+
{
16+
Q_OBJECT
17+
18+
public:
19+
explicit TcpClient(QWidget *parent = 0);
20+
~TcpClient();
21+
void setHostAddress(QHostAddress address);
22+
void setFileName(QString fileName){localFile = new QFile(fileName);}
23+
24+
protected:
25+
void changeEvent(QEvent *);
26+
void closeEvent(QCloseEvent *);
27+
private:
28+
Ui::TcpClient *ui;
29+
QTcpSocket *tcpClient;
30+
quint16 blockSize;
31+
QHostAddress hostAddress;
32+
qint16 tcpPort;
33+
34+
qint64 TotalBytes;
35+
qint64 bytesReceived;
36+
qint64 bytesToReceive;
37+
qint64 fileNameSize;
38+
QString fileName;
39+
QFile *localFile;
40+
QByteArray inBlock;
41+
42+
QTime time;
43+
44+
private slots:
45+
void on_tcpClientCloseBtn_clicked();
46+
void newConnect();
47+
void readMessage();
48+
void displayError(QAbstractSocket::SocketError);
49+
50+
51+
void on_tcpClientCancelBtn_clicked();
52+
};
53+
54+
#endif // TCPCLIENT_H

tcpclient.ui

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>TcpClient</class>
4+
<widget class="QDialog" name="TcpClient">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>350</width>
10+
<height>180</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>接收端</string>
15+
</property>
16+
<widget class="QLabel" name="tcpClientStatusLabel">
17+
<property name="geometry">
18+
<rect>
19+
<x>90</x>
20+
<y>30</y>
21+
<width>171</width>
22+
<height>21</height>
23+
</rect>
24+
</property>
25+
<property name="font">
26+
<font>
27+
<pointsize>12</pointsize>
28+
<weight>75</weight>
29+
<bold>true</bold>
30+
</font>
31+
</property>
32+
<property name="text">
33+
<string>等待接收文件... ...</string>
34+
</property>
35+
<property name="alignment">
36+
<set>Qt::AlignCenter</set>
37+
</property>
38+
</widget>
39+
<widget class="QProgressBar" name="progressBar">
40+
<property name="geometry">
41+
<rect>
42+
<x>50</x>
43+
<y>70</y>
44+
<width>271</width>
45+
<height>23</height>
46+
</rect>
47+
</property>
48+
<property name="value">
49+
<number>0</number>
50+
</property>
51+
</widget>
52+
<widget class="QWidget" name="horizontalLayoutWidget">
53+
<property name="geometry">
54+
<rect>
55+
<x>70</x>
56+
<y>100</y>
57+
<width>201</width>
58+
<height>51</height>
59+
</rect>
60+
</property>
61+
<layout class="QHBoxLayout" name="horizontalLayout">
62+
<item>
63+
<widget class="QPushButton" name="tcpClientCancelBtn">
64+
<property name="text">
65+
<string>取消</string>
66+
</property>
67+
</widget>
68+
</item>
69+
<item>
70+
<spacer name="horizontalSpacer">
71+
<property name="orientation">
72+
<enum>Qt::Horizontal</enum>
73+
</property>
74+
<property name="sizeHint" stdset="0">
75+
<size>
76+
<width>40</width>
77+
<height>20</height>
78+
</size>
79+
</property>
80+
</spacer>
81+
</item>
82+
<item>
83+
<widget class="QPushButton" name="tcpClientCloseBtn">
84+
<property name="text">
85+
<string>关闭</string>
86+
</property>
87+
</widget>
88+
</item>
89+
</layout>
90+
</widget>
91+
</widget>
92+
<resources/>
93+
<connections/>
94+
</ui>

0 commit comments

Comments
 (0)