天天看点

在QT中利用socket简单实现两个窗口之间的通信

【重点强调】

一定先在.pro文件中添加:

一、客户端控件名称展示

(控件名称要是看不明白,私信我吧,嗯,小灶小灶,交流交流。。。)

在QT中利用socket简单实现两个窗口之间的通信
在QT中利用socket简单实现两个窗口之间的通信

二、客户端头文件代码展示 clientwidget.h

#ifndef CLIENTWIDGET_H
#define CLIENTWIDGET_H

#include <QWidget>
#include <QDebug>
#include <QTcpSocket>

namespace Ui { class clientwidget; }

class clientwidget : public QWidget
{
    Q_OBJECT

public:
    clientwidget(QWidget *parent = 0);
    ~clientwidget();

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

private slots:
    void button_send();       //发送按钮的槽函数
    void button_close();      //关闭按钮的槽函数
    void button_connect();    //连接按钮的槽函数

};
#endif // CLIENTWIDGET_H

           

三、客户端.cpp文件展示 clientwidget.cpp

#include "clientwidget.h"
#include "ui_clientwidget.h"
#include <QHostAddress>

clientwidget::clientwidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::clientwidget)
{
    ui->setupUi(this);
    tcpSocket=NULL;
    //1.创建用于通信的套接字
    tcpSocket = new QTcpSocket(this);
    //设置窗口名称
    setWindowTitle("客户端");

    //建立三个按钮的槽函数
    connect(ui->pushButton_connect,SIGNAL(clicked()),this,SLOT(button_connect()));
    connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(button_send()));
    connect(ui->pushButton_close,SIGNAL(clicked()),this,SLOT(button_close()));

    //2.建立套接字连接
    connect(tcpSocket,&QTcpSocket::connected,
            [=]()
    {
        ui->textEdit_read->setText("连接成功!");
    }
    );
    connect(tcpSocket,&QTcpSocket::readyRead,
            [=]()
    {
        //获取对方发来的数据
        QByteArray drray = tcpSocket->readAll();
        ui->textEdit_read->append(drray);
    }
    );
}
//客户端析构函数
clientwidget::~clientwidget()
{
    delete ui;
}
//发送按钮的槽函数
void clientwidget::button_send()
{
    //获取编辑的内容
    QString str = ui->textEdit_send->toPlainText();
    //发送编辑的内容
    tcpSocket->write(str.toUtf8().data());
    //清空输入文本的地方
    ui->textEdit_send->clear();
}
//关闭按钮的槽函数
void clientwidget::button_close()
{
    //主动和服务器断开连接
    tcpSocket->disconnectFromHost();
    tcpSocket->close();
}
//连接按钮的槽函数
void clientwidget::button_connect()
{
    //获取输入的ip和port
    QString ip = ui->lineEdit_ip->text();
    qint16 port  = ui->lineEdit_port->text().toInt();
    //连接主机
    tcpSocket->connectToHost(QHostAddress(ip),port);
}
           

四、服务器端控件名称展示

在QT中利用socket简单实现两个窗口之间的通信
在QT中利用socket简单实现两个窗口之间的通信

五、服务器端头文件展示 serwidget.h

#ifndef SERWIDGET_H
#define SERWIDGET_H

#include <QWidget>
#include <QDebug>
#include <QTcpSocket>
#include <QTcpServer>

namespace Ui {
class serwidget;
}

class serwidget : public QWidget
{
    Q_OBJECT

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

private:
    Ui::serwidget *ui;
    QTcpServer *tcpServer;  //用于监听的套接字
    QTcpSocket *tcpSocket;  //用于通信的套接字

private slots:
    void button_send();    //发送按钮的槽函数
    void button_close();   //关闭按钮的槽函数
};

#endif // SERWIDGET_H

           

六、服务器端.cpp文件展示 serwidget.cpp

#include "serwidget.h"
#include "ui_serwidget.h"
#include <QHostAddress>

serwidget::serwidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::serwidget)
{
    tcpServer=NULL;
    tcpSocket=NULL;

    ui->setupUi(this);

    //建立两个按钮的槽函数
    connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(button_send()));
    connect(ui->pushButton_close,SIGNAL(clicked()),this,SLOT(button_close()));

    //监听套接字,指定父对象,自动回收空间
    tcpServer = new QTcpServer(this);
    tcpServer->listen(QHostAddress::Any,8888);
    //设置窗口名称
    setWindowTitle("服务器:8888");
    //设置通信连接
    connect(tcpServer,&QTcpServer::newConnection,
            [=]()
    {
        //取出建立好的连接,填充用于通信的套接字
        tcpSocket = tcpServer->nextPendingConnection();
        //获取对方的ip和port
        QString ip = tcpSocket->peerAddress().toString();
        qint16 port = tcpSocket->peerPort();
        QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port);
        ui->textEdit_read->setText(temp);
        //读取内容的套接字连接
        connect(tcpSocket,&QTcpSocket::readyRead,
                [=]()
        {
            QByteArray drray = tcpSocket->readAll();
            ui->textEdit_read->append(drray);
        });
    });
}

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

void serwidget::button_send()
{
    //判断套接字是否正确
    if(tcpSocket==NULL)
        return ;
    //获取缓冲区的内容
    QString str = ui->textEdit_send->toPlainText();
    //使用套接字发送数据
    tcpSocket->write(str.toUtf8().data());
    ui->textEdit_send->clear();
}

void serwidget::button_close()
{
    //判断套接字是否存在
    if(tcpSocket==NULL)
        return ;
    tcpSocket->disconnectFromHost();
    tcpSocket->close();
    tcpSocket=NULL;
}

           

七、main.cpp代码展示

#include "clientwidget.h"
#include "serwidget.h"
#include <QApplication>

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

``

           

继续阅读