QT示例:基于TCP 点对多通讯(server,clients)
- 一、服务器server
- 二、客户端Client
下载:基于TCP 点对多Socket通讯
一、服务器server
因为对于客户端来说,只能连接一个服务器。而对于服务器来说,它是面向多连接的,如何协调处理多客户端连接就显得尤为重要。
- 注意问题:
每个新加入的客户端,服务器给其分配一个SocketDescriptor后,就会emit newConnection()信号,但分配好的SocketDecriptor并没有通过newConnection()信号传递,所以用户得不到这个客户端标识SocketDescriptor。同样的,每当服务器收到新的消息时,客户端会emit readReady()信号,然而readReady()信号也没有传递SocketDescriptor, 这样的话,服务器端即使接收到消息,也不知道这个消息是从哪个客户端发出的。 - 解决的方法:
- 通过重写==[virtual protected] void QTcpServer::incomingConnection(qintptr socketDescriptor)==,获取soketDescriptor。自定义TcpClient类继承QTcpSocket,并将获得的soketDescriptor作为类成员。 这个方法的优点是:可以获取到soketDescriptor,灵活性高。缺点是:需要重写函数、自定义类。
- 在newConnection()信号对应的槽函数中,通过QTcpSocket *QTcpServer::nextPendingConnection()函数获取 新连接的客户端:Returns the next pending connection as a connected QTcpSocket object. 虽然仍然得不到soketDescriptor,**但可以通过QTcpSocket类的peerAddress()和peerPort()成员函数获取客户端的IP和端口号,同样是唯一标识。 **优点:无需重写函数和自定义类,代码简洁。缺点:无法获得SocketDecriptor,灵活性差。
本文示例为第二种方法:
1).pro 添加:
QT += network
2)主函数 main.cpp 添加:
#include "mytcpserver.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MyTcpServer w;w.show();return a.exec();
}
3)MyTcpServer.h 添加:
#include "mytcpserver.h"
#include "ui_mytcpserver.h"MyTcpServer::MyTcpServer(QWidget *parent) :QMainWindow(parent),ui(new Ui::MyTcpServer)
{ui->setupUi(this);// 一 、创建QTcpSever对象;tcpServer = new QTcpServer(this);ui->edtIP->setText(QNetworkInterface().allAddresses().at(1).toString()); //获取本地IPui->btnConnect->setEnabled(true);ui->btnSend->setEnabled(false);// 设置默认按钮样式ui->btnConnect->setStyleSheet("");connect(tcpServer, &QTcpServer::newConnection, this, &MyTcpServer::NewConnectionSlot);
}MyTcpServer::~MyTcpServer()
{delete ui;
}// 二、监听--断开
void MyTcpServer::on_btnConnect_clicked()
{if(ui->btnConnect->text()=="监听"){bool ok = tcpServer->listen(QHostAddress::Any, ui->edtPort->text().toInt());if(ok){ui->btnConnect->setText("断开");ui->btnConnect->setStyleSheet("color: red;");ui->btnSend->setEnabled(true);}}else{for(int i=0; i<tcpClient.length(); i++) // 断开所有连接{tcpClient[i]->disconnectFromHost();bool ok = tcpClient[i]->waitForDisconnected(1000);if(!ok){// 处理异常QMessageBox::warning(this, tr("错误"),tr("断开连接失败!"), QMessageBox::Ok);}tcpClient.removeAt(i); // 从保存的客户端列表中取去除}tcpServer->close(); // 不再监听端口ui->btnConnect->setText("监听");ui->btnConnect->setStyleSheet("");ui->btnSend->setEnabled(false);}
}// 三、新连接建立的槽函数
void MyTcpServer::NewConnectionSlot()
{currentClient = tcpServer->nextPendingConnection();tcpClient.append(currentClient);ui->cbxConnection->addItem(tr("%1:%2").arg(currentClient->peerAddress().toString().split("::ffff:")[1]).arg(currentClient->peerPort()));connect(currentClient, &QTcpSocket::readyRead, this, &MyTcpServer::ReadData);connect(currentClient, &QTcpSocket::disconnected, this, &MyTcpServer::disconnectedSlot);
}// 四、客户端数据可读信号,对应的读数据槽函数
void MyTcpServer::ReadData()
{// 由于readyRead信号并未提供SocketDecriptor,所以需要遍历所有客户端for(int i=0; i<tcpClient.length(); i++){QByteArray buffer = tcpClient[i]->readAll();if(buffer.isEmpty()) // 客户端 数据为空,则跳过continue;// 客户端有数据则 获取IP 和端口static QString IP_Port, IP_Port_Pre;IP_Port = tr("[%1:%2]:").arg(tcpClient[i]->peerAddress().toString().split("::ffff:")[1]).arg(tcpClient[i]->peerPort());// 若此次消息的地址与上次不同,则需显示此次消息的客户端地址if(IP_Port != IP_Port_Pre)ui->edtRecv->append(IP_Port);ui->edtRecv->append(buffer);//更新ip_portIP_Port_Pre = IP_Port;}
}// 五、断开连接的槽函数
void MyTcpServer::disconnectedSlot()
{//由于disconnected信号并未提供SocketDescriptor,所以需要遍历寻找for(int i=0; i<tcpClient.length(); i++){if(tcpClient[i]->state() == QAbstractSocket::UnconnectedState){// 删除存储在combox中的客户端信息ui->cbxConnection->removeItem(ui->cbxConnection->findText(tr("%1:%2").arg(tcpClient[i]->peerAddress().toString().split("::ffff:")[1]).arg(tcpClient[i]->peerPort())));// 删除存储在tcpClient列表中的客户端信息tcpClient[i]->destroyed();tcpClient.removeAt(i);}}
}// 六、发送数据
void MyTcpServer::on_btnSend_clicked()
{QString data = ui->edtSend->toPlainText();if(data == "")return; // 文本输入框为空时//全部连接if(ui->cbxConnection->currentIndex() == 0){for(int i=0; i<tcpClient.length(); i++)tcpClient[i]->write(data.toLatin1()); //qt5除去了.toAscii()}//指定连接else{QString clientIP = ui->cbxConnection->currentText().split(":")[0]; // IP 地址int clientPort = ui->cbxConnection->currentText().split(":")[1].toInt(); // port 端口号
// qDebug() << clientIP;
// qDebug() << clientPort;for(int i=0; i<tcpClient.length(); i++){if(tcpClient[i]->peerAddress().toString().split("::ffff:")[1]==clientIP&& tcpClient[i]->peerPort()==clientPort){tcpClient[i]->write(data.toLatin1());return; //ip:port唯一,无需继续检索}}}
}// 清楚窗口
void MyTcpServer::on_btnClear_clicked()
{ui->edtRecv->clear();
}
5)界面 mytcpserver.ui
二、客户端Client
1).pro 添加:
QT += network
2)主函数 main.cpp 添加:
#include "mytcpclient.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MyTcpClient w;w.show();return a.exec();
}
3)MyTcpClient.h 添加:
#include <QMainWindow>
#include <QTcpSocket>
#include <QHostAddress>
#include <QMessageBox>namespace Ui {
class MyTcpClient;
}class MyTcpClient : public QMainWindow
{Q_OBJECTpublic:explicit MyTcpClient(QWidget *parent = 0);~MyTcpClient();private:Ui::MyTcpClient *ui;QTcpSocket *tcpClient;private slots://客户端槽函数void ReadData();void ReadError(QAbstractSocket::SocketError);void on_btnConnect_clicked();void on_btnSend_clicked();void on_pushButton_clicked();
};
4)MyTcpClient.cpp 添加:
#include "mytcpclient.h"
#include "ui_mytcpclient.h"MyTcpClient::MyTcpClient(QWidget *parent) :QMainWindow(parent),ui(new Ui::MyTcpClient)
{ui->setupUi(this);// 一、初始化TCP客户端tcpClient = new QTcpSocket(this); //实例化tcpClienttcpClient->abort(); //取消原有连接ui->btnConnect->setEnabled(true);ui->btnSend->setEnabled(false);connect(tcpClient, &QTcpSocket::readyRead, this, &MyTcpClient::ReadData);connect(tcpClient, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(ReadError(QAbstractSocket::SocketError)));
}MyTcpClient::~MyTcpClient()
{delete ui;
}// 二、连接
void MyTcpClient::on_btnConnect_clicked()
{if(ui->btnConnect->text()=="连接"){tcpClient->connectToHost(ui->edtIP->text(), ui->edtPort->text().toInt());if (tcpClient->waitForConnected(1000)) // 连接成功则进入if{}{ui->btnConnect->setText("断开");ui->btnSend->setEnabled(true);}}else{tcpClient->disconnectFromHost();if (tcpClient->state() == QAbstractSocket::UnconnectedState || tcpClient->waitForDisconnected(1000)) //已断开连接则进入if{}{ui->btnConnect->setText("连接");ui->btnSend->setEnabled(false);}}
}// 三、读取数据
void MyTcpClient::ReadData()
{QByteArray buffer = tcpClient->readAll();if(!buffer.isEmpty()){ui->edtRecv->append(buffer);}
}// 四、发送数据
void MyTcpClient::on_btnSend_clicked()
{QString data = ui->edtSend->toPlainText();if(data != ""){tcpClient->write(data.toLatin1()); //qt5出去了.toAscii()}
}// 连接错误信息处理
void MyTcpClient::ReadError(QAbstractSocket::SocketError)
{tcpClient->disconnectFromHost();ui->btnConnect->setText(tr("连接"));QMessageBox msgBox;msgBox.setText(tr("failed to connect server because %1").arg(tcpClient->errorString()));msgBox.exec();
}// 清空按钮
void MyTcpClient::on_pushButton_clicked()
{ui->edtRecv->clear();
}
2)mytcpclient.ui 添加
参考博客:
QT 之TCP网络编程