1. 程式人生 > >QT基於TCP的Socket程式設計

QT基於TCP的Socket程式設計

1、在工程檔案中加入QT += network

2、伺服器除了使用到了QTcpSocket類,還需要用到QTcpSever類

(1)建立QTcpSever物件

  1. tcpServer = new QTcpServer(this);  
(2)監聽埠
  1. tcpServer->listen(QHostAddress::LocalHost, 30000)  
(3)當伺服器被客戶端訪問時,會發出newConnection()訊號,因此為該訊號新增槽函式,並用一個QTcpSocket物件接受客戶端訪問
  1. connect(tcpServer, &QTcpServer::newConnection,  
  2.             this, &MainWindow::sendMessage);  
(4)獲取客戶端連線
  1. // 獲取已經建立的連線的套接字
  2.     clientConnection = tcpServer->nextPendingConnection();  
  3.     QObject::connect(clientConnection, &QTcpSocket::readyRead, this, &MainWindow::readMessage);  
(5)發資料
  1. // 用於暫存要傳送的資料
  2.     QByteArray block;  
  3.     QDataStream out(&block, QIODevice::WriteOnly);  
  4.     // 設定資料流的版本,客戶端和伺服器端使用的版本要相同
  5.     out.setVersion(QDataStream::Qt_5_6);  
  6.     out << (quint16)0;  
  7.     out << tr("connect sucessful !");  
  8.     out.device()->seek(0);  
  9.     out << (quint16)(block.size() - sizeof(quint16));  
  10.     clientConnection->write(block);  
  11.     clientConnection->flush();  
(6)、讀資料
  1. QDataStream in(clientConnection);  
  2.     // 設定資料流版本,這裡要和伺服器端相同
  3.     in.setVersion(QDataStream::Qt_5_6);  
  4.     in >> blockSize;  
  5.     in >> message;  
3、客戶端

(1)建立QTcpSocket套接字物件

  1. tcpSocket = new QTcpSocket(this);  

(2)使用這個物件連線伺服器

server->listen(QHostAddress::Any, port)  

(3)當伺服器被客戶端訪問時,會發出newConnection()訊號,因此為該訊號新增槽函式,並用一個QTcpSocket物件接受客戶端訪問

  1. connect(server,&QTcpServer::newConnection,this,&MainWindow::server_New_Connect);  
  2. void MainWindow::server_New_Connect()  
  3. {  
  4.     //獲取客戶端連線
  5.     socket = server->nextPendingConnection();  
  6. }  

程式碼:

客戶端

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QAbstractSocket>
class QTcpSocket;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    QList<QString> DataList;
    Ui::MainWindow *ui;
    QTcpSocket *tcpSocket;
    QString message;
    // 用來存放資料的大小資訊
    quint16 blockSize;

private slots:
    void newConnect();
    void readMessage();
    void displayError(QAbstractSocket::SocketError);

    void on_ConPushButton_clicked();
    void on_SendPushButton_clicked();
    void on_disConPushButton_clicked();
    void on_SeeDatabtn_clicked();
    void on_ClearBtn_clicked();
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtNetwork>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->hostLineEdit->setText("127.0.0.1");
    ui->portLineEdit->setText("30000");
    tcpSocket = new QTcpSocket(this);

    connect(tcpSocket, &QTcpSocket::readyRead, this, &MainWindow::readMessage);
    connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(displayError(QAbstractSocket::SocketError)));
}

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

void MainWindow::newConnect(){
    // 初始化資料大小資訊為0
    blockSize = 0;

    // 取消已有的連線
    tcpSocket->abort();
    tcpSocket->connectToHost(ui->hostLineEdit->text(),
                             ui->portLineEdit->text().toInt());
    ui->conn_state->setText("已連線");
}

void MainWindow::readMessage(){
    QDataStream in(tcpSocket);

    // 設定資料流版本,這裡要和伺服器端相同
    in.setVersion(QDataStream::Qt_5_6);


    in >> blockSize;
    // 將接收到的資料存放到變數中
    in >> message;
    // 顯示接收到的資料
    ui->MessageAccept->setText(message);
    if(message != "connect sucessful !"){
        DataList.append(message);
    }
}



