一、TcpSer
widget.h:
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
- #include
- #include
- #include
- #include
-
- 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 newConnection_slot(); //自定义处理newConnection信号的槽函数声明
- void readyRead_slot(); //自定义处理readyRead信号的槽函数声明
-
- void on_startBtn_clicked();
-
- private:
- Ui::Widget *ui;
-
- //定义服务器指针
- QTcpServer *server;
-
- //定义客户端容器
- QList
clientList; - };
- #endif // WIDGET_H
widget.cpp:
- #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_startBtn_clicked()
- {
- quint16 port = ui->portEdit->text().toUInt();
-
- if(!server->listen(QHostAddress::Any,port))
- {
- QMessageBox::information(this,"失败","监听失败");
- return;
- }else{
- QMessageBox::information(this,"成功","服务器启动成功");
- }
- }
- void Widget::newConnection_slot()
- {
- qDebug()<<"有新的客户端发来连接请求";
-
- //记录连接过来的客户端套接字地址
- QTcpSocket *s=server->nextPendingConnection();
-
- //将该套接字放入客户端链表中
- clientList.push_back(s);
-
- //客户端发来数据,套接字会自动发射一个readyRead信号,可以连接到对应槽函数进行处理
- connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
- }
-
- void Widget::readyRead_slot()
- {
- qDebug()<<"有新的客户端消息发来了";
-
- //遍历客户端链表,将无效客户端移除
- for(int i=0;i<clientList.size();i++)
- {
- //如果返回值为0则表示该套接字为无效连接
- if(clientList[i]->state() == 0)
- {
- clientList.removeAt(i);
- }
- }
-
- //遍历客户端链表,判断是拿个客户端发来的数据
- for(int i=0;i<clientList.size();i++)
- {
- //返回值是待读数据的字节数,如果是0表示无数据待读
- if(clientList[i]->bytesAvailable()!=0)
- {
- //将该套接字中的数据读出来
- QByteArray msg = clientList[i]->readAll();
-
- //将数据展示到ui界面
- ui->msgWidget->addItem(QString::fromLocal8Bit(msg));
-
- for(int j=0;j<clientList.size();j++)
- {
- clientList[j]->write(msg);
- }
- }
- }
- }
main.cpp:
- #include "widget.h"
-
- #include
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- return a.exec();
- }
二、TcpCli
widget.h:
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
- #include
-
- 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_sendBtn_clicked();
-
- void on_connectBtn_clicked();
-
- void on_disconnectBtn_clicked();
-
- void connected_slot(); //自定义处理connected信号的槽函数声明
- void readyRead_slot(); //自定义处理readyRead信号的槽函数声明
- void disconnected_slot(); //自定义处理disconnected信号的槽函数声明
- private:
- Ui::Widget *ui;
-
- //定义客户端指针
- QTcpSocket *socket;
-
- //定义字符串接收用户
- QString userName;
- };
- #endif // WIDGET_H
widget.cpp:
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
-
- //给客户端实例化对象
- socket = new QTcpSocket(this);
-
- //如果客户端成功连接到服务器,客户端会发送一个connected信号,可以连接槽函数进行处理
- connect(socket,&QTcpSocket::connected,this,&Widget::connected_slot);
-
- //如果服务器发来信号,客户端会自动发送一个readyRead信号,连接到对应槽函数进行处理
- connect(socket,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
-
- //如果客户端与服务器断开连接,客户端会自动发送一个disconnected信号,连接到槽函数进行处理
- connect(socket,&QTcpSocket::disconnected,this,&Widget::disconnected_slot);
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- //连接服务器按钮对应的槽函数的定义
- void Widget::on_sendBtn_clicked()
- {
- //获取ui界面输入的内容
- QString msg =userName+ ": " + ui->msgEdit->text();
-
-
- //发送给服务器
- socket->write(msg.toLocal8Bit());
-
-
- //清空编辑框内容
- ui->msgEdit->clear();
- }
-
- //处理connected信号的槽函数的定义
- void Widget::on_connectBtn_clicked()
- {
- //获得ui界面上的信息
- userName = ui->userNameEdit->text();
- QString ip = ui->ipEdit->text();
- quint16 port = ui->portEdit->text().toUInt();
-
- //客户端调用connectToHost连接给定的服务器
- socket->connectToHost(ip,port);
-
- //因为只需要连接一次,所以将连接写到构造函数中去
-
-
- }
-
- //处理readyRead信号的槽函数的定义
- void Widget::on_disconnectBtn_clicked()
- {
- QString msg = userName +": 离开聊天室";
- socket->write(msg.toLocal8Bit());
-
-
- //断开连接
- //函数原型: virtual void disconnectFromHost();
- socket->disconnectFromHost();
- }
-
- void Widget::connected_slot()
- {
- QMessageBox::information(this,"成功","已成功进入聊天室");
- //向服务器发送一条数据:userName进入聊天室
- QString msg = userName + ":进入聊天室";
-
- socket->write(msg.toLocal8Bit()); //将数据写给服务器
- }
-
- void Widget::readyRead_slot()
- {
- //读取套接字中的信息
- QByteArray msg = socket->readAll();
-
-
- //将数据展示到ui界面
- ui->msgWidget->addItem(QString::fromLocal8Bit(msg));
- }
-
- void Widget::disconnected_slot()
- {
- QMessageBox::information(this, "提示", "退出成功");
- }
main.cpp:
- #include "widget.h"
-
- #include
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- return a.exec();
- }