Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QUdpSocket>
- #include <QtDebug>
- class Connect : public QObject
- {
- Q_OBJECT
- public:
- Connect()
- {
- // linux box
- // m_host = QHostAddress("fe80::f66d:4ff:fe02:5ee7%eth0");
- // m_dest = QHostAddress("FE80::2F4:B9FF:FE65:52DF%eth0");
- // mac box
- m_host = QHostAddress("fe80::60c:ceff:fedb:8018%en0");
- m_dest = QHostAddress("FE80::2F4:B9FF:FE65:52DF%en0");
- // m_dest = QHostAddress("192.168.10.1");
- m_sock = new QUdpSocket(this);
- m_port = 6000;
- m_sock->bind(QHostAddress::AnyIPv6, m_port);
- connect(m_sock, SIGNAL(readyRead()), this, SLOT(readClient()));
- };
- ~Connect()
- {
- delete m_sock;
- };
- void send(QByteArray data)
- {
- qDebug() << "scope id = " << m_dest.scopeId();
- qint64 ret = m_sock->writeDatagram(data, m_dest, m_port);
- if (ret != data.size())
- {
- qDebug() << "oops ret " << ret << " error:" << m_sock->error();
- }
- else
- {
- qDebug() << "success";
- }
- };
- public slots:
- void readClient(void)
- {
- QUdpSocket *socket = dynamic_cast<QUdpSocket*>(sender());
- while (socket->state() == QAbstractSocket::BoundState &&
- socket->hasPendingDatagrams())
- {
- QByteArray buffer;
- buffer.resize(socket->pendingDatagramSize());
- QHostAddress sender;
- quint16 senderPort;
- socket->readDatagram(buffer.data(), buffer.size(),
- &sender, &senderPort);
- qDebug() << "receiving " << buffer.size() << " bytes";
- }
- };
- private:
- QUdpSocket *m_sock;
- QHostAddress m_host, m_dest;
- quint16 m_port;
- };
- int main(int argc, char **arg)
- {
- Connect test;
- test.send("test data");
- return 0;
- }
- #include "main.moc"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement