QTfrontend/netudpserver.cpp
changeset 413 523f1769f2bc
child 417 bc7fea9abd9c
equal deleted inserted replaced
412:2ddcc3e3e644 413:523f1769f2bc
       
     1 #include <QUdpSocket>
       
     2 
       
     3 #include "netudpserver.h"
       
     4 
       
     5 #include <QDebug>
       
     6 
       
     7 HWNetUdpServer::HWNetUdpServer(QObject* parent) :
       
     8   QObject(parent)
       
     9 {
       
    10   pUdpSocket = new QUdpSocket(this);
       
    11   pUdpSocket->bind(46631);
       
    12 
       
    13   connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
       
    14   
       
    15 }
       
    16 
       
    17 void HWNetUdpServer::onClientRead()
       
    18 {
       
    19   while (pUdpSocket->hasPendingDatagrams()) {
       
    20     QByteArray datagram;
       
    21     datagram.resize(pUdpSocket->pendingDatagramSize());
       
    22     QHostAddress clientAddr;
       
    23     quint16 clientPort;
       
    24     pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
       
    25     if(QString("%1").arg(datagram.data())==QString("hedgewars client")) {
       
    26       // send answer to client
       
    27       qDebug() << "received UDP query from " << clientAddr << ":" << clientPort;
       
    28       pUdpSocket->writeDatagram("hedgewars server", clientAddr, 46632);
       
    29     }
       
    30   }
       
    31 }