好久没有更新博客了,今天简单写下关于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
#include //通信套接字
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
#include //监听套接字
#include //通信套接字
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
#include
#include
#define cout qDebug()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
#include
#define cout qDebug()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;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201209150357288.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNjU1OTY3,size_16,color_FFFFFF,t_70#pic_center)
|