QTfrontend/netudpserver.cpp
changeset 5146 7b65e83e261d
parent 4976 088d40d8aba2
equal deleted inserted replaced
5145:120f4271f197 5146:7b65e83e261d
    20 #include <QUdpSocket>
    20 #include <QUdpSocket>
    21 
    21 
    22 #include "netudpserver.h"
    22 #include "netudpserver.h"
    23 
    23 
    24 HWNetUdpServer::HWNetUdpServer(QObject *parent, const QString & descr, quint16 port) :
    24 HWNetUdpServer::HWNetUdpServer(QObject *parent, const QString & descr, quint16 port) :
    25   HWNetRegisterServer(parent, descr, port)
    25   HWNetRegisterServer(parent, descr, port),
       
    26   m_descr(descr)
    26 {
    27 {
    27   pUdpSocket = new QUdpSocket(this);
    28   pUdpSocket = new QUdpSocket(this);
    28   pUdpSocket->bind(46631);
    29   pUdpSocket->bind(46631);
    29   connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
    30   connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead()));
    30 }
    31 }
    35     QByteArray datagram;
    36     QByteArray datagram;
    36     datagram.resize(pUdpSocket->pendingDatagramSize());
    37     datagram.resize(pUdpSocket->pendingDatagramSize());
    37     QHostAddress clientAddr;
    38     QHostAddress clientAddr;
    38     quint16 clientPort;
    39     quint16 clientPort;
    39     pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
    40     pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
    40     if(QString("%1").arg(datagram.data())==QString("hedgewars client")) {
    41     if(datagram.startsWith("hedgewars client")) {
    41       // send answer to client
    42       // send answer to client
    42       pUdpSocket->writeDatagram("hedgewars server", clientAddr, clientPort);
    43       pUdpSocket->writeDatagram(QString("hedgewars server\n%1").arg(m_descr).toUtf8(), clientAddr, clientPort);
    43     }
    44     }
    44   }
    45   }
    45 }
    46 }
    46 
    47 
    47 void HWNetUdpServer::unregister()
    48 void HWNetUdpServer::unregister()