Qt中的TCP和命名空間的基本使用

一:對TCP的原理簡介:數組

二:Qt實行TCP機制():服務器

(1):server.h:app

#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTcpServer> //監聽套接字,只有服務器端才須要監聽套接字
#include<QTcpSocket> //通訊套接字
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    QTcpServer *TcpServer; //建立監聽套接字
    QTcpSocket *Socket; //建立通訊套接字

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

(2)main.cpp:函數

#include "server.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

(3):server.cpp佈局

#include "server.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowTitle("服務端");

    Socket = NULL; //爲了close的時候程序仍然處於監聽循環中/
    TcpServer = new QTcpServer(this); //建立套接字.
    TcpServer->listen(QHostAddress::Any,6666);
    connect(TcpServer,&QTcpServer::newConnection,
            [=]()
    {
       Socket = TcpServer->nextPendingConnection(); //取出創建好的套接字.
       //取出IP地址,注意IPv4地址由32爲的二進制數組成,咱們看到的是用點十進制法表示的。
       qint32 ip = Socket->peerAddress().toIPv4Address();
       qint16 port = Socket->peerPort();
       QString temp = QString("[%1:%2]:成功鏈接").arg(ip).arg(port);
       ui->readEdit->setText(temp);
    }
            );
    connect(Socket,&QTcpSocket::readyRead,
            [=]()
    {
        //獲取接受到的消息,
        QByteArray arry = Socket->readAll();
        ui->readEdit->append(arry);
    }
            );
}

Widget::~Widget()
{
    delete ui;
}

//在佈局中選中要轉到槽的控件,自動生成槽函數.(並且在這個過程就已經指定信號
void Widget::on_pushButton_clicked()
{
    if(Socket == NULL) //當沒有客戶端鏈接的時候,讓其不執行下面的代碼,如若執行程序就會自動終止.
    {
        return; //讓其循環,在這裏面.
    }
    //獲取編輯區內容
    QString str = ui->textEdit_2->toPlainText();
    Socket->write(str.toUtf8().data());//發送獲取到的內容.


}

void Widget::on_pushButton_2_clicked()
{
    if(Socket == NULL)
    {
        return;
    }
    //主動和客戶端端口斷開鏈接.
    Socket->disconnectFromHost();
    Socket->close();
    Socket = NULL;
}

(4):客戶端:ui

custom.hthis

#ifndef CUSTOM_H
#define CUSTOM_H

#include <QWidget>
#include <QTcpSocket> //通訊套接字
QT_BEGIN_NAMESPACE
namespace Ui { class Custom; }
QT_END_NAMESPACE

class Custom : public QWidget
{
    Q_OBJECT

public:
    Custom(QWidget *parent = nullptr);
    ~Custom();
    QTcpSocket *Socket;

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

private:
    Ui::Custom *ui;
};
#endif // CUSTOM_H

main.cppspa

#include "custom.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Custom w;
    w.show();
    return a.exec();
}

custom.cppcode

#include "custom.h"
#include "ui_custom.h"
#include<QHostAddress>
Custom::Custom(QWidget *parent): QWidget(parent),ui(new Ui::Custom)
{
    ui->setupUi(this);
    Socket = NULL;
    Socket = new QTcpSocket(this);
    connect(Socket,&QTcpSocket::connected,
            [=]()
    {
        ui->read_TEXT->setText("成功和服務器創建好鏈接");
    }
            );
    connect(Socket,&QTcpSocket::readyRead,
            [=]()
    {
        //獲取對方發送的內容
        QByteArray array = Socket->readAll();
        //追加到編輯區
        ui->read_TEXT->append(array);
    }
            );
}

Custom::~Custom()
{
    delete ui;
}


void Custom::on_pushButton_clicked()
{
    //獲取服務器端口和IP地址
    QString IP = ui->lineEdit_2->text();
    qint16 port = ui->line_Ediy1->text().toInt();
    //主動和服務器創建鏈接
    Socket->connectToHost(QHostAddress(IP),port);
}

void Custom::on_pushButton_2_clicked()
{
    //獲取編輯框內容
    QString  str = ui->read_TEXT->toPlainText();
    //發送數據
    Socket->write(str.toUtf8().data());
}

void Custom::on_pushButton_3_clicked()
{
    //主動和對方斷開鏈接
    Socket->disconnectFromHost();
}

 三:命名空間的使用:server

相關文章
相關標籤/搜索