void MainWindow::displayError(QAbstractSocket::SocketError)
{
    qDebug() << tcpSocket->errorString();
    ui->conn_state->setText("連線發生錯誤!請重新連線。");
}

void MainWindow::on_ConPushButton_clicked()
{
    newConnect();
}

void MainWindow::on_SendPushButton_clicked()
{
    // 用於暫存要傳送的資料
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);

    // 設定資料流的版本,客戶端和伺服器端使用的版本要相同
    out.setVersion(QDataStream::Qt_5_6);
    out << (quint16)0;
    out << ui->MesslineEdit->text();
    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));

    tcpSocket->write(block);
    tcpSocket->flush();
}

void MainWindow::on_disConPushButton_clicked()
{
    tcpSocket->disconnectFromHost();
    ui->conn_state->setText("未連線");
}

//檢視接收資料
void MainWindow::on_SeeDatabtn_clicked()
{
    for(int i=0;i<DataList.size();i++){
        qDebug() << DataList.at(i);
    }
}

//清空接收資料
void MainWindow::on_ClearBtn_clicked()
{
    DataList.clear();
}

伺服器端

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtNetwork>
class QTcpServer;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QTcpServer *tcpServer;
    QTcpSocket *clientConnection;
    QString message;
    // 用來存放資料的大小資訊
    quint16 blockSize;
    QTimer *timer;

private slots:
    //void acceptMessage();
    void sendMessage();
    void readMessage();

    void displayError(QAbstractSocket::SocketError);
    void on_send_clicked();

    void timerSendMess();
};

#endif // MAINWINDOW_H

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtNetwork>
#include <QTime>
#include <QTimer>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    tcpServer = new QTcpServer(this);
    if((!tcpServer->listen(QHostAddress::LocalHost, 30000))){
        qDebug() << tcpServer->errorString();
        close();
    }
    connect(tcpServer, &QTcpServer::newConnection,
            this, &MainWindow::sendMessage);

    //定時器
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,&MainWindow::timerSendMess);

}

MainWindow::~MainWindow()
{
    clientConnection->close();
    delete ui;
}

//定時傳送資料
void MainWindow::timerSendMess(){
    int rand = qrand() % 30000;
    ui->MessSend->setText(QString::number(rand, 10));

    // 用於暫存要傳送的資料
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);

    // 設定資料流的版本,客戶端和伺服器端使用的版本要相同
    out.setVersion(QDataStream::Qt_5_6);
    out << (quint16)0;
    out << QString::number(rand, 10);
    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));

    clientConnection->write(block);
    clientConnection->flush();
}

void MainWindow::sendMessage(){

    // 獲取已經建立的連線的套接字
    clientConnection = tcpServer->nextPendingConnection();
    QObject::connect(clientConnection, &QTcpSocket::readyRead, this, &MainWindow::readMessage);
    connect(clientConnection, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(displayError(QAbstractSocket::SocketError)));

    // 用於暫存要傳送的資料
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);

    // 設定資料流的版本,客戶端和伺服器端使用的版本要相同
    out.setVersion(QDataStream::Qt_5_6);
    out << (quint16)0;
    out << tr("connect sucessful !");
    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));

    clientConnection->write(block);
    clientConnection->flush();

    ui->waitconn->setText(tr("連線成功!"));
}

void MainWindow::on_send_clicked()
{
    // 用於暫存要傳送的資料
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);

    // 設定資料流的版本,客戶端和伺服器端使用的版本要相同
    out.setVersion(QDataStream::Qt_5_6);
    out << (quint16)0;
    out << ui->MessSend->text();
    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));

    clientConnection->write(block);
    clientConnection->flush();
}

void MainWindow::readMessage(){
    QDataStream in(clientConnection);

    // 設定資料流版本,這裡要和伺服器端相同
    in.setVersion(QDataStream::Qt_5_6);

    in >> blockSize;
    in >> message;

    //讀出內容和大小
    qDebug() << blockSize;
    qDebug() << message;

    if(message=="ACTION"){
        //1s一個數據
        timer->start(300);
    }
    if(message=="STOP"){
        timer->stop();
    }

    ui->MessAccept->setText(message);
}

void MainWindow::displayError(QAbstractSocket::SocketError){
    qDebug() << clientConnection->errorString();
    ui->waitconn->setText("未連線,等待連線");
}