server
- #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();
- //监听客户端的连接请求
- //函数原型bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
- //参数1:要监听的主机地址,如果是any表示监听任意的主机地址,也可以是特定的主机地址
- //参数2:该服务器的端口号,如果是0,表示由服务器自动指定,一般由程序员指定
- //返回值:成功进入监听,返回true,其他情况,返回false
- if(!server->listen(QHostAddress::Any, port)){
- QMessageBox::information(this, "失败", "监听失败");
- return;
- }else{
- QMessageBox::information(this, "成功", "服务器监听成功");
- }
-
- //此时,服务器已经成功进入监听状态,如果有客户端向服务器发来连接请求
- //那么该服务器,就会自动发射一个newconnection的信号,我们可以将该信号连接到对应的槽函数中执行相关函数
- //由于只要连接一次,所以可以将该连接放在构造函数中完成
- }
- //自定义处理newConnection的槽函数的实现
- void Widget::newConnection_slot()
- {
- qDebug()<<"您有";
- QTcpSocket *s = server->nextPendingConnection();
- //将套接字放入客户端容器中
- clientList.push_back(s);
- //至此,多个客户端已经跟服务器建立连接,并放入容器中了
- //此时,如果有客户端向服务器发来数据,那么对应的客户端套接字就会自动发射一个readyread信号
- //我们可以将该信号连接到自定义的槽函数中,处理相关逻辑
- connect(s, &QTcpSocket::readyRead, this, &Widget::readyRead_solt);
- }
- //处理readyRead信号的槽函数的声明
- void Widget::readyRead_solt()
- {
- qDebug()<<"有新的客户端";
- //遍历客户端链表,将无效的客户端移除
- for(int i=0; i
size(); i++){ - //判断当前套接字是否有效连接
- if(clientList[i]->state() == 0){ //如果是0,表示该套接字是无效连接
- //将该套接字移出链表
- clientList.removeAt(i);
- }
- }
-
- for(int i=0; i
size(); i++){ - //求当前客户端套接字中待读字节的字节数
- //返回值:待读数据的字节数,如果等于0,表示无数据待读
- if(clientList[i]->bytesAvailable()){
- //将数据读出来
- QByteArray msg = clientList[i]->readAll();
-
- //将数据展示到ui界面
- ui->msgWidget->addItem( QString::fromLocal8Bit(msg));
-
- //将接收到的数据,转发给所有客户端
- for (int j=0; j
size(); j++) { - clientList[i]->write(msg);
- }
- }
- }
- }
client
- #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_startButton_clicked()
- {
- //获取用户输入的ip、username和port
- username = ui->usernameEdit->text();
- QString ip = ui->addressEdit->text();
- quint16 port = ui->portEdit->text().toUInt();
- //连接服务器
- socket->connectToHost(ip, port);
- }
- //处理connected信号的槽函数
- void Widget::connected_slot()
- {
- QMessageBox::information(this, "连接", "已成功进入聊天室");
- //向服务器发送一条数据:***,进入聊天室
- QString msg = username + "进入聊天室";
- socket->write(msg.toLocal8Bit());
- }
-
- void Widget::readyread_slot()
- {
- //读取套接字中的信息
- QByteArray msg = socket->readAll();
- //将数据展示到recvList
- ui->recvList->addItem(QString::fromLocal8Bit(msg));
- }
-
- void Widget::disconnected_slot()
- {
- QMessageBox::information(this, "退出", "退出成功");
- }
-
- void Widget::on_sendbtn_clicked()
- {
- //获取ui界面输入的内容
- QString msg = username + ":" + ui->sendmsgEdit->toPlainText();
-
- //发送
- socket->write(msg.toLocal8Bit());
-
- //清空输入框
- ui->sendmsgEdit->setText("");
- }
-
- void Widget::on_stopButton_clicked()
- {
- QString msg = username + "离开聊天室";
- socket->write(msg.toLocal8Bit());
- socket->disconnectFromHost();
-
-
- }
运行截图
