#include "tcpserverwidget.h"
#include "common.h"
#include "ui_tcpserverwidget.h"
#include <QHeaderView>
#include <QTcpServer>
#include <QTcpSocket>
TCPServerWidget::TCPServerWidget(QWidget *parent)
: QWidget(parent), ui(new Ui::TCPServerWidget) {
ui->setupUi(this);
ui->localIp->addItem("Any");
ui->localIp->addItems(getIPAddresses());
ui->clientTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->clientTable->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->clientTable->horizontalHeader()->setSectionResizeMode(
QHeaderView::Stretch);
m_Server = new QTcpServer;
connect(m_Server, &QTcpServer::newConnection, [=]() {
QTcpSocket *tcpSocket = m_Server->nextPendingConnection();
QString ip = tcpSocket->peerAddress().toString();
quint16 port = tcpSocket->peerPort();
appendLog(ui->textEdit, QString("%1:%2:connect").arg(ip).arg(port));
bool r = false;
for (int i = 0; i < ui->clientTable->rowCount(); ++i) {
QTableWidgetItem *ipitem = ui->clientTable->item(i, 0);
QTableWidgetItem *portitem = ui->clientTable->item(i, 1);
if (nullptr != ipitem && nullptr != portitem &&
ipitem->text() == ip && portitem->text().toUShort() == port) {
r = true;
break;
}
}
connect(tcpSocket, &QTcpSocket::disconnected, [=]() {
appendLog(ui->textEdit,
QString("%1:%2 disconnected")
.arg(tcpSocket->peerAddress().toString())
.arg(tcpSocket->peerPort()));
for (int i = 0; i < ui->clientTable->rowCount(); ++i) {
QTableWidgetItem *ipitem = ui->clientTable->item(i, 0);
QTableWidgetItem *portitem = ui->clientTable->item(i, 1);
if (nullptr != ipitem && nullptr != portitem &&
ipitem->text() == ip &&
portitem->text().toUShort() == port) {
ui->clientTable->removeRow(i);
break;
}
}
for (QTcpSocket *socket : m_Clients) {
if (ip == socket->peerAddress().toString() &&
port == socket->peerPort()) {
m_Clients.removeOne(socket);
socket = nullptr;
break;
}
}
});
connect(tcpSocket, &QTcpSocket::readyRead, [=]() {
QByteArray data = (ui->toHex->isChecked())
? tcpSocket->readAll().toHex()
: tcpSocket->readAll();
appendLog(ui->textEdit,
QString("%1:%2:%3").arg(ip).arg(port).arg(QString(data)));
});
m_Clients.append(tcpSocket);
int row = ui->clientTable->rowCount();
ui->clientTable->insertRow(row);
ui->clientTable->setItem(row, 0, new QTableWidgetItem(ip));
ui->clientTable->setItem(row, 1,
new QTableWidgetItem(QString::number(port)));
});
}
TCPServerWidget::~TCPServerWidget() {
delete ui;
}
QTcpSocket *TCPServerWidget::socket(int row) {
QTableWidgetItem *ipitem = ui->clientTable->item(row, 0);
QTableWidgetItem *portitem = ui->clientTable->item(row, 1);
if (nullptr != ipitem && nullptr != portitem) {
QString ip = ipitem->text();
quint16 port = portitem->text().toUShort();
for (QTcpSocket *tcpSocket : m_Clients) {
if (ip == tcpSocket->peerAddress().toString() &&
port == tcpSocket->peerPort())
return tcpSocket;
}
}
return nullptr;
}
void TCPServerWidget::on_listen_clicked() {
if (ui->listen->text() == "listen") {
if (m_Server->listen((ui->localIp->currentText() == "Any")
? QHostAddress::Any
: QHostAddress(ui->localIp->currentText()),
ui->localPort->value())) {
ui->listen->setText("listening");
appendLog(ui->textEdit, "start listening");
} else {
appendLog(ui->textEdit,
"start listen error:" + m_Server->errorString());
}
} else {
m_Server->close();
ui->listen->setText("listen");
for (QTcpSocket *tcpSocket : m_Clients) {
tcpSocket->close();
tcpSocket->disconnectFromHost();
}
m_Clients.clear();
appendLog(ui->textEdit, "stop listening");
}
}
void TCPServerWidget::on_close_clicked() {
int row = ui->clientTable->currentRow();
QTcpSocket *tcpSocket = socket(row);
if (nullptr != tcpSocket) {
tcpSocket->close();
tcpSocket->disconnectFromHost();
}
}
void TCPServerWidget::on_send_clicked() {
QByteArray data = (ui->toHex->isChecked())
? QByteArray::fromHex(ui->message->text().toUtf8())
: ui->message->text().toUtf8();
int row = ui->clientTable->currentRow();
QTcpSocket *tcpSocket = socket(row);
if (nullptr != tcpSocket)tcpSocket->write(data, data.size());
}
void TCPServerWidget::on_clear_clicked() {
ui->textEdit->clear();
}