26 Star 107 Fork 45

c./SocketTool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
tcpserveroperatewidget.cpp 2.94 KB
一键复制 编辑 原始数据 按行查看 历史
#include "tcpserveroperatewidget.h"
#include "ui_tcpserveroperatewidget.h"
TcpServeroperatewidget::TcpServeroperatewidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::TcpServeroperatewidget)
{
ui->setupUi(this);
m_tcpServer = nullptr;
}
TcpServeroperatewidget::~TcpServeroperatewidget()
{
if (m_tcpServer != nullptr)
{
m_tcpServer->close();
m_tcpServer->deleteLater();
}
delete ui;
}
void TcpServeroperatewidget::setTcpServer(QTcpServer *tcpServer)
{
Q_ASSERT(m_tcpServer == nullptr);
if (tcpServer == nullptr)
return;
m_tcpServer = tcpServer;
m_hostAddress = m_tcpServer->serverAddress();
m_port = m_tcpServer->serverPort();
connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(newConnectionComing()));
connect(m_tcpServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(acceptSocketError(QAbstractSocket::SocketError)));
ui->labelLocalPort->setText(QString(tr("监听端口: %1").arg(m_tcpServer->serverPort())));
if (m_tcpServer->isListening())
{
ui->pushButtonStart->setEnabled(false);
ui->pushButtonStop->setEnabled(true);
ui->labelSocketState->setText(tr("已启动"));
}
else
{
ui->pushButtonStart->setEnabled(true);
ui->pushButtonStop->setEnabled(false);
ui->labelSocketState->setText(tr("已停止"));
}
}
void TcpServeroperatewidget::on_pushButtonStart_clicked()
{
if (!m_tcpServer->listen(m_hostAddress, m_port))
{
QString error = QString("Start TCP Server failed! %1").arg(m_tcpServer->errorString());
QMessageBox::critical(this, tr("Error"), error);
}
if (m_tcpServer->isListening())
{
ui->pushButtonStart->setEnabled(false);
ui->pushButtonStop->setEnabled(true);
ui->labelSocketState->setText(tr("已启动"));
}
else
{
ui->pushButtonStart->setEnabled(true);
ui->pushButtonStop->setEnabled(false);
ui->labelSocketState->setText(tr("已停止"));
}
}
void TcpServeroperatewidget::on_pushButtonStop_clicked()
{
m_tcpServer->close();
if (m_tcpServer->isListening())
{
ui->pushButtonStart->setEnabled(false);
ui->pushButtonStop->setEnabled(true);
ui->labelSocketState->setText(tr("已启动"));
}
else
{
ui->pushButtonStart->setEnabled(true);
ui->pushButtonStop->setEnabled(false);
ui->labelSocketState->setText(tr("已停止"));
}
}
void TcpServeroperatewidget::newConnectionComing()
{
QTcpSocket* tcpSocket = m_tcpServer->nextPendingConnection();
if (tcpSocket != nullptr)
{
tcpSocket->close();
tcpSocket->deleteLater();
}
}
void TcpServeroperatewidget::acceptSocketError(QAbstractSocket::SocketError socketError)
{
QString msg = QString("%1 发生错误:%4").arg(QTime::currentTime().toString()).
arg(m_tcpServer->errorString());
ui->textBrowser->appendPlainText(msg);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/chengjianxi/SocketTool.git
git@gitee.com:chengjianxi/SocketTool.git
chengjianxi
SocketTool
SocketTool
master

搜索帮助