diff --git a/UDPExamples/UDPDaemon.cpp b/UDPExamples/UDPDaemon.cpp index b7620ec90..2044bfb00 100644 --- a/UDPExamples/UDPDaemon.cpp +++ b/UDPExamples/UDPDaemon.cpp @@ -17,12 +17,14 @@ #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -212,6 +214,42 @@ private: QHash clients_; }; +void list_interfaces () +{ + for (auto const& net_if : QNetworkInterface::allInterfaces ()) + { + if (net_if.flags () & QNetworkInterface::IsUp) + { + std::cout << net_if.humanReadableName ().toStdString () << ":\n" + " id: " << net_if.name ().toStdString () << " (" << net_if.index () << ")\n" + " addr: " << net_if.hardwareAddress ().toStdString () << "\n" + " flags: "; + if (net_if.flags () & QNetworkInterface::IsRunning) + { + std::cout << "Running "; + } + if (net_if.flags () & QNetworkInterface::CanBroadcast) + { + std::cout << "Broadcast "; + } + if (net_if.flags () & QNetworkInterface::CanMulticast) + { + std::cout << "Multicast "; + } + if (net_if.flags () & QNetworkInterface::IsLoopBack) + { + std::cout << "Loop-back "; + } + std::cout << "\n addresses:\n"; + for (auto const& ae : net_if.addressEntries ()) + { + std::cout << " " << ae.ip ().toString ().toStdString () << '\n'; + } + std::cout << '\n'; + } + } +} + #include "UDPDaemon.moc" int main (int argc, char * argv[]) @@ -232,6 +270,11 @@ int main (int argc, char * argv[]) auto help_option = parser.addHelpOption (); auto version_option = parser.addVersionOption (); + QCommandLineOption list_option (QStringList {"l", "list-interfaces"}, + app.translate ("UDPDaemon", + "Print the available network interfaces.")); + parser.addOption (list_option); + QCommandLineOption port_option (QStringList {"p", "port"}, app.translate ("UDPDaemon", "Where is the UDP service port number to listen on.\n" @@ -257,6 +300,12 @@ int main (int argc, char * argv[]) parser.process (app); + if (parser.isSet (list_option)) + { + list_interfaces (); + return EXIT_SUCCESS; + } + Server server {static_cast (parser.value (port_option).toUInt ()) , QHostAddress {parser.value (multicast_addr_option).trimmed ()} , parser.values (network_interface_option)};