UdpServer::UdpServer() : out(stdout, QIODevice::WriteOnly) { socket = new QUdpSocket(this); connect(socket, SIGNAL(readyRead()), this, SLOT(readDatagrams())); connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(handleError())); QList< QHostAddress > addresses = QHostInfo::fromName( QHostInfo::localHostName() ).addresses(); foreach ( const QHostAddress & a, addresses ){ QString protocol = "???"; switch ( a.protocol() ){ case QAbstractSocket::IPv4Protocol: protocol = "IPv4"; break; case QAbstractSocket::IPv6Protocol: protocol = "IPv6"; break; case QAbstractSocket::UnknownNetworkLayerProtocol: protocol = "err"; break; } if(protocol == "IPv4"){ address = a.toString(); break; } }
void Pult::setNetworkData(char *ip, int port) { udpSocket = new QUdpSocket(this); myHost = QString(ip); myPort = port; udpSocket->bind(myHost, myPort); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readDatagrams())); }
BacnetUdpTransportLayerHandler::BacnetUdpTransportLayerHandler(QObject *parent) : QObject(parent), _socket(new QUdpSocket(this)), _bvllHndlr(0) { connect(_socket, SIGNAL(readyRead()), this, SLOT(readDatagrams())); //make space for _datagrams to be received _datagram.resize(Bacnet::BvllMaxSize); }
RFIDMonitorDaemon::RFIDMonitorDaemon(QObject *parent) : QObject(parent), m_localServer(0), m_tcpSocket(0), m_tcpAppSocket(0), isConnected(false) { m_configManager = new ConfigManager(this); m_localServer = new QLocalServer(this); m_tcpSocket = new QTcpSocket(this); m_tcpSocket->setObjectName("server"); m_udpSocket = new QUdpSocket(this); m_tcpAppSocket = new QTcpSocket(this); m_tcpAppSocket->setObjectName("deskApp"); m_serverName = "RFIDMonitorDaemon"; m_restoreTimer.setInterval(10000); connect(&m_restoreTimer, &QTimer::timeout, [=](){ m_configManager->restoreConfig(); initMonitor(); }); connect(m_localServer, SIGNAL(newConnection()), SLOT(ipcNewConnection())); connect(m_tcpSocket, SIGNAL(connected()), SLOT(tcpConnected())); connect(m_tcpSocket, SIGNAL(disconnected()), SLOT(tcpDisconnected())); connect(m_tcpSocket, SIGNAL(readyRead()), SLOT(routeTcpMessage())); connect(m_tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(tcpHandleError(QAbstractSocket::SocketError))); connect(m_udpSocket, SIGNAL(readyRead()), SLOT(readDatagrams())); connect(m_tcpAppSocket, &QTcpSocket::connected, ([=] () { m_udpSocket->close(); qDebug() << "Connected with DeskApp";})); connect(m_tcpAppSocket, &QTcpSocket::disconnected, ([=] () { qDebug() << "DeskApp Connection Closed"; if(!m_udpSocket->bind(QHostAddress::Any, 9999)){ qDebug() << QString("Couldn't listening broadcast"); }; })); connect(m_tcpAppSocket, SIGNAL(readyRead()), SLOT(routeTcpMessage())); QString socketFile = QString("/tmp/%1").arg(m_serverName); if(QFile::exists(socketFile)){ QString rmCommand = QString("rm -f %1").arg(socketFile); system(rmCommand.toStdString().c_str()); } }
Discover::Discover(QObject *parent, Mode mode, QHostAddress multicastAddress, quint16 multicastPort) : QObject(parent) { mServerMode = (mode == ServerMode); port = multicastPort; groupAddress = multicastAddress; announcePeriodMsec = 500; running = false; announceNeedsResponse = true; departure = false; defaultScope = "Local"; setLoggerTag("Discover"); // Timers timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(announceRecords())); expireTimer = new QTimer(this); connect(expireTimer, SIGNAL(timeout()), this, SLOT(expireRecords())); // Server mode? if (mServerMode) { // Server's don't announce periodically timer->stop(); } else { // Announce it immediately after returning to event loop if (running) timer->start(0); } // Loopback socket // loopbackSocket = new QUdpSocket(this); // connect(loopbackSocket, SIGNAL(readyRead()), this, SLOT(readDatagrams())); // if (not loopbackSocket->bind(QHostAddress::LocalHost, port, QAbstractSocket::ShareAddress | QAbstractSocket::ReuseAddressHint)) // { // qlogDebug() << "Error binding loopbackSocket:" << loopbackSocket->errorString(); // } // Global socket globalSocket = new QUdpSocket(this); connect(globalSocket, SIGNAL(readyRead()), this, SLOT(readDatagrams())); if (mServerMode) logDebug(QString("Discover global server mode on port %1").arg(port)); if (not globalSocket->bind(QHostAddress::AnyIPv4, mServerMode?port:0)) { logWarning(QString("Error binding globalSocket: %1").arg(globalSocket->errorString())); } }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QSettings settings(QString("../config.ini"), QSettings::IniFormat); network_port = settings.value("settings/network_port", "7003").toInt(); bit_error_rate = settings.value("settings/bit_error_rate", 10).toInt(); socket = new QUdpSocket(this); socket->bind(QHostAddress::AnyIPv4, network_port); connect(socket, SIGNAL(readyRead()), this, SLOT(readDatagrams())); tx_socket = new QUdpSocket(this); connect(this, SIGNAL(valueChanged(QString)), ui->textBrowser, SLOT(append(QString))); QTime time = QTime::currentTime(); qsrand((uint)time.msec()); }
void Discover::announceRecords () { // Reset timer timer->start(announcePeriodMsec); if (not running) return; // Servers don't announce periodically or send departure messages if (mServerMode) return; // Start building a list of records to send in each scope QList<Record> recordsToSend; /////////////////////////////////////////////////////////////////////// // Global scope first /////////////////////////////////////////////////////////////////////// for (Record record : ownedRecords) if (record["Scope"] == "Global") { recordsToSend.push_back(record); } if (not recordsToSend.empty() or defaultScope=="Global") { // This is a discovery request which should be responded to QByteArray datagram("DSDR"); if (departure) datagram = "DSDD"; datagram += makeDatagram(recordsToSend, false); // Send to each Global server for (QPair<QHostAddress,quint16> globalServer : globalServers) { globalSocket->writeDatagram(datagram, globalServer.first, globalServer.second); } } /////////////////////////////////////////////////////////////////////// // Local scope next /////////////////////////////////////////////////////////////////////// for (Record record : ownedRecords) if (record["Scope"] == "Local") { recordsToSend.push_back(record); } if (not recordsToSend.empty() or defaultScope=="Global" or defaultScope=="Local") { QByteArray datagram("DSDA"); if (announceNeedsResponse) datagram = "DSDR"; if (departure) datagram = "DSDD"; datagram += makeDatagram(recordsToSend, true); // For each interface for (auto iface : QNetworkInterface::allInterfaces()) { // For each IPv4 address on this interface for (auto entry : iface.addressEntries()) { if (entry.ip().protocol() != QAbstractSocket::IPv4Protocol) continue; // Add it to addressEntryCache if necessary for local scope testing if (not addressEntryCache.contains(entry)) addressEntryCache.push_back(entry); // Multicast if (iface.flags().testFlag(QNetworkInterface::CanMulticast) and !entry.ip().isNull() and !entry.ip().isLoopback()) { // Create the socket if it doesn't exit yet if (not multiSocket.contains(entry.ip().toString())) { logDebug(QString("New multicast socket: %1 %2").arg(entry.ip().toString(), entry.netmask().toString())); // Add it, create and bind the socket QUdpSocket* socket = new QUdpSocket(this); connect(socket, SIGNAL(readyRead()), this, SLOT(readDatagrams())); socket->setSocketOption(QAbstractSocket::MulticastTtlOption, 1); if (not socket->bind(QHostAddress::AnyIPv4, port, QAbstractSocket::ShareAddress | QAbstractSocket::ReuseAddressHint)) { logWarning(QString("Error binding to iface %1: %2").arg(entry.ip().toString(), socket->errorString())); } socket->setMulticastInterface(iface); socket->joinMulticastGroup(groupAddress, iface); multiSocket.insert(entry.ip().toString(), socket); } // Send datagram multiSocket[entry.ip().toString()]->writeDatagram(datagram, groupAddress, port); } } } } /////////////////////////////////////////////////////////////////////// // Loopback scope last /////////////////////////////////////////////////////////////////////// for (Record record : ownedRecords) if (record["Scope"] == "Loopback") { recordsToSend.push_back(record); } if (not recordsToSend.empty() or true) // Any scope is above or equivalent to loopback { QByteArray datagram("DSDA"); if (announceNeedsResponse) datagram = "DSDR"; if (departure) datagram = "DSDD"; datagram += makeDatagram(recordsToSend, true); // Loopback //loopbackSocket->writeDatagram(datagram, QHostAddress::LocalHost, port); } // Reset announceNeedsResponse = false; departure = false; }
void Dialog::setConnectionData() { this->setWindowTitle(QString::number(fromPort)); connect(socket, SIGNAL(readyRead()), this, SLOT(readDatagrams())); }