Skip to content

Commit

Permalink
修复TCP读写器时,没有客户端连接的时候,无法关闭TCP服务器的BUG;添加显示TCP客户端IP和端口的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
changfeng1050 committed Mar 9, 2018
1 parent a10d9d5 commit 7ec4184
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 47 deletions.
3 changes: 3 additions & 0 deletions data/AbstractReadWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Q_OBJECT

virtual bool isOpen()=0;

virtual bool isConnected() =0;

virtual void close()=0;

virtual QByteArray readAll()=0;
Expand All @@ -26,6 +28,7 @@ Q_OBJECT
virtual QString settingsText() const =0;

signals:

void readyRead();
};

Expand Down
4 changes: 4 additions & 0 deletions data/SerialReadWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ QString SerialReadWriter::settingsText() const {
return QString("%1 %2 %3 %4 %5").arg(settings.name).arg(settings.baudRate).arg(settings.dataBits).arg(
settings.stopBits).arg(settings.parity);
}

bool SerialReadWriter::isConnected() {
return serial != nullptr && serial->isOpen();
}
2 changes: 2 additions & 0 deletions data/SerialReadWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Q_OBJECT

bool isOpen() override;

bool isConnected() override;

void close() override;

QByteArray readAll() override;
Expand Down
21 changes: 19 additions & 2 deletions data/TcpReadWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,28 @@ bool TcpReadWriter::open() {
}
_tcpSocket = _tcpServer->nextPendingConnection();
connect(_tcpSocket, &QTcpSocket::readyRead, this, &TcpReadWriter::readyRead);
connect(_tcpSocket, &QTcpSocket::disconnected, [this] {
emit connectionClosed();
});

auto address = _tcpSocket->peerAddress().toString();
if (address.contains(':')) {
auto index = address.lastIndexOf(':');
address = address.right(address.count() - index - 1);
}
auto port = _tcpSocket->peerPort();
emit currentSocketChanged(address, port);
});


if (!_tcpServer->isListening()) {
return _tcpServer->listen(QHostAddress::Any, static_cast<quint16>(_port));
}
return false;
}

bool TcpReadWriter::isOpen() {
return _tcpServer != nullptr && _tcpServer->isListening() && _tcpSocket != nullptr && _tcpSocket->isOpen();
return _tcpServer != nullptr && _tcpServer->isListening();
}

void TcpReadWriter::close() {
Expand All @@ -42,11 +54,12 @@ void TcpReadWriter::close() {
_tcpServer = nullptr;
}


if (_tcpSocket != nullptr) {
if (_tcpSocket->isOpen()) {
_tcpSocket->close();
}
delete _tcpSocket;
// delete _tcpSocket;
_tcpSocket = nullptr;
}
}
Expand Down Expand Up @@ -78,3 +91,7 @@ void TcpReadWriter::setPort(int port) {
QString TcpReadWriter::settingsText() const {
return QString("%1 %2").arg(_address).arg(_port);
}

bool TcpReadWriter::isConnected() {
return isOpen() && _tcpSocket != nullptr && _tcpSocket->isOpen();
}
9 changes: 9 additions & 0 deletions data/TcpReadWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Q_OBJECT

bool isOpen() override;

bool isConnected() override;

QString settingsText() const override;

void close() override;
Expand All @@ -33,6 +35,13 @@ Q_OBJECT

qint64 write(const QByteArray &byteArray) const override;

signals:

void currentSocketChanged(const QString &address, qint16 port);

void connectionClosed();


private:
QTcpServer *_tcpServer{nullptr};
QTcpSocket *_tcpSocket{nullptr};
Expand Down
8 changes: 8 additions & 0 deletions global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ QString getIp() {
return address.toString();
}
}
for (auto address:ipAddress) {
if (address.protocol() == QAbstractSocket::IPv6Protocol) {
return address.toString();
}
}

return "";
}

QString getAddressString(const QHostAddress &address) {
return address.toString();
}
Loading

0 comments on commit 7ec4184

Please sign in to comment.