Qt学习(五):TCP通信
生活随笔
收集整理的這篇文章主要介紹了
Qt学习(五):TCP通信
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
知識點
- 通信套接字和監(jiān)聽套接字
- 獲取編輯器內(nèi)容,發(fā)送到套接字
完整項目github地址:
結(jié)果演示
widget.cpp
#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);//為了防止段錯誤tcpServer = NULL;tcpSocket = NULL;setWindowTitle("服務(wù)端(端口:8888)");//監(jiān)聽套接字,指定父對象,讓其自動回收空間tcpServer = new QTcpServer(this);tcpServer->listen(QHostAddress::Any,8888);connect(tcpServer,&QTcpServer::newConnection,[=](){//取出建立好連接的套接字tcpSocket=tcpServer->nextPendingConnection();//獲取對方的IP和端口QString ip=tcpSocket->peerAddress().toString();qint16 port =tcpSocket->peerPort();QString ipDate=QString("ip=%1 port=%2").arg(ip).arg(port);ui->showEdit->setText(ipDate);//從通信套接字中取出內(nèi)容connect(tcpSocket,&QTcpSocket::readyRead,[=](){QByteArray array=tcpSocket->readAll();ui->showEdit->append(array);});}); }Widget::~Widget() {delete ui; }void Widget::on_buttonSend_clicked() {if(NULL == tcpSocket){return;}//獲取編輯區(qū)內(nèi)容QString str=ui->sendEdit->toPlainText();//給對方發(fā)送數(shù)據(jù), 使用套接字是tcpSockettcpSocket->write(str.toUtf8().data()); }void Widget::on_buttonClose_clicked() {if(NULL == tcpSocket){return;}//主動和客戶端端口連接tcpSocket->disconnectFromHost();tcpSocket->close();tcpSocket = NULL; }widget.h
#ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QTcpServer> //監(jiān)聽套接字 #include <QTcpSocket> //通信套接字namespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private slots:void on_buttonSend_clicked();void on_buttonClose_clicked();private:Ui::Widget *ui;QTcpServer *tcpServer; //監(jiān)聽套接字QTcpSocket *tcpSocket; //通信套接字 };#endif // WIDGET_Hclienttcp.cpp
#include "clienttcp.h" #include "ui_clienttcp.h" #include <QHostAddress>ClientTCP::ClientTCP(QWidget *parent) :QWidget(parent),ui(new Ui::ClientTCP) {ui->setupUi(this);setWindowTitle("客戶端");tcpSocket=new QTcpSocket(this);connect(tcpSocket,&QTcpSocket::connected,[=](){ui->textEditRead->setText("與服務(wù)端已經(jīng)成功連接!");});//從通信套接字里面取內(nèi)容connect(tcpSocket,&QTcpSocket::readyRead,[=](){//獲取對方發(fā)送的內(nèi)容QByteArray array = tcpSocket->readAll();//追加到編輯區(qū)中ui->textEditRead->append(array);});}ClientTCP::~ClientTCP() {delete ui; }void ClientTCP::on_buttonConnect_clicked() {//獲取服務(wù)器ip和端口QString ip=ui->lineEditIP->text();qint16 port=ui->lineEditPort->text().toInt();//主動和服務(wù)器建立連接tcpSocket->connectToHost(QHostAddress(ip),port); }void ClientTCP::on_buttonWrite_clicked() {//獲取編輯器內(nèi)容,發(fā)送到套接字//獲取編輯區(qū)內(nèi)容QString str=ui->textEditWrite->toPlainText();//給對方發(fā)送數(shù)據(jù), 使用套接字是tcpSockettcpSocket->write(str.toUtf8().data()); }void ClientTCP::on_buttonClose_clicked() {//主動和客戶端端口連接tcpSocket->disconnectFromHost();tcpSocket->close();}clienttcp.h
#ifndef CLIENTTCP_H #define CLIENTTCP_H#include <QWidget> #include <QTcpSocket>namespace Ui { class ClientTCP; }class ClientTCP : public QWidget {Q_OBJECTpublic:explicit ClientTCP(QWidget *parent = 0);~ClientTCP();private slots:void on_buttonConnect_clicked();void on_buttonWrite_clicked();void on_buttonClose_clicked();private:Ui::ClientTCP *ui;QTcpSocket *tcpSocket; //通信套接字};#endif // CLIENTTCP_H總結(jié)
以上是生活随笔為你收集整理的Qt学习(五):TCP通信的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt学习:棋盘游戏
- 下一篇: Qt学习(六):UDP通信