1、简介
简单实现控制TCP服务器获取连接的套接字。点击断开服务器即可关闭所有连接,最大连接数量为5个。
声明源文件
#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{//设置固定大小setFixedSize(1024,600);btnClearText = new QPushButton(this);btnCtrlServer = new QPushButton(this);textRecvBrowser = new QTextBrowser(this);//按键清空btnClearText->setGeometry(48,40,200,60);btnClearText->setText("清空浏览");//设置控制按钮btnCtrlServer->setGeometry(48, 40+40+30 , 200, 60);btnCtrlServer->setText("打开服务");//浏览处理textRecvBrowser->setGeometry(300, 40, 649, 500);//连接处理QObject::connect(btnClearText, SIGNAL(clicked()), this, SLOT(on_pushButton_textRecvBrowser()));QObject::connect(btnCtrlServer, SIGNAL(clicked()),this, SLOT(on_pushButton_ctrlTcpServer()));//TCP服务器处理tcpServer = new QTcpServer(this);QObject::connect(tcpServer, SIGNAL(newConnection()),this, SLOT(on_tcpServer_newConnectCallHandler()));//最大连接数量5个tcpServer->setMaxPendingConnections(5);tcpServer->listen(QHostAddress("127.0.0.1"), 80);if(tcpServer->isListening())btnCtrlServer->setText("关闭服务");elsebtnCtrlServer->setText("打开服务");}MainWindow::~MainWindow()
{delete btnClearText;delete btnCtrlServer;delete textRecvBrowser;delete tcpServer;
}//清空浏览记录
void MainWindow::on_pushButton_textRecvBrowser()
{textRecvBrowser->clear();
}//
void MainWindow::on_pushButton_ctrlTcpServer()
{//状态查询if(tcpServer->isListening()){//遍历所有数据foreach(QTcpSocket* tcpSocket, tcpSocketList){if(tcpSocket->state() == QAbstractSocket::ConnectedState)tcpSocket->close();//关闭连接}//tcpServer->close();}elsetcpServer->listen(QHostAddress::Any, 80);//监听状态if(tcpServer->isListening())btnCtrlServer->setText("关闭服务");elsebtnCtrlServer->setText("打开服务");
}//IPv6转换IPv4
QString convert_to_ipv4_addr(QHostAddress &addr)
{quint32 addr_origin = addr.toIPv4Address();QHostAddress addr_host = QHostAddress(addr_origin);QString addr_str = addr_host.toString();return addr_str;
}//新建连接
void MainWindow::on_tcpServer_newConnectCallHandler()
{while (tcpServer->hasPendingConnections()) {//获取连接QTcpSocket *tcpSocket = tcpServer->nextPendingConnection();//添加指针列表tcpSocketList.append(tcpSocket);//显示具体连接信息QHostAddress ipv6 = tcpSocket->peerAddress();textRecvBrowser->append(convert_to_ipv4_addr(ipv6) + ":" + QString::number(tcpSocket->peerPort()));//设置连接QObject::connect(tcpSocket, SIGNAL(readyRead()),this, SLOT(on_tcpSocket_readyRead()));QObject::connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(on_tcpSocket_disconnected()));//qDebug() << "当前连接数量" << tcpSocketList.size();}}void MainWindow::on_tcpSocket_disconnected()
{QTcpSocket* socket = qobject_cast<QTcpSocket *>(sender()); //当前信息//断开所有连接if(socket->state() == QAbstractSocket::ConnectedState)socket->close();//关闭连接//断开连接则清除tcpSocketList.removeOne(socket);textRecvBrowser->append("已断开连接");qDebug() << "当前连接数量" << tcpSocketList.size();
}//
void MainWindow::on_tcpSocket_readyRead()
{QTcpSocket* socket = qobject_cast<QTcpSocket *>(sender()); // 取得当前socket对象socket->write("Hello",5);
// QMessageBox msgBox(this);// msgBox.setText("我是准备好读数据");
// msgBox.setWindowTitle("提示");
// msgBox.exec();
}
声明的头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QPushButton>
#include <QTextBrowser>
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include <QMessageBox>
#include <QDebug>class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_pushButton_textRecvBrowser();void on_pushButton_ctrlTcpServer();void on_tcpServer_newConnectCallHandler();void on_tcpSocket_readyRead();void on_tcpSocket_disconnected();
private:QList<QTcpSocket *> tcpSocketList;QPushButton *btnCtrlServer = nullptr;QPushButton *btnClearText = nullptr;QTextBrowser *textRecvBrowser = nullptr;QTcpServer *tcpServer = nullptr;
};
#endif // MAINWINDOW_H