qt|qt 服务器和客户端

先上效果图
qt|qt 服务器和客户端
文章图片

1. 图中样式我采用了qss 总体设计, 然后对每个按钮进行单个设计
2. 使用 "控件提升", 为列表添加删除功能
2. 客户端可以监视多个客户端, 可以群发和单发
3. 输入框使用 "eventFilter" , 来使用 "回车" 发送消息

一、 服务器端
1. 在 .pro 文件中添加QT += core guinetwork
2. 定义:

private: Ui::MainWindow *ui; QTcpSocket * tcpClientSocket; // 客户端套接字 QTcpServer * tcpServer; // 服务器端对象 QList clientList; // 用户连接表

3 . new 出tcpServer . 添加信号和槽: connect(tcpServer,SIGNAL(newConnection()),this,SLOT(newConnectClient()));
tcpServer = new QTcpServer(this); ui->server_ip->setText(get_localmachine_ip()); // 获取本机的IP地址 ui->port_lineEdit->setText(tr("666")); connect(tcpServer,SIGNAL(newConnection()),this,SLOT(newConnectClient())); // 新建连接

4. newConnectClient函数用来保存新的连接,并添加"读"和"断开" 的处理函数
void MainWindow::newConnectClient() { tcpClientSocket = tcpServer->nextPendingConnection(); //获取网络套接字 clientList.append(tcpClientSocket); // 保存连接的SocketQString strIP =tr("%1 : %2").arg(tcpClientSocket->peerAddress().toString().split("::ffff:")[1]).arg( tcpClientSocket->peerPort()); // 拼接字符串 ui->connect_listWidget->addItem(strIP); ui->IP_comboBox->addItem(strIP); connect(tcpClientSocket,SIGNAL(readyRead()),this,SLOT(readData())); // 读取客户端发送的信息 connect(tcpClientSocket,SIGNAL(error(QAbstractSocket::SocketError)),\ this,SLOT(readError(QAbstractSocket::SocetError))); connect(tcpClientSocket,SIGNAL(disconnected()),this,SLOT(disconnectedSlot())); // 断开连接}

5. 点击监听按钮,开始监听客户端的请求:
try { bool ok = tcpServer->listen(QHostAddress::Any,ui->port_lineEdit->text().toInt()); if(ok) { ui->statusBar->showMessage("开始监听"); ui->connect_Button->setText("断开"); ui->connect_Button->setStyleSheet("QPushButton{background-color: rgb(124, 200, 78); color:red; }" "QPushButton:pressed{background-color:rgb(85, 170, 255); color:red; }"); }} catch(QAbstractSocket::SocketError e){ readError(e); }

6. 读取数据使用

void MainWindow::readData() { foreach(QTcpSocket *socket, clientList){ QByteArray data = https://www.it610.com/article/socket->readAll(); if(!data.isEmpty()) { ui->show_textEdit->append(QString(data)); } } }

7. 发送数据
// 发送 void MainWindow::on_send_button_clicked() { QString data = https://www.it610.com/article/ui->send_textEdit->toPlainText(); if (!data.isEmpty()) { if (!ui->IP_comboBox->currentText().isEmpty()) { //qDebug() <IP_comboBox->currentText(); //qDebug() << ui->IP_comboBox->itemText(0); //qDebug() << ui->IP_comboBox->currentIndex(); QString clientIP = ui->IP_comboBox->currentText().split(":")[0].trimmed(); int clientPort = ui->IP_comboBox->currentText().split(":") [1].trimmed().toInt(); //qDebug() << "ip " <isValid()) { if (socket->peerAddress().toString().split("::ffff:")[1] == clientIP && socket->peerPort() == clientPort) { int sendRe = socket->write(data.toUtf8().data()); ui->send_textEdit->clear(); } } } } } }


二、客户端:
1. 定义socket
private: Ui::MainWindow *ui; QTcpSocket *clientSocket;

2. 设置要连接的服务器IP和端口
this->setWindowTitle(tr("客户端")); this->setFixedSize(750,600); ui->statusBar->showMessage(tr("未连接服务器")); ui->server_ip->setText("192.168.1.3"); ui->port_lineEdit->setText("666");

3. 连接服务器, 注意ip地址需要用 “QHostAddress” 进行转换, 直接用ip无法连接 , 连接成功添加 “读” 和“删除” 信号
if (ui->connect_Button->text() == "连接") { this->clientSocket = new QTcpSocket(this); QString ip = ui->server_ip->text().trimmed(); QHostAddress * addtress = new QHostAddress; addtress->setAddress(ip); short port = ui->port_lineEdit->text().toShort(); this->clientSocket->connectToHost(*addtress,port); connect(clientSocket,SIGNAL(readyRead()),this,SLOT(readyReadSlot())); connect(clientSocket,SIGNAL(disconnected()),this,SLOT(disconnectedSlots())); }

4. 读取和发送和服务器一样

源码地址: https://download.csdn.net/download/chen1231985111/10872106






【qt|qt 服务器和客户端】

    推荐阅读