QUdpSocket

#include<QHostInfo>

#include<QNetworkInterface>

#include<QUdpSocket>

#include<QByteArray>

int  Port = 5556 ; port number is defined

The sender:

udpSend=new QUdpSocket;

// Bing Binding I P and a port number, an I represents the present machine by P and the port number to send data (I P the machine must have a)

udpSend->bind(QHostAddress("192.168.0.34"),port);

QString str=ui->lineEdit_3->text();

Arr = Strktolatinl ();

// send data out parameters: content data, data length, the other ( receiver side ) the I P address and port

intn=udpSend->writeDatagram(arr,str.length(),QHostAddress("192.168.0.255"),port);

 

Receiving end

udpReceive=new QUdpSocket;

// Bind I P port, only to realize that I sent to P data port will be received

udpReceive->bind(QHostAddress("192.168.0.231"),port);

// connect signals and slots, as long as datagrams over, it will trigger slot function

connect(udpReceive,SIGNAL(readyRead()),this,SLOT(dataReceived()));

void  Form :: dataReceived ()

{

    the while ( udpReceive -> hasPendingDatagrams ()) // determines whether there is data packets readable

    {

        The QByteArray  Datagram; // container used to hold the data packets

// get the length of the first datagram

        datagram.resize(udpReceive->pendingDatagramSize());

// save data packets into the container, easy to operate 

       udpReceive->readDatagram(datagram.data(),datagram.size());

        ui->textEdit->setText(datagram.data());

    }

}

 

 

Packet size does not exceed 6 4KB

Transmitting data structure

#pragma  Pack (. 1) byte alignment

#pragma  pack ()

 

INFO_SCAN  information;

        memset(&info,0,sizeof(info));

        m_Socket->readDatagram((char*)&info,sizeof(info));

  INFO_SCAN  information;

    info.orderType=TELESOFTWARE_CLOSE;

    memset(&info.softwareName,0,sizeof(info.softwareName));

    QString str=ui->software_name->text();

    Strchpy (Info. Sofatvrenme , Strktolatinl ());

 

    int n=m_Socket->writeDatagram((char*)&info,sizeof(info),QHostAddress(ui->broadcast_address->text()),PORT);

    

Published 104 original articles · won praise 22 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_41672557/article/details/103450168