bool Network::openConnection(QString &netInterface) { // Internet Access Point QNetworkConfigurationManager manager; const bool canStartIAP = (manager.capabilities() & QNetworkConfigurationManager::CanStartAndStopInterfaces); // If there is a default access point, use it QNetworkConfiguration cfg = manager.defaultConfiguration(); if (!cfg.isValid() || !canStartIAP) { return false; } // Open session m_session = new QNetworkSession(cfg); m_session->open(); // Waits for session to be open and continues after that m_session->waitForOpened(); // Show interface name to the user QNetworkInterface iff = m_session->interface(); netInterface = iff.humanReadableName(); return true; }
Client::Client(QString purpose) : networkSession(0) { Client::purpose = purpose; tcpSocket = new QTcpSocket; Client::blockSize = 0; qDebug() << connect(tcpSocket, &QTcpSocket::readyRead, this, &Client::readData); //connect(tcpSocket, &QTcpSocket::error, this, &Client::displayError); QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); qDebug() << connect(networkSession, &QNetworkSession::opened, this, &Client::sessionOpened); } qDebug() << "Client set up, waiting"; }
Server::Server() { QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); if (networkSession) { connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); TRACE_INFO(NET, "Opening network session...\n"); networkSession->open(); } } else { sessionOpened(); } fortunes << tr("You've been leading a dog's life. Stay off the furniture.") << tr("You've got to think about tomorrow.") << tr("You will be surprised by a loud noise.") << tr("You will feel hungry again in another hour.") << tr("You might have mail.") << tr("You cannot kill time without injuring eternity.") << tr("Computers are not intelligent. They only think they are."); if (tcpServer) { connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendDatas())); } /* connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addStretch(1); buttonLayout->addWidget(quitButton); buttonLayout->addStretch(1); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(statusLabel); mainLayout->addLayout(buttonLayout); setLayout(mainLayout); setWindowTitle(tr("Fortune Server")); */ }
Server::Server(QObject *parent) : QObject(parent), tcpServer(0), networkSession(0), memBuf(NULL) { QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); QTextStream out(stdout); out << tr("Opening network session."); networkSession->open(); } else { sessionOpened(); } connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendImage())); }
bool TCPClientProducer::init() { assert( m_networkSession == 0 ); assert( m_tcpSocket != 0 ); QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } m_networkSession = new QNetworkSession(config); connect(m_networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); qDebug() << "Opening network session."; m_networkSession->open(); } return true; }
void Server::StartServer() { shotTimer->stop(); is_config_mode = false; emit write_message(tr("Network session starting.")); QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpen())); emit write_message(tr("Opening network session.")); networkSession->open(); } else { sessionOpen(); } connect(tcpServer, SIGNAL(newConnection()), this, SLOT(recieveConnection())); }
BigBlobbyClient::BigBlobbyClient() : networkSession_( NULL ), tcpSocket_( new QTcpSocket( this ) ), portNumber_( DEFAULT_PORT_NUMBER ), log_( logger::FileLogger::instance() ) { connect( tcpSocket_, SIGNAL( readyRead() ), this, SLOT( readBigBlobbyResponse() ) ); connect( tcpSocket_, SIGNAL( error( QAbstractSocket::SocketError ) ), this, SLOT( displayError( QAbstractSocket::SocketError ) ) ); QNetworkConfigurationManager manager; if( manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired ) { // Get saved network configuration QSettings settings( QSettings::UserScope, QLatin1String( "BigBlobby" ) ); settings.beginGroup( QLatin1String( "QtNetwork" ) ); const QString id = settings.value( QLatin1String( "DefaultNetworkConfiguration" ) ).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier( id ); if( (config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered ) { config = manager.defaultConfiguration(); } networkSession_ = new QNetworkSession( config, this ); connect( networkSession_, SIGNAL( opened() ), this, SLOT( sessionOpened() ) ); networkSession_->open(); } }
Server::Server(QWidget *parent) : QDialog(parent), mTcpServer(0), mNetworkSession(0) { QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } mNetworkSession = new QNetworkSession(config, this); connect(mNetworkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); mNetworkSession->open(); } else { sessionOpened(); } mIPAddressMapper = new QSignalMapper; connect(mTcpServer, SIGNAL(newConnection()), this, SLOT(acceptClientConnection())); connect(mIPAddressMapper, SIGNAL(mapped(QString)), this, SIGNAL(clientDisconnected(QString))); }
Server::Server(QWidget *parent) : QDialog(parent) { networkSession=0; tcpServer=0; ui = new Ui::Server; ui->setupUi(this); val=0; ui->quit_button->setAutoDefault(false); QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); networkSession->open(); } else { sessionOpened(); } connect(ui->quit_button, SIGNAL(clicked()), this, SLOT(close())); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(conn())); }
void Loco::RunSlippy () { mainUi.displayMap->SetLocator (locator); // Set Internet Access Point QNetworkConfigurationManager manager; const bool canStartIAP = (manager.capabilities() & QNetworkConfigurationManager::CanStartAndStopInterfaces); // Is there default access point, use it QNetworkConfiguration cfg1 = manager.defaultConfiguration(); if (!cfg1.isValid() || (!canStartIAP && cfg1.state() != QNetworkConfiguration::Active)) { networkSetupError = QString(tr("This example requires networking, " "and no avaliable networks or access points " "could be found.")); QTimer::singleShot(0, this, SLOT(networkSetupError())); return; } netSession = new QNetworkSession(cfg1, this); conHelp = new ConnectivityHelper (netSession, this); connect (netSession, SIGNAL (opened()), this, SLOT (networkSessionOpened ())); connect (conHelp, SIGNAL (networkingCancelled()), this, SLOT (Quit())); }
lc::ClientHelpNotificationServer::ClientHelpNotificationServer( QObject *argParent ) : QObject{ argParent }, hostAddress{ settings->serverIP } { QNetworkConfigurationManager manager; if ( manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired ) { // Get saved network configuration QSettings settings{ QSettings::UserScope, QLatin1String{ "QtProject" } }; settings.beginGroup( QLatin1String{ "QtNetwork" } ); const QString id = settings.value( QLatin1String{ "DefaultNetworkConfiguration" } ).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier( id ); if ( ( config.state() & QNetworkConfiguration::Discovered ) != QNetworkConfiguration::Discovered ) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession{ config, this }; connect( networkSession, &QNetworkSession::opened, this, &ClientHelpNotificationServer::OpenSession ); networkSession->open(); } else { OpenSession(); } connect( helpMessageServer, &QTcpServer::newConnection, this, &ClientHelpNotificationServer::SendReply ); }
TcpEchoServer::TcpEchoServer(quint16 port, QObject *parent) : QObject(parent), tcpServer(0), networkSession(0) { QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); qDebug() << "Opening network session."; networkSession->open(); } else { sessionOpened(port); } }
Server::Server(QWidget *parent) : QDialog(parent), tcpServer(0), clientSocket(0), networkSession(0), blockSize(0) { serverStatusLabel = new QLabel(tr("Status")); messageLabel = new QLabel(tr("Message:")); quitButton = new QPushButton(tr("Quit")); quitButton->setAutoDefault(false); sendMessageButton = new QPushButton(tr("Send Message")); chatBox = new QTextEdit; chatBox->setReadOnly(true); messageLineEdit = new QLineEdit; messageLineEdit->setEnabled(false); QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); serverStatusLabel->setText("Opening network session."); networkSession->open(); } else sessionOpened(); QGridLayout *mainLayout = new QGridLayout; mainLayout->addWidget(serverStatusLabel, 0, 0); mainLayout->addWidget(messageLabel, 2, 0); mainLayout->addWidget(messageLineEdit, 3, 0); mainLayout->addWidget(sendMessageButton, 4, 0); mainLayout->addWidget(chatBox, 5, 0); mainLayout->addWidget(quitButton, 6, 0, 1, 2); setLayout(mainLayout); connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); connect(sendMessageButton, SIGNAL(clicked()), this, SLOT(sendMessage())); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(clientProcessing())); setWindowTitle("Server"); }
Server::Server(/* QWidget *parent */) : /*QDialog(parent),*/ tcpServer(0), networkSession(0) { //statusLabel = new QLabel; //quitButton = new QPushButton(tr("Quit")); //quitButton->setAutoDefault(false); QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); ///statusLabel->setText(tr("Opening network session.")); std::cout << "Opening network session." << std::endl; networkSession->open(); } else { sessionOpened(); } fortunes << tr("bla bla bue ee ee e e.") << tr("You've got to think about tomorrow.") << tr("You wi Q_OBJECTll be surprised by a loud noise.") << tr("You will feel hungry again in another hour.") << tr("You might have mail.") << tr("You cannot kill time without injuring eternity.") << tr("Computers are not intelligent. They only think they are."); ///connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune())); // QHBoxLayout *buttonLayout = new QHBoxLayout; // buttonLayout->addStretch(1); // buttonLayout->addWidget(quitButton); // buttonLayout->addStretch(1); // QVBoxLayout *mainLayout = new QVBoxLayout; // mainLayout->addWidget(statusLabel); // mainLayout->addLayout(buttonLayout); // setLayout(mainLayout); // setWindowTitle(tr("Fortune Server")); }
RSSListing::RSSListing(QWidget *parent) : QWidget(parent), currentReply(0) { #ifdef Q_OS_SYMBIAN // Set Internet Access Point QNetworkConfigurationManager manager; const bool canStartIAP = manager.capabilities() & QNetworkConfigurationManager::CanStartAndStopInterfaces; // Is there default access point, use it QNetworkConfiguration cfg = manager.defaultConfiguration(); if (!cfg.isValid() || !canStartIAP) { // Available Access Points not found QMessageBox::warning(this, "Error", "No access point"); return; } m_session = new QNetworkSession(cfg); m_session->open(); m_session->waitForOpened(); #endif lineEdit = new QLineEdit(this); lineEdit->setText("http://labs.qt.nokia.com/blogs/feed"); fetchButton = new QPushButton(tr("Fetch"), this); treeWidget = new QTreeWidget(this); connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem*,int)), this, SLOT(itemActivated(QTreeWidgetItem*))); QStringList headerLabels; headerLabels << tr("Title") << tr("Link"); treeWidget->setHeaderLabels(headerLabels); treeWidget->header()->setResizeMode(QHeaderView::ResizeToContents); connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*))); connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(fetch())); connect(fetchButton, SIGNAL(clicked()), this, SLOT(fetch())); QVBoxLayout *layout = new QVBoxLayout(this); QHBoxLayout *hboxLayout = new QHBoxLayout; hboxLayout->addWidget(lineEdit); hboxLayout->addWidget(fetchButton); layout->addLayout(hboxLayout); layout->addWidget(treeWidget); setWindowTitle(tr("RSS listing example")); #if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5) resize(640,480); #endif }
MapZoom::MapZoom() : QMainWindow(0) { map = new LightMaps(this); setCentralWidget(map); map->setFocus(); QAction *osloAction = new QAction(tr("&Oslo"), this); QAction *berlinAction = new QAction(tr("&Berlin"), this); QAction *jakartaAction = new QAction(tr("&Jakarta"), this); QAction *nightModeAction = new QAction(tr("Night Mode"), this); nightModeAction->setCheckable(true); nightModeAction->setChecked(false); QAction *osmAction = new QAction(tr("About OpenStreetMap"), this); connect(osloAction, SIGNAL(triggered()), SLOT(chooseOslo())); connect(berlinAction, SIGNAL(triggered()), SLOT(chooseBerlin())); connect(jakartaAction, SIGNAL(triggered()), SLOT(chooseJakarta())); connect(nightModeAction, SIGNAL(triggered()), map, SLOT(toggleNightMode())); connect(osmAction, SIGNAL(triggered()), SLOT(aboutOsm())); QMenu *menu = menuBar()->addMenu(tr("&Options")); menu->addAction(osloAction); menu->addAction(berlinAction); menu->addAction(jakartaAction); menu->addSeparator(); menu->addAction(nightModeAction); menu->addAction(osmAction); QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system // default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); networkSession->open(); } else { networkSession = 0; } setWindowTitle(tr("Light Maps")); }
void FtpClient::init() { qDebug() << "FtpClient:: " << port << " Init"; // if we did not find one, use IPv4 localhost if (ipAddress.isEmpty()) ipAddress = QHostAddress(QHostAddress::LocalHost).toString(); qDebug() << "FtpClient:: " << port << " Init connect"; // from abtractsocket connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(handleError(QAbstractSocket::SocketError))); connect(tcpSocket, SIGNAL(connected()), this, SLOT(handleConnected())); connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(handleDisconnected())); connect(tcpSocket, SIGNAL(hostFound()), this, SLOT(handleHostFound())); connect(tcpSocket, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&,QAuthenticator*)), this, SLOT(handleProxyAuthenticationRequired(const QNetworkProxy&,QAuthenticator*))); connect(tcpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(handleStateChanged(QAbstractSocket::SocketState))); // from QIODevice connect(tcpSocket, SIGNAL(aboutToClose()), this, SLOT(handleAboutToClose())); connect(tcpSocket, SIGNAL(bytesWritten(qint64)), this, SLOT(handleBytesWritten(qint64))); connect(tcpSocket, SIGNAL(readChannelFinished()), this, SLOT(handleReadChannelFinished())); connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(handleReadyRead())); qDebug() << "FtpClient:: " << port << " Init NetworkConfigurationManager manager"; QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { qDebug() << "FtpClient::Init QNetworkConfigurationManager::NetworkSessionRequired"; // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String(SETTING_STR)); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } qDebug() << "FtpClient:: " << port << " Init new QNetworkSession(config, this)"; networkSession = new QNetworkSession(config, this); qDebug() << "FtpClient:: " << port << " Init connect(networkSession, SIGNAL(opened()), this, SLOT(handleSessionOpened()))"; connect(networkSession, SIGNAL(opened()), this, SLOT(handleSessionOpened())); qDebug() << "FtpClient:: " << port << " Init networkSession->open()"; networkSession->open(); } else { qDebug() << "FtpClient:: Init " << port << " tcpSocket->connectToHost())" << " ipAddress " << ipAddress << " port " << port; tcpSocket->connectToHost(QHostAddress(ipAddress), port); } mInitiated = true; }
int main(int argc, char *argv[]) { QApplication app(argc, argv); QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } QNetworkSession *networkSession = new QNetworkSession(config, &app); networkSession->open(); networkSession->waitForOpened(); if (networkSession->isOpen()) { // Save the used configuration QNetworkConfiguration config = networkSession->configuration(); QString id; if (config.type() == QNetworkConfiguration::UserChoice) { id = networkSession->sessionProperty( QLatin1String("UserChoiceConfiguration")).toString(); } else { id = config.identifier(); } QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); settings.beginGroup(QLatin1String("QtNetwork")); settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id); settings.endGroup(); } } //ChatDialog dialog; InitialWindow window; #ifdef Q_OS_SYMBIAN // Make application better looking and more usable on small screen // dialog.showMaximized(); #else // dialog.show(); window.show(); #endif return app.exec(); }
bool Session::ensureNetwork() { QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { if (!s_network) s_network = new QNetworkSession(manager.defaultConfiguration(), qApp); s_network->open(); } // TODO: return value? return true; }
void GuiBehind::initConnection() { // Connection QNetworkConfigurationManager manager; const bool canStartIAP = (manager.capabilities() & QNetworkConfigurationManager::CanStartAndStopInterfaces); QNetworkConfiguration cfg = manager.defaultConfiguration(); if (!cfg.isValid() || (!canStartIAP && cfg.state() != QNetworkConfiguration::Active)) return; mNetworkSession = new QNetworkSession(cfg, this); connect(mNetworkSession, SIGNAL(opened()), this, SLOT(connectOpened())); connect(mNetworkSession, SIGNAL(error(QNetworkSession::SessionError)), this, SLOT(connectError(QNetworkSession::SessionError))); mNetworkSession->open(); }
int main(int argc, char *argv[]) { QApplication a(argc, argv); QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } QNetworkSession *networkSession = new QNetworkSession(config, &a); networkSession->open(); networkSession->waitForOpened(); if (networkSession->isOpen()) { // Save the used configuration QNetworkConfiguration config = networkSession->configuration(); QString id; if (config.type() == QNetworkConfiguration::UserChoice) { id = networkSession->sessionProperty( QLatin1String("UserChoiceConfiguration")).toString(); } else { id = config.identifier(); } QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id); settings.endGroup(); } } interface w; w.show(); return a.exec(); }
Server::Server(QWidget *parent) : QDialog(parent), tcpServer(0), networkSession(0) { statusLabel = new QLabel; quitButton = new QPushButton(tr("Quit")); quitButton->setAutoDefault(false); QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); statusLabel->setText(tr("Opening network session.")); networkSession->open(); } else { sessionOpened(); } fortunes << tr("all in on this"); connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune())); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addStretch(1); buttonLayout->addWidget(quitButton); buttonLayout->addStretch(1); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(statusLabel); mainLayout->addLayout(buttonLayout); setLayout(mainLayout); setWindowTitle(tr("Fortune Server")); }
TCP_Song::TCP_Song(void) { // find out name of this machine QString name = QHostInfo::localHostName(); if (!name.isEmpty()) { qDebug() << name << endl; } // find out IP addresses of this machine QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses(); // add non-localhost addresses for (int i = 0; i < ipAddressesList.size(); ++i) { if (!ipAddressesList.at(i).isLoopback()) qDebug() << ipAddressesList.at(i).toString() << endl; } tcpSocket = new QTcpSocket(this); connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readSongs())); QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); networkSession->open(); } }
MainWindow::MainWindow(const QVariantHash &programOptions, QWidget *parent) : QMainWindow(parent), m_programOptions(programOptions) { // Set Internet Access Point QNetworkConfigurationManager manager; const bool canStartIAP = (manager.capabilities() & QNetworkConfigurationManager::CanStartAndStopInterfaces); // Is there default access point, use it QNetworkConfiguration cfg = manager.defaultConfiguration(); if (!cfg.isValid() || (!canStartIAP && cfg.state() != QNetworkConfiguration::Active)) { QMessageBox::information(0, tr("Map Viewer Demo"), tr( "Available Access Points not found.")); qApp->quit(); return; } bool open_session = true; if (m_programOptions.contains("session")) open_session = m_programOptions["session"].toBool(); else if (m_programOptions.contains("nosession")) open_session = !m_programOptions["nosession"].toBool(); if (open_session) { m_session = new QNetworkSession(cfg, this); connect(m_session, SIGNAL(opened()), this, SLOT(networkSessionOpened())); connect(m_session, SIGNAL(error(QNetworkSession::SessionError)), this, SLOT(networkSessionError(QNetworkSession::SessionError))); m_session->open(); } else { qDebug("Bypassing session..."); networkSessionOpened(); } setWindowTitle("MapViewer"); resize(640, 480); }
Ftp::Ftp(void) : ftp(0), networkSession(0) { QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) config = manager.defaultConfiguration(); networkSession = new QNetworkSession(config, this); connect(networkSession, SIGNAL(opened()), this, SLOT(enableConnect())); connected = false; qDebug() << "FTP: Opening session..."; networkSession->open(); } }
VideoStreamer::VideoStreamer(QObject* parent, quint8 fps, qint16 screen_width, qint16 screen_height, QString hostname, quint16 port) : QObject(parent), m_tcp_socket(new QTcpSocket(this)), m_fps(fps), m_screen_width(screen_width), m_screen_height(screen_height), m_hostname(hostname), m_port(port) { // TODO find out if we really need this QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } m_network_session = new QNetworkSession(config, this); connect(m_network_session, &QNetworkSession::opened, this, &VideoStreamer::sessionOpened); m_network_session->open(); } typedef void (QAbstractSocket::*QAbstractSocketErrorSignal)(QAbstractSocket::SocketError); connect( m_tcp_socket, static_cast<QAbstractSocketErrorSignal>(&QTcpSocket::error), this, &VideoStreamer::socketError); }
Server::Server(QWidget *parent) : QDialog(parent), ui(new Ui::Server), tcpServer(0), clientSocket(0), networkSession(0), blockSize(0) { ui->setupUi(this); statusLabel = ui->statusLabel; connectionLabel = ui->connectionLabel; okButton = ui->okButton; okButton->setEnabled(false); QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { /// Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); /// If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); statusLabel->setText("Открытие сессии."); networkSession->open(); } else sessionOpened(); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection())); setWindowTitle("Настройки сервера"); }
Server::Server(QString ipAddr, quint32 port, QString purpose) : tcpServer(0), networkSession(0) { Server::clientConnection = NULL; Server::purpose = purpose; Server::port = port; QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); //statusLabel->setText(tr("Opening network session.")); networkSession->open(); } else { sessionOpened(); } fortunes << tr("You've been leading a dog's life. Stay off the furniture.") << tr("You've got to think about tomorrow.") << tr("You will be surprised by a loud noise.") << tr("You will feel hungry again in another hour.") << tr("You might have mail.") << tr("You cannot kill time without injuring eternity.") << tr("Computers are not intelligent. They only think they are."); //connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune())); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(openNewConnection())); //connect(tcpServer, &QTcpServer::newConnection, this, &Server::openNewConnection); }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), hotelServer(Q_NULLPTR), networkSession(0), ui(new Ui::MainWindow) { ui->setupUi(this); //sets manager incase something changes QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); connect(networkSession, &QNetworkSession::opened, this, &MainWindow::sessionOpened); networkSession->open(); } else {//opens session straight away sessionOpened();} hotelInfo =tr("Hotel Name: Hampton Inn\n" "Number of available rooms: 50\n" "Number of floors: 10\n" "Number of unavailable rooms: 5"); //hotelInfo set for mock purposes connect(ui->quitButton, &QAbstractButton::clicked, this, &QWidget::close); connect(hotelServer, &QTcpServer::newConnection, this, &MainWindow::sendHotelInfo); setWindowTitle("Hotel Server"); }
Client::Client(QWidget *parent) : QWidget(parent), networkSession(0) { _config = new Config(); state = 1; setWindowTitle(tr("RORI")); _connectionInt = new ConnectionInterface(_config, this); _connectedInt = new ConnectedClass(_config, this); configSsl(); QNetworkConfigurationManager manager; if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { // Get saved network configuration QSettings settings(QSettings::UserScope, QLatin1String("QtProject")); settings.beginGroup(QLatin1String("QtNetwork")); const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString(); settings.endGroup(); // If the saved network configuration is not currently discovered use the system default QNetworkConfiguration config = manager.configurationFromIdentifier(id); if ((config.state() & QNetworkConfiguration::Discovered) != QNetworkConfiguration::Discovered) { config = manager.defaultConfiguration(); } networkSession = new QNetworkSession(config, this); connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened())); _connectionInt->setStatus(tr("Opening network session.")); networkSession->open(); } connect(this, SIGNAL(dataFromClient(QSslSocket*,QString)), this, SLOT(parseJSON(QSslSocket*,QString))); connect(_connectionInt, SIGNAL(connectBtnClicked()), this, SLOT(connectToEntry())); connect(_connectedInt, SIGNAL(sendMessage(QString)), this, SLOT(sendMessage(QString))); _connectionInt->setFixedSize(QApplication::desktop()->width()/3, QApplication::desktop()->height()-50); _connectedInt->setFixedSize(QApplication::desktop()->width()/3, QApplication::desktop()->height()-50); setFixedSize(QApplication::desktop()->width()/3, QApplication::desktop()->height()-50); show(); }