UDP通讯只需要是用bind绑定对IP和Port的监听,断开监听可以使用函数
abort()
做实验的时候用自定义类
class UDPClient : public QObject { Q_OBJECT public: explicit UDPClient(QObject *parent = nullptr); public slots: void startClient(); void endClient(); void WriteDatagrams(const QByteArray &datagram, const QHostAddress &host, quint16 port); private slots: void readPendingDatagrams(); private: QUdpSocket *udpSocket; signals: void dataReceive(QByteArray data); };
打算调用abort()函数时却用了UDPClient类创建的对象进行调用:
UDPClient *udpclient; udpclient = new UDPClient(); udpclient->abort();
显然是错的
因为abort()函数是QUdpSocket类的方法。且在本实验中udpSocket是UDPClient的私有成员,需要在UDPClient进行内部调用,如
void UDPClient::endClient() { udpSocket->abort(); }
标签:UDP,UDPClient,void,public,abort,断开,udpSocket,监听,QT From: https://www.cnblogs.com/toriyung/p/17988947