使用QT进行WIFI无线传输数据

好久没有更新博客了,今天简单写下关于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

#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

#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;
}

#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;
}

在这里插入图片描述

  • 7
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值