QTfrontend/netudpwidget.cpp
changeset 413 523f1769f2bc
child 414 52e6be3a0985
equal deleted inserted replaced
412:2ddcc3e3e644 413:523f1769f2bc
       
     1 #include <QUdpSocket>
       
     2 #include <QListWidget>
       
     3 
       
     4 #include "netudpwidget.h"
       
     5 
       
     6 #include <QDebug>
       
     7 
       
     8 HWNetUdpWidget::HWNetUdpWidget(QWidget* parent) :
       
     9   QWidget(parent), mainLayout(this)
       
    10 {
       
    11   serversList = new QListWidget(this);
       
    12   mainLayout.addWidget(serversList);
       
    13   pUdpSocket = new QUdpSocket(this);
       
    14 
       
    15   pUdpSocket->bind();//46632);
       
    16   connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
       
    17   pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631);
       
    18 }
       
    19 
       
    20 void HWNetUdpWidget::onClientRead()
       
    21 {
       
    22   while (pUdpSocket->hasPendingDatagrams()) {
       
    23     QByteArray datagram;
       
    24     datagram.resize(pUdpSocket->pendingDatagramSize());
       
    25     QHostAddress clientAddr;
       
    26     quint16 clientPort;
       
    27     pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
       
    28     if(QString("%1").arg(datagram.data())==QString("hedgewars server")) {
       
    29       qDebug() << "detected server at " << clientAddr << ":" << clientPort;
       
    30       serversList->addItem(clientAddr.toString());
       
    31     }
       
    32   }
       
    33 }