概述
好久没有更新博客了,今天简单写下关于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+"rn";
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+"rn";
ui->textBrowser->setText(msg);
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readMsg()));
}
void Service::readMsg()
{
QByteArray array=tcpSocket->readAll();
QString str=array;
msg+=str+"rn";
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;
}
最后
以上就是平常香水为你收集整理的使用QT进行WIFI无线传输数据的全部内容,希望文章能够帮你解决使用QT进行WIFI无线传输数据所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复