QT之不同主機之間TCP通訊

tianya_team發表於2016-11-23

注意:伺服器端在監聽時,如果要接收其他主機的連線請求,需設定為

QHostAddress::Any

一、客戶端實現

1.tcpclient.h

#ifndef TCPCLIENT_H
#define TCPCLIENT_H

#include <QMainWindow>
#include <QtNetwork>
#include <QtNetwork/QTcpSocket>

namespace Ui {
class TcpClient;
}

class TcpClient : public QMainWindow
{
    Q_OBJECT

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

protected:
    void init();
    void newTcpConnect();     //用於建立伺服器端與客戶端的連線函式

private:
    Ui::TcpClient *ui;
    QTcpSocket *tcpSocket;

private slots:
    void receiveData();       //接收來自服務端的資料
    void displayError(QAbstractSocket::SocketError);
};

#endif // TCPCLIENT_H



2.tcpclient.cpp

#include "tcpclient.h"
#include "ui_tcpclient.h"

#define ip "192.168.1.141"
#define port 8000

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

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


void TcpClient::init()
{
    tcpSocket=new QTcpSocket(this);
    newTcpConnect();
    connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(receiveData()));
    connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),

               this,SLOT(displayError(QAbstractSocket::SocketError)));
}


void TcpClient::receiveData()
{
    QString datas=tcpSocket->readAll();  //接收字串資料
    ui->receiveLineEdit->setText(datas);  //顯示字串資料
}


void TcpClient::newTcpConnect()
{
    tcpSocket->abort();
    tcpSocket->connectToHost(ip,port);
}



void TcpClient::displayError(QAbstractSocket::SocketError)
{
   qDebug()<<tcpSocket->errorString();
   tcpSocket->close();
}



二、服務端實現

1.tcpserver.h

#ifndef TCPSERVER_H
#define TCPSERVER_H

#include <QMainWindow>
#include <QtNetwork>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QTcpServer>
#include <QTimer>>

namespace Ui {
class TcpServer;
}

class TcpServer : public QMainWindow
{
    Q_OBJECT

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

protected:
    void init();      //初始化函式用於完成一些諸如訊號與槽之間的聯絡和變數初始化工作


private slots:

    void on_sendButton_clicked();

    void newListen();         //建立TCP監聽事件

    void acceptConnection();        //接收客戶端連線

    void displayError(QAbstractSocket::SocketError);    //顯示錯誤資訊

private:
    Ui::TcpServer *ui;
    QTcpSocket *tcpSocket;
    QTcpServer *tcpServer;
    QTimer *timer;
};

#endif // TCPSERVER_H

2.tcpserver.cpp

#include "tcpserver.h"
#include "ui_tcpserver.h"

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

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

void TcpServer::init()
{
    timer=new QTimer;
    this->tcpServer=new QTcpServer(this);
    this->tcpSocket=new QTcpSocket(this);

    newListen();

    //當有客戶端連線請求時發出訊號,acceptConnection為訊號處理函式
    connect(tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
    //當tcpSocket在接受客戶端連線時出現錯誤,發出訊號並處裡
    connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),
            this,SLOT(displayError(QAbstractSocket::SocketError)));

}

void TcpServer::on_sendButton_clicked()
{
    this->tcpSocket->write(ui->sendLineEdit->text().toLatin1());
}


void TcpServer::newListen()
{
    if(!tcpServer->listen(QHostAddress::Any,8000))
        {
            qDebug()<<tcpServer->errorString();
            close();
            return;
        }
}


void TcpServer::acceptConnection()
{
    tcpSocket=tcpServer->nextPendingConnection();
}


void TcpServer::displayError(QAbstractSocket::SocketError)
{
    qDebug()<<tcpSocket->errorString();
    tcpSocket->close();
}


三、實現結果


相關文章