QTfrontend/netudpserver.cpp
changeset 413 523f1769f2bc
child 417 bc7fea9abd9c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/netudpserver.cpp	Wed Feb 07 22:59:49 2007 +0000
@@ -0,0 +1,31 @@
+#include <QUdpSocket>
+
+#include "netudpserver.h"
+
+#include <QDebug>
+
+HWNetUdpServer::HWNetUdpServer(QObject* parent) :
+  QObject(parent)
+{
+  pUdpSocket = new QUdpSocket(this);
+  pUdpSocket->bind(46631);
+
+  connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
+  
+}
+
+void HWNetUdpServer::onClientRead()
+{
+  while (pUdpSocket->hasPendingDatagrams()) {
+    QByteArray datagram;
+    datagram.resize(pUdpSocket->pendingDatagramSize());
+    QHostAddress clientAddr;
+    quint16 clientPort;
+    pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
+    if(QString("%1").arg(datagram.data())==QString("hedgewars client")) {
+      // send answer to client
+      qDebug() << "received UDP query from " << clientAddr << ":" << clientPort;
+      pUdpSocket->writeDatagram("hedgewars server", clientAddr, 46632);
+    }
+  }
+}