UDP programming learning and QtUDP programming learning in Qt

Source: Internet
Author: User
Tags constant definition

UDP programming learning and QtUDP programming learning in Qt
I. Overview: # include <QUdpSocket> the QUdpSocket class inherits from q1_actsocket. All functions in this class are reentrent ).
II. Introduction: QUdpSocket public type:

EnumBindFlag {Invalid Address, DontShareAddress, ReuseAddressHint, DefaultForPlatform} flagsBindModeQUdpSocket public function: QUdpSocket (QObject * parent = 0) virtual ~ QUdpSocket () boolbind (const QHostAddress & address, quint16 port) boolbind (const QHostAddress & address, quint16 port, BindMode mode) boolbind (quint16 port = 0) boolbind (quint16 port, bindMode) boolhasPendingDatagrams () terminate (const QHostAddress & groupAddress, const QNetworkInterface & iface) terminate (const QHostAddress & groupAddress, const QNetworkInterface & iface) Evaluate () constqint64readDatagram (char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0) voidsetMulticastInterface (const QNetworkInterface & iface) qint64writeDatagram (const char * data, qint64 size, const QHostAddress & address, quint16 port) qint64writeDatagram (const QByteArray & datarray, const QHostAddress & host, quint16 port)

QUdpSocket Description: QUdpSocket provides the UDP Socket API to receive and send UDP datagram. The most common usage of the QUdpSocket class is to bind an IP address and Port with the bind () function, and then call the writedatax () and readdatax () functions to transmit data. To use functions such as read (), readLine (), and write () in QIODevice, you must first call the connectToHost () function to establish a connection with the other party.
As long as a datagram is written to the network, the SOCKET generates a bytesWritten () signal. If only the datagram is sent, you do not need to call bind (). When the datagram arrives, the readyRead () signal is generated. At this time, the hasPendingDatagrams () function returns true ). Call pendingDatagramSize () to obtain the length (size) of the First datagram, and readdatasync () to read the content of the datagram.
Note: After receiving the readyRead () signal, read the datagram. Otherwise, the next datagram will not generate the readyRead () signal.
Example:void Server::initSocket(){     udpSocket = new QUdpSocket(this);     udpSocket->bind(QHostAddress::LocalHost, 7755);     connect(udpSocket, SIGNAL(readyRead()),             this, SLOT(readPendingDatagrams()));}void Server::readPendingDatagrams(){     while (udpSocket->hasPendingDatagrams()) {         QByteArray datagram;         datagram.resize(udpSocket->pendingDatagramSize());         QHostAddress sender;         quint16 senderPort;         udpSocket->readDatagram(datagram.data(), datagram.size(),                                 &sender, &senderPort);         processTheDatagram(datagram);     }}

The QUdpSocket class also supports UPD multicast (multicast ). JoinMulticastGroup () and leaveMulticastGroup () are used to control group members. qmeanactsocket: secure and qmeanactsocket: MulticastLoopbackOption are used to set the TTL and loopback options of the socket respectively. setMulticastInterface () set an external interface for multicast datagram. multicastInterface () is used to obtain the interface type: IP_MULTICAST_IF corresponds to IPv4, IPV6_MULTICAST_IFIPv6.
Through the QUdpSocket class, you can also use connectToHost () to establish a virtual connection with the UDP server, and then use read () and write () to exchange data, instead of specifying a receiver for each datagram ).
Iii. Member logo meaning:
enum QUdpSocket::BindFlagflags QUdpSocket::BindMode
BindFlag values can form different flags and pass them to the QUdpSocket: bind () function to modify the features of bind. BindMode is typedef for QFlags <BindFlag>. It is a BindFlag value or operation.
Constant Definition Value Description
QUdpSocket: Invalid Address 0x1 1. allow other services to bind the same address and port
2. It is useful when multiple processes listen to the same address and port to share the load of a single service (for example: a web server with several preset listeners can improve the response time ). However, since any service allows rebind, this option should cause some security considerations.
3. It should be noted that combining this option with ReuseAddressHint will also allow your service to rebind an existing shared address.
4. On Unix, this option is equivalent to SO_REUSEADDR. On Windows, this option is ignored.
QUdpSocket: dont#address 0x2 1. Use a proprietary method to bind an address and port. Other services cannot be rebound. 2. Use this option to ensure the binding is successful, the specified service will be the only listener of the address and port. Even services with a ReuseAddressHint are not allowed to be rebound. 3. In terms of security, this option is better than the specified address, however, in some operating systems, administrator permissions are required to run. 4. On Unix and Mac OS, the default behavior of binding addresses and ports is not shared. Therefore, this option is ignored; in Windows, it is equivalent to SO_EXCLUSIVEADDRUSE socket options.
QUdpSocket: ReuseAddressHint 0x4 1. Provide a prompt for QUdpSocke, that is, when the address and port have been bound by other sockets, try to rebind 2. On Unix, this option is ignored; similar to SO_REUSEADDR socket options in Windows
QUdpSocket: DefaultForPlatform 0x0 1. Default Option of the current platform 2. On Unix and Mac OS, this option is equivalent to DontShareAddress + ReuseAddressHint; in Windows, it is equivalent to external address.

Qt udp Communication Programming

"Because the communication is between two programs on the local machine, the two IP addresses in my program can both be written to the local machine, but cannot communicate, and the conn variable is 0"

What does communication mean? Is there an error message?
 
Qt udp Socket programming

This is too professional. Go to your place and find a professional to ask in person.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.