01 #include <QtCore/QCoreApplication>
02 #include <QtCore/QSignalMapper>
03 #include <QtCore/QStringList>
04 #include <QtNetwork/QNetworkInterface>
05 #include <QtNetwork/QHostAddress>
06 #include <QtNetwork/QTcpServer>
07 #include <iostream>
08 #include "greeter.h"
09
10
11 int main(int argc, char *argv[])
12 {
13   QCoreApplication app(argc, argv);
14   int port = app.arguments().at(1).toInt();
15
16   Greeter* greeter = new Greeter(&app);
17
18   QSignalMapper* sigMap;
19   sigMap = new QSignalMapper(&app);
20   greeter->connect(sigMap,
21            SIGNAL(mapped(QObject *)),
22            SLOT(newConnection(QObject *)));
23
24   QList<QTcpServer> servers;
25
26   QList<QNetworkInterface> ifs;
27   ifs = QNetworkInterface::allInterfaces();
28
29   foreach(const QNetworkInterface& i, ifs) {
30     QList<QNetworkAddressEntry> entries;
31     entries = i.addressEntries();
32
33     foreach(const QNetworkAddressEntry& entry, entries) {
34       QHostAddress address = entry.ip();
35
36       // fix scope of link-local addresses
37       Q_IPV6ADDR addr6;// = address.toIPv6Address();
38       addr6 = address.toIPv6Address();
39       if (addr6[0] == 0xfe &&
40           addr6[1] == 0x80) {
41         QString name=i.humanReadableName();
42         address.setScopeId(name);
43       }
44
45       QTcpServer* server;
46       server = new QTcpServer(&app);
47       sigMap->setMapping(server, server);
48       sigMap->connect(server,
49                   SIGNAL(newConnection()),
50                   SLOT(map()));
51
52       server->listen(address, port);
53       if (!server->isListening()) {
54         std::cout << "Cannot listen on "
55                   << address.toString().toAscii().constData() << std::endl;
56       }
57     }
58   }
59
60   return app.exec();
61 }