一、qt实现服务端发送 .h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QString>
#include <QList>
#include <QHostInfo>
#include <QHostAddress>
#include <QNetworkInterface>
#include <QUdpSocket>
#include <QJsonObject>
#include <QJsonDocument>
const quint16 PORT = 400;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
void getHost();
void sendMessage();
};
#endif
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::getHost()
{
QString localHostName = QHostInfo::localHostName();
qDebug() <<QString::fromLocal8Bit("主机名:") + localHostName;
QHostInfo info = QHostInfo::fromName(localHostName);
QList<QHostAddress> list = info.addresses();
if(!list.isEmpty())
{
qDebug() << "ip:" + list.first().toString();
}
QList<QNetworkInterface> listImpl = QNetworkInterface::allInterfaces();
if(!listImpl.isEmpty())
{
for (int i = 0; i < listImpl.length(); ++i) {
QNetworkInterface interface = listImpl.at(i);
qDebug() << QString::fromLocal8Bit("设备:") + interface.name();
qDebug() << QString::fromLocal8Bit("硬件地址:") + interface.hardwareAddress();
QList<QNetworkAddressEntry> enrtyList = interface.addressEntries();
for (int j = 0; j < enrtyList.length(); ++j) {
QNetworkAddressEntry entry = enrtyList.at(j);
qDebug() << QString::fromLocal8Bit("IP:") + entry.ip().toString();
qDebug() << QString::fromLocal8Bit("子网掩码:") + entry.netmask().toString();
qDebug() << QString::fromLocal8Bit("广播地址:") + entry.broadcast().toString();
}
}
}
}
void MainWindow::sendMessage()
{
QUdpSocket qus;
QByteArray msg = "Hello world!";
QJsonObject json;
json.insert("VideoID", QJsonValue("1"));
json.insert("AlgoID", QJsonValue("2"));
json.insert("DateTime", QJsonValue("3"));
QJsonDocument document;
document.setObject(json);
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
qus.writeDatagram(byteArray, QHostAddress("127.0.0.1"), PORT);
}
void MainWindow::on_pushButton_clicked()
{
sendMessage();
}
二、c#实现发送
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp);
IPAddress serverAddr = IPAddress.Parse("127.0.0.1");
IPEndPoint endPoint = new IPEndPoint(serverAddr, 400);
string text = "{\"AlgoID\":\"2\",\"DateTime\":\"3\",\"VideoID\":\"1\"}";
byte[] send_buffer = Encoding.ASCII.GetBytes(text);
sock.SendTo(send_buffer, endPoint);
}
}
}
qt实现客户端接收 .h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QString>
#include <QList>
#include <QHostInfo>
#include <QHostAddress>
#include <QNetworkInterface>
#include <QUdpSocket>
#include <QJsonParseError>
#include <QJsonObject>
#include <QNetworkDatagram>
const quint16 PORT = 400;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void receive();
private slots:
void on_pushButton_clicked();
private:
QUdpSocket *uSocket;
private:
Ui::MainWindow *ui;
};
#endif
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
uSocket = new QUdpSocket;
uSocket->bind(QHostAddress("127.0.0.1"), PORT);
connect(uSocket, SIGNAL(readyRead()), this, SLOT(receive()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::receive()
{
QByteArray ba;
while(uSocket->hasPendingDatagrams())
{
qDebug()<<"receive!!!";
ba.resize(uSocket->pendingDatagramSize());
uSocket->readDatagram(ba.data(), ba.size());
QJsonParseError json_error;
QJsonDocument doucment_Parse = QJsonDocument::fromJson(ba, &json_error);
if (json_error.error == QJsonParseError::NoError)
{
if (doucment_Parse.isObject())
{
const QJsonObject obj = doucment_Parse.object();
for(auto it=obj.begin();it!=obj.end();it++)
{
qDebug()<<it.key()<<":"<<it.value().toString();
}
}
}
ui->textEdit->append(ba.data());
}
}
void MainWindow::on_pushButton_clicked()
{
ui->textEdit->clear();
}
|