当前位置:   article > 正文

使用QT进行WIFI无线传输数据_qt实现无线网络通信

qt实现无线网络通信

好久没有更新博客了,今天简单写下关于WiFi无线通信进行数据传输的相关内容.
基于TCP/IP协议的通信.代码在文章末尾;具体实现如下:
1.首先win+R 进入命令行,输入ipconfig查看WiFi网卡的IP地址;
2.使用WiFi网址对网关进行ping操作,保证网关可以ping通;
3.在另一台具有WiFi网卡的电脑进行同样操作;
4.两台电脑互ping,保证ping通,若不通检查防火墙是否关闭.
将文末的WiFi程序进行执行,发送即可成功.

#ifndef CLIENT_H
#define CLIENT_H

#include <QWidget>
#include <QTcpSocket>   //通信套接字

namespace Ui {
class Client;
}

class Client : public QWidget
{
    Q_OBJECT

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

public slots:
    void doConnected();
    void receiveMsg();
private slots:
    void on_pbtConnect_clicked();

    void on_pbtSend_clicked();

    void on_pbtClose_clicked();

private:
    Ui::Client *ui;
    QTcpSocket *tcpSocket;
    QString msg;
    QString recMsg;
};

#endif // CLIENT_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
#ifndef SERVICE_H
#define SERVICE_H

#include <QWidget>
#include <QTcpServer>   //监听套接字
#include <QTcpSocket>   //通信套接字




namespace Ui {
class Service;
}

class Service : public QWidget
{
    Q_OBJECT

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

public slots:
    void serverConnect();
    void readMsg();

private slots:
    void on_pbtSend_clicked();

    void on_pbtClose_clicked();

private:
    Ui::Service *ui;
    QTcpServer *tcpServer;
    QTcpSocket *tcpSocket;
    QString msg;
    QString recMsg;
};

#endif // SERVICE_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
#include "client.h"
#include "ui_client.h"
#include <QHostAddress>

#include <QMessageBox>
#include <QDebug>
#define cout qDebug()<<"[" <<__FILE__<<":"<<__LINE__<<"]"
#pragma execution_character_set("utf-8")


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


Client::Client(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Client)
{
    ui->setupUi(this);
    setWindowTitle("客户端");
    recMsg.clear();
    tcpSocket=NULL;
    tcpSocket=new QTcpSocket(this);

    connect(tcpSocket,SIGNAL(connected()),this,SLOT(doConnected()));
    connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(receiveMsg()));

}

void Client::doConnected()
{
    recMsg="成功和服务器连接";
    ui->textBrowser->setText(recMsg);
}

void Client::receiveMsg()
{
    QByteArray array=tcpSocket->readAll();
    QString str=array;
    msg+=str+"\r\n";
    ui->textBrowser->setText(msg);
}

void Client::on_pbtConnect_clicked()
{
    if(tcpSocket==NULL)
        tcpSocket=new QTcpSocket(this);
    QString ip=ui->letIP->text();
    qint16 port=ui->letPort->text().toInt();
    tcpSocket->connectToHost(QHostAddress(ip),port);

}

void Client::on_pbtSend_clicked()
{
    if(tcpSocket==NULL)
    {
        ui->textBrowser->setText("当前无连接");
        return;
    }
    QString str=ui->textEdit->toPlainText();
    tcpSocket->write(str.toUtf8().data());
}


void Client::on_pbtClose_clicked()
{
    if(tcpSocket==NULL)
    {
        ui->textBrowser->setText("当前无连接");
        return;
    }
    //主动和客户端断开连接
    tcpSocket->disconnectFromHost();
    tcpSocket->close();
    tcpSocket=NULL;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
#include "service.h"
#include "ui_service.h"

#include <QMessageBox>
#include <QDebug>
#define cout qDebug()<<"[" <<__FILE__<<":"<<__LINE__<<"]"
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

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



Service::Service(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Service)
{
    ui->setupUi(this);
    msg.clear();
    recMsg.clear();
    tcpServer=NULL;
    tcpSocket=NULL;
    tcpServer=new QTcpServer(this);



    setWindowTitle("服务器");

    tcpServer->listen(QHostAddress::Any,8888);
    connect(tcpServer,SIGNAL(newConnection()),this,SLOT(serverConnect()));



}

void Service::serverConnect()
{
    tcpSocket=tcpServer->nextPendingConnection();
    QString ip=tcpSocket->peerAddress().toString();
    int port=tcpSocket->peerPort();
    QString temp=QString("[%1:%2]:连接成功").arg(ip).arg(port);
    QString title=QString("服务器 ip%1:%2").arg(ip).arg(port);
    setWindowTitle(title);
    msg+=temp+"\r\n";
    ui->textBrowser->setText(msg);
    connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readMsg()));
}

void Service::readMsg()
{
    QByteArray array=tcpSocket->readAll();
    QString str=array;
    msg+=str+"\r\n";
    ui->textBrowser->setText(msg);

}

void Service::on_pbtSend_clicked()
{
    if(tcpSocket==NULL)
    {
        ui->textBrowser->setText("当前无连接");
        return;
    }

    QString str=ui->textEdit->toPlainText();
    tcpSocket->write(str.toUtf8().data());
}


void Service::on_pbtClose_clicked()
{
    if(tcpSocket==NULL)
    {
        ui->textBrowser->setText("当前无连接");
        return;
    }
    //主动和客户端断开连接
    tcpSocket->disconnectFromHost();
    tcpSocket->close();
//    tcpSocket=NULL;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86

在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号