Advertisement
Guest User

Untitled

a guest
May 2nd, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #include <QUdpSocket>
  2. #include <QtDebug>
  3.  
  4. class Connect : public QObject
  5. {
  6. Q_OBJECT
  7. public:
  8. Connect()
  9. {
  10. // linux box
  11. // m_host = QHostAddress("fe80::f66d:4ff:fe02:5ee7%eth0");
  12. // m_dest = QHostAddress("FE80::2F4:B9FF:FE65:52DF%eth0");
  13. // mac box
  14. m_host = QHostAddress("fe80::60c:ceff:fedb:8018%en0");
  15. m_dest = QHostAddress("FE80::2F4:B9FF:FE65:52DF%en0");
  16.  
  17. // m_dest = QHostAddress("192.168.10.1");
  18.  
  19. m_sock = new QUdpSocket(this);
  20. m_port = 6000;
  21. m_sock->bind(QHostAddress::AnyIPv6, m_port);
  22. connect(m_sock, SIGNAL(readyRead()), this, SLOT(readClient()));
  23. };
  24. ~Connect()
  25. {
  26. delete m_sock;
  27. };
  28. void send(QByteArray data)
  29. {
  30. qDebug() << "scope id = " << m_dest.scopeId();
  31. qint64 ret = m_sock->writeDatagram(data, m_dest, m_port);
  32. if (ret != data.size())
  33. {
  34. qDebug() << "oops ret " << ret << " error:" << m_sock->error();
  35. }
  36. else
  37. {
  38. qDebug() << "success";
  39. }
  40. };
  41.  
  42. public slots:
  43. void readClient(void)
  44. {
  45. QUdpSocket *socket = dynamic_cast<QUdpSocket*>(sender());
  46.  
  47. while (socket->state() == QAbstractSocket::BoundState &&
  48. socket->hasPendingDatagrams())
  49. {
  50. QByteArray buffer;
  51. buffer.resize(socket->pendingDatagramSize());
  52. QHostAddress sender;
  53. quint16 senderPort;
  54. socket->readDatagram(buffer.data(), buffer.size(),
  55. &sender, &senderPort);
  56. qDebug() << "receiving " << buffer.size() << " bytes";
  57. }
  58. };
  59.  
  60. private:
  61. QUdpSocket *m_sock;
  62. QHostAddress m_host, m_dest;
  63. quint16 m_port;
  64. };
  65.  
  66. int main(int argc, char **arg)
  67. {
  68. Connect test;
  69. test.send("test data");
  70. return 0;
  71. }
  72.  
  73. #include "main.moc"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement