1.效果
2.服务器:
2.1:ui界面
2.2:头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QList>
#include <QMessageBox>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_clicked();
void newConnection_slot();
void readyRead_slot();
private:
Ui::Widget *ui;
QTcpServer *server;
QList<QTcpSocket *> clientList;
};
#endif // WIDGET_H
2.3:cpp文件
main:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
函数封装:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
server=new QTcpServer(this);
connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text()=="启动")
{
ui->pushButton->setText("关闭");
quint16 port=ui->lineEdit->text().toUInt();
if(!server->listen(QHostAddress::Any,port))
{
QMessageBox::information(this,"失败","监听失败");
}else
{
QMessageBox::information(this,"成功","服务器启动成功");
}
}else{
for(int i=0;i<clientList.size();i++)
{
clientList.removeAt(i);
}
server->close();
ui->pushButton->setText("启动");
}
}
void Widget::newConnection_slot()
{
qDebug()<<"您有新的客户端发来了连接请求了";
QTcpSocket *s=server->nextPendingConnection();
clientList.push_back(s);
connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
}
void Widget::readyRead_slot()
{
qDebug()<<"有新的客户端信息发来了";
for(int i=0;i<clientList.size();i++)
{
if(clientList[i]->state()==0)
{
clientList.removeAt(i);
}
}
for(int i=0;i<clientList.size();i++)
{
if(clientList[i]->bytesAvailable()!=0)
{
QByteArray msg=clientList[i]->readAll();
ui->msgWidget->addItem(QString::fromLocal8Bit(msg));
for(int j=0;j<clientList.size();j++)
{
clientList[j]->write(msg);
}
}
}
}
2.4:pro文件
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
3.客户端:
3.1:ui界面
3.2:头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpSocket>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_2_clicked();
void connect_slot();
void readyRead_slot();
void on_pushButton_1_clicked();
void on_pushButton_3_clicked();
void disconnected_slot();
private:
Ui::Widget *ui;
QTcpSocket *socket;
QString userName;
};
#endif // WIDGET_H
3.3:cpp文件
main:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
函数封装:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
socket =new QTcpSocket(this);
connect(socket,&QTcpSocket::connected,this,&Widget::connect_slot);
connect(socket,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
connect(socket,&QTcpSocket::disconnected,this,&Widget::disconnected_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_2_clicked()
{
userName=ui->lineEdit_2->text();
QString ip=ui->lineEdit_3->text();
quint16 port=ui->lineEdit_4->text().toUInt();
socket->connectToHost(ip,port);
}
void Widget::connect_slot()
{
QMessageBox::information(this,"成功","您已经成功进入聊天室");
QString msg=userName +":进入聊天室";
socket->write(msg.toLocal8Bit());
}
void Widget::readyRead_slot()
{
QByteArray msg=socket->readAll();
ui->listWidget->addItem(QString::fromLocal8Bit(msg));
}
void Widget::on_pushButton_1_clicked()
{
QString msg =userName+":"+ui->lineEdit->text();
socket->write(msg.toLocal8Bit());
ui->lineEdit->clear();
}
void Widget::on_pushButton_3_clicked()
{
QString msg=userName+":离开聊天室";
socket->write(msg.toLocal8Bit());
socket->disconnectFromHost();
}
void Widget::disconnected_slot()
{
QMessageBox::information(this,"提示","退出成功");
}
3.4:pro文件
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target