Qt编程详解--网络通信之UDP

    一、知识储备

    使用网络模块前要先在.pro文件中添加network
    QT       += core gui network

    QUdpSocket类是Qt对UDP协议加socket的封装
    1、创建QUdpSocket类对象
    2、绑定ip地址和端口号
    3、连接readyRead()信号,当此信号发射后,就表示可以接受数据了
    4、在槽函数中调用readDatagram函数接收数据

qint64 QUdpSocket::readDatagram ( char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0 )

          host:发者的地址
          port:返回时的端口号

    5、返回数据
    

qint64 QUdpSocket::writeDatagram ( const char * data, qint64 size, const QHostAddress & address, quint16 port )

    host和port是readDatagram函数接收到的
    注意:每个QUdpSocket对象都需要绑定一个地址和端口号

二、实现 简单的聊天的功能

客户端通过下面的 lineEdit 发送数据,显示在服务端的 textEdit 上

服务端同理

Qt编程详解--网络通信之UDP_第1张图片Qt编程详解--网络通信之UDP_第2张图片

 client:

//widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT
    
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    QUdpSocket* udpSocket;

private slots:
    void on_pushButton_clicked();
    void recv();
private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    udpSocket = new QUdpSocket;
    udpSocket->bind(QHostAddress("192.168.43.4"),6677);
    connect(udpSocket,SIGNAL(readyRead()),this,SLOT(recv()));
}

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

void Widget::recv()
{
    char buf[256] = {};
    QHostAddress sender=QHostAddress("192.168.43.4");
    quint16 port=6688;
    udpSocket->readDatagram(buf,sizeof(buf),&sender,&port);
    ui->textEdit->append(buf);
}

void Widget::on_pushButton_clicked()
{
    QString msg = ui->lineEdit->text();
    udpSocket->writeDatagram(msg.toStdString().c_str(),msg.size(),QHostAddress("192.168.43.4"),6688);
    ui->lineEdit->setText("");
}

server:

//widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT
    QUdpSocket* udpSocket;
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
public slots:
    void readyRead();
private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    udpSocket = new QUdpSocket;
    udpSocket->bind(QHostAddress("192.168.43.4"),6688);
    connect(udpSocket,SIGNAL(readyRead()),this,SLOT(readyRead()));
}

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

void Widget::readyRead()
{
    qDebug("-------");
    char buf[256] = {};
    QHostAddress sender;
    quint16 port;
    int ret = udpSocket->readDatagram(buf,sizeof(buf),&sender,&port);
    ui->textEdit->append(buf);
}

void Widget::on_pushButton_clicked()
{
    QString msg = ui->lineEdit->text();
    udpSocket->writeDatagram(msg.toStdString().c_str(),msg.size(),QHostAddress("192.168.43.4"),6677);
    ui->lineEdit->setText("");
}

 

你可能感兴趣的:(#,Qt编程详解)