增加发送文字信息
This commit is contained in:
parent
4322e27ea6
commit
2b7a62b8c1
|
@ -1,9 +1,9 @@
|
|||
# File Trans
|
||||
基于 Qt Socket 的文件传输软件。
|
||||
基于 Qt Socket 的文件传输和消息发送软件。
|
||||
FileTrans 程序在 64 位 Linux 系统 Qt5.15.2 编译。
|
||||
|
||||
## 界面
|
||||

|
||||

|
||||
[Android版](https://gitee.com/sonichy/filetrans_android)
|
||||
|
||||
## 参考
|
||||
|
|
|
@ -88,7 +88,8 @@ void MainWindow::newConnect()
|
|||
connect(tcpSocket, &QTcpSocket::disconnected, [=](){
|
||||
qDebug() << state;
|
||||
if (state == 0) {
|
||||
state = 1;
|
||||
if (type == "file")
|
||||
state = 1;
|
||||
} else {
|
||||
file.close();
|
||||
file.open(QIODevice::ReadOnly);
|
||||
|
@ -115,18 +116,24 @@ void MainWindow::readyRead()
|
|||
QString s = BA;
|
||||
QJsonParseError JPE;
|
||||
QJsonDocument JD = QJsonDocument::fromJson(s.toUtf8(), &JPE);
|
||||
QString fileName = "?";
|
||||
QString fileName = "?";
|
||||
if (JPE.error == QJsonParseError::NoError) {
|
||||
if (JD.isObject()) {
|
||||
if (JD.isObject()) {
|
||||
QJsonObject JO = JD.object();
|
||||
fileName = JO.value("fileName").toString();
|
||||
lastModified = JO.value("lastModified").toDouble()/1000;
|
||||
type = JO.value("type").toString();
|
||||
if (type == "text") {
|
||||
QString message = JO.value("message").toString();
|
||||
ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] " + "接收:" + message);
|
||||
} else if(type == "file") {
|
||||
fileName = JO.value("fileName").toString();
|
||||
lastModified = JO.value("lastModified").toDouble()/1000;
|
||||
ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] " + "接收:" + fileName);
|
||||
QString filepath = ui->lineEdit_root_dir->text() + "/" + fileName;
|
||||
file.setFileName(filepath);
|
||||
file.open(QIODevice::WriteOnly);
|
||||
}
|
||||
}
|
||||
}
|
||||
ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] " + "接收:" + fileName);
|
||||
QString filepath = ui->lineEdit_root_dir->text() + "/" + fileName;
|
||||
file.setFileName(filepath);
|
||||
file.open(QIODevice::WriteOnly);
|
||||
} else {
|
||||
file.write(BA);
|
||||
length += BA.length();
|
||||
|
@ -153,6 +160,31 @@ QString MainWindow::BS(long b)
|
|||
return s;
|
||||
}
|
||||
|
||||
void MainWindow::on_pushButton_send_pressed()
|
||||
{
|
||||
QString s = ui->lineEdit_message->text();
|
||||
if (s == "")
|
||||
return;
|
||||
qint64 port = ui->lineEdit_port->text().toInt();
|
||||
QTcpSocket *tcpSocket1 = new QTcpSocket(this);
|
||||
connect(tcpSocket1, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), [=](QAbstractSocket::SocketError socketError){
|
||||
qDebug() << socketError;
|
||||
QMetaEnum metaEnum = QMetaEnum::fromType<QAbstractSocket::SocketError>();
|
||||
QString errorString = metaEnum.valueToKey(socketError);
|
||||
ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] " + errorString);
|
||||
});
|
||||
tcpSocket1->connectToHost(QHostAddress(ui->lineEdit_ip->text()), port);
|
||||
connect(tcpSocket1, &QTcpSocket::connected, [=](){
|
||||
QJsonObject JO;
|
||||
JO.insert("type", "text");
|
||||
JO.insert("message", s);
|
||||
QString s = QJsonDocument(JO).toJson(QJsonDocument::Compact);
|
||||
tcpSocket1->write(s.toUtf8());
|
||||
tcpSocket1->close();
|
||||
ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] 发送:" + s);
|
||||
});
|
||||
}
|
||||
|
||||
void MainWindow::upload(QString filepath)
|
||||
{
|
||||
if (ui->lineEdit_ip->text() == IP) {
|
||||
|
@ -172,6 +204,7 @@ void MainWindow::upload(QString filepath)
|
|||
connect(tcpSocket1, &QTcpSocket::connected, [=](){
|
||||
QString fileName = QFileInfo(filepath).fileName();
|
||||
QJsonObject JO;
|
||||
JO.insert("type", "file");
|
||||
JO.insert("fileName", fileName);
|
||||
JO.insert("lastModified", static_cast<qint64>(QFileInfo(filepath).lastModified().toTime_t())*1000);
|
||||
QString s = QJsonDocument(JO).toJson(QJsonDocument::Compact);
|
||||
|
|
|
@ -35,13 +35,13 @@ public:
|
|||
private:
|
||||
Ui::MainWindow *ui;
|
||||
QSettings settings;
|
||||
QString IP, path;
|
||||
QString IP, path, type;
|
||||
QTcpServer *tcpServer;
|
||||
QTcpSocket *tcpSocket;
|
||||
int state;
|
||||
QFile file;
|
||||
qint64 length, port_local;
|
||||
QString BS(long b);
|
||||
QString BS(long b);
|
||||
void upload(QString filepath);
|
||||
double lastModified = 0;
|
||||
void dragEnterEvent(QDragEnterEvent*);
|
||||
|
@ -50,6 +50,7 @@ private:
|
|||
private slots:
|
||||
void newConnect();
|
||||
void readyRead();
|
||||
void on_pushButton_send_pressed();
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -40,10 +40,10 @@
|
|||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<item row="4" column="0">
|
||||
<widget class="QLabel" name="label_5">
|
||||
<property name="text">
|
||||
<string>Port</string>
|
||||
<string>信息</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
|
@ -60,23 +60,6 @@
|
|||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label_4">
|
||||
<property name="text">
|
||||
<string>本机</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1" colspan="2">
|
||||
<widget class="QLabel" name="label_local">
|
||||
<property name="text">
|
||||
<string>ip:port</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1" colspan="2">
|
||||
<widget class="QLineEdit" name="lineEdit_port">
|
||||
<property name="acceptDrops">
|
||||
|
@ -87,6 +70,23 @@
|
|||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1" colspan="2">
|
||||
<widget class="QLabel" name="label_local">
|
||||
<property name="text">
|
||||
<string>ip:port</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Port</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1" colspan="2">
|
||||
<widget class="QLineEdit" name="lineEdit_root_dir">
|
||||
<property name="acceptDrops">
|
||||
|
@ -94,7 +94,30 @@
|
|||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label_4">
|
||||
<property name="text">
|
||||
<string>本机</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="1">
|
||||
<widget class="QLineEdit" name="lineEdit_message"/>
|
||||
</item>
|
||||
<item row="4" column="2">
|
||||
<widget class="QPushButton" name="pushButton_send">
|
||||
<property name="text">
|
||||
<string>发送</string>
|
||||
</property>
|
||||
<property name="flat">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1" colspan="2">
|
||||
<widget class="QLineEdit" name="lineEdit_ip">
|
||||
<property name="acceptDrops">
|
||||
<bool>false</bool>
|
||||
|
@ -121,7 +144,7 @@
|
|||
<item>
|
||||
<widget class="QPushButton" name="pushButton_start">
|
||||
<property name="text">
|
||||
<string>Start</string>
|
||||
<string>启动</string>
|
||||
</property>
|
||||
<property name="checkable">
|
||||
<bool>true</bool>
|
||||
|
@ -138,7 +161,7 @@
|
|||
<item>
|
||||
<widget class="QPushButton" name="pushButton_clear">
|
||||
<property name="text">
|
||||
<string>Clear</string>
|
||||
<string>清除</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
|
|
BIN
preview.jpg
BIN
preview.jpg
Binary file not shown.
Before Width: | Height: | Size: 116 KiB |
Loading…
Reference in New Issue