1、指定父对象,监听套接字
indexSocket = 0;
tcpServer = new QTcpServer(this);
const QString IP = "192.168.9.11";
QHostAddress address;
address.setAddress(IP);
tcpServer->listen(address, 8888);
connect(tcpServer, &QTcpServer::newConnection,this,&MainWindow::onNewConnect);
void MainWindow::onNewConnect()
{
tcpSocket[indexSocket] = tcpServer->nextPendingConnection();
QString ip = tcpSocket[indexSocket]->peerAddress().toString();
qint16 port = tcpSocket[indexSocket]->peerPort();
QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port);
ui->textBrowser->append(temp);
for(int i=0; i<indexSocket+1; i++){
connect(tcpSocket[i], &QTcpSocket::readyRead,
[=]()
{
QByteArray array = tcpSocket[i]->readAll();
if(array.length())
ui->textBrowser->append(array);
}
);
}
indexSocket++;
}
2、给多个客户端发送数据
for(int i=0; i<indexSocket; i++){
if(NULL == tcpSocket[i]){
return;
}
QString str = ui->textEditSend->toPlainText();
tcpSocket[i]->write( str.toUtf8().data() );
}
源文件
.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
#define MAXCONNECT 100
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButtonClose_clicked();
void on_pushButtonBreak_clicked();
void onNewConnect(void);
void on_pushButtonSend_clicked();
private:
Ui::MainWindow *ui;
QTcpServer *tcpServer;
QTcpSocket *tcpSocket[MAXCONNECT];
int indexSocket;
};
#endif
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
indexSocket = 0;
tcpServer = new QTcpServer(this);
const QString IP = "192.168.9.11";
QHostAddress address;
address.setAddress(IP);
tcpServer->listen(address, 8888);
connect(tcpServer, &QTcpServer::newConnection,this,&MainWindow::onNewConnect);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButtonClose_clicked()
{
ui->textBrowser->clear();
ui->textEditSend->clear();
}
void MainWindow::on_pushButtonBreak_clicked()
{
for(int i=0; i<indexSocket; i++){
if(NULL == tcpSocket[i])
{
return;
}
tcpSocket[i]->disconnectFromHost();
tcpSocket[i]->close();
tcpSocket[i] = NULL;
}
}
void MainWindow::onNewConnect()
{
tcpSocket[indexSocket] = tcpServer->nextPendingConnection();
QString ip = tcpSocket[indexSocket]->peerAddress().toString();
qint16 port = tcpSocket[indexSocket]->peerPort();
QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port);
ui->textBrowser->append(temp);
for(int i=0; i<indexSocket+1; i++){
connect(tcpSocket[i], &QTcpSocket::readyRead,
[=]()
{
QByteArray array = tcpSocket[i]->readAll();
if(array.length())
ui->textBrowser->append(array);
}
);
}
indexSocket++;
}
void MainWindow::on_pushButtonSend_clicked()
{
for(int i=0; i<indexSocket; i++){
if(NULL == tcpSocket[i]){
return;
}
QString str = ui->textEditSend->toPlainText();
tcpSocket[i]->write( str.toUtf8().data() );
}
}