赞
踩
程序示例精选
Qt+C++ TCP发送接收信息客户端与服务端窗体
如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!
这篇博客针对<<Qt+C++ TCP发送接收信息客户端与服务端窗体>>编写代码,代码整洁,规则,易读。 应用推荐首选。
代码如下(示例):
- #include "QTcpClinet.h"
- #include "TcpServer.h"
- #include"qstring.h"
- #include"qdebug.h"
代码如下(示例):
- #include "QTcpClinet.h"
-
- QTcpClinet::QTcpClinet(QWidget *parent)
- : QWidget(parent)
- {
- ui.setupUi(this);
- socket = new QTcpSocket(this);
- }
-
- QTcpClinet::~QTcpClinet()
- {
- delete this->socket;//回收内存
- }
-
- void QTcpClinet::on_btn_connect_clicked()
- {
- if (ui.btn_connect->text()==tr("连接服务器"))
- {
- QString ip = ui.lineEdit_ip->text();//获取ip
- //取消已有的连接
- socket->abort();
- //连接服务器
- socket->connectToHost(ip, port);
- //如果连接成功
- if (isconnect)
- {
- ui.textEdit->append("The connection was successful!!");
- ui.btn_push->setEnabled(true);//按钮使能
- //修改按键文字
- ui.btn_connect->setText("断开服务器连接");
- //接收缓冲区(服务器)信息
- }
- else
- {
- ui.textEdit->append("The connection falied!!");
- }
- }
- else
- {
- //断开连接
- socket->disconnectFromHost();
- ui.btn_connect->setText("连接服务器");
- ui.btn_push->setEnabled(false);//关闭发送按钮使能
- }
-
- }
-
- //接收缓冲区信息函数
- void QTcpClinet::ReadData()
- {
- QByteArray buf = socket->readAll();
- ui.textEdit->append(buf);
- }
- //发送按钮事件
- void QTcpClinet::on_btn_push_clicked()
- {
- QByteArray data = ui.lineEdit_3->text().toUtf8();//获取lineEdit控件中的数据并发送给服务器
-
- //判断是否写入成功
- bool iswrite = socket->waitForBytesWritten();
- if (iswrite)
- {
- //写入成功
- }
- else
- {
- //没有写入成功
- }
- }

- #include "TcpServer.h"
- #include"qstring.h"
- #include"qdebug.h"
- #pragma execution_character_set("utf-8")
- TcpServer::TcpServer(QWidget *parent)
- : QWidget(parent)
- {
- ui.setupUi(this);
- server = new QTcpServer(this);
- //客户机连接信号槽
- connect(server, &QTcpServer::newConnection, this, &TcpServer::ClientConnect);
- }
-
- TcpServer::~TcpServer()
- {
- server->close();
- server->deleteLater();
- }
-
- void TcpServer::on_btn_listen_clicked()
- {
- if (ui.btn_listen->text()=="侦听")
- {
- //侦听指定端口的所有ip
- if (!server->listen(QHostAddress::Any, port))
- {
- //若出错,则输出错误信息
- qDebug() << server->errorString();
- return;
- }
- //修改按键文字
- ui.btn_listen->setText("取消侦听");
- }
- else
- {
- socket->abort();
- //取消侦听
- server->close();
- //修改按键文字
- ui.btn_listen->setText("侦听");
- }
- }
-
- void TcpServer::ClientConnect()
- {
- //解析所有客户连接
- while (server->hasPendingConnections())
- {
- //连接上后通过socket获取连接信息
- QString str = QString("[ip:%1,port:%2]").arg(socket->peerAddress().toString()).arg(socket->peerPort());
- //提示连接成功
- ui.textEdit_server->append(str+"Connect to the server");
- //复选框选项为连接服务器的ip
- ui.comboBox->addItem(str);
- //将socket地址放入combobox属性内
- //ui.comboBox->setItemData(ui.comboBox->count()-1, QVariant((int)socket));
- //监听客户端是否有消息发送
- }
- }
-
- //获取客户端向服务器发送的信息
- void TcpServer::ReadData1()
- {
- QString str = QString("[ip:%1,port:%2]").arg(socket->peerAddress().toString()).arg(socket->peerPort());
- ui.textEdit_server->append(str +QString(buf));
- }
-
- //服务器向客户端发送信息
- void TcpServer::on_btn_server_clicked()
- {
- if(ui.comboBox->count()== 0)return;
- //QTcpSocket* skt= (QTcpSocket*)ui.comboBox->itemData(ui.comboBox->currentIndex()).value<int>();
- socket->write(ui.lineEdit1->text().toUtf8());
- }

如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。