#include <QtGui>
#include <QtNetwork>
#include "client.h"
//! [0]
{
hostLabel
= new QLabel(tr
("&Server name:"));
portLabel
= new QLabel(tr
("S&erver port:"));
// find out which IP to connect to
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i) {
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
break;
}
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())
hostLabel->setBuddy(hostLineEdit);
portLabel->setBuddy(portLineEdit);
statusLabel
= new QLabel(tr
("Run Server as well"));
btnConnect->setDefault(true);
btnConnect->setEnabled(false);
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(hostLineEdit,
SIGNAL(textChanged
(QString)),
this,
SLOT(enablebtnConnect
()));
connect(portLineEdit,
SIGNAL(textChanged
(QString)),
this,
SLOT(enablebtnConnect
()));
connect(btnConnect, SIGNAL(clicked()), this, SLOT(newconnect()));
connect(timer, SIGNAL(timeout()),this, SLOT(requestNewFortune()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
mainLayout->addWidget(hostLabel, 0, 0);
mainLayout->addWidget(hostLineEdit, 0, 1);
mainLayout->addWidget(portLabel, 1, 0);
mainLayout->addWidget(portLineEdit, 1, 1);
mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
mainLayout->addWidget(btnConnect, 3, 0);
mainLayout->addWidget(quitButton, 3, 1);
setLayout(mainLayout);
setWindowTitle(tr("Fortune client"));
portLineEdit->setFocus();
QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
// Get saved network configuration
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()));
btnConnect->setEnabled(false);
statusLabel->setText(tr("Opening network session."));
networkSession->open();
}
}
void client::newconnect()
{
timer->start();
}
void client::requestNewFortune()
{
btnConnect->setEnabled(false);
blockSize = 0;
tcpSocket->abort();
tcpSocket->connectToHost(hostLineEdit->text(),
portLineEdit->text().toInt());
}
void client::readFortune()
{
//! [9]
if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
//! [8]
//! [10]
in >> blockSize;
}
if (tcpSocket->bytesAvailable() < blockSize)
return;
//! [10] //! [11]
in >> nextFortune;
if (nextFortune == joyvalue) {
QTimer::singleShot(0,
this,
SLOT(requestNewFortune
()));
return;
}
//! [11]
//! [12]
joyvalue = nextFortune;
//! [9]
statusLabel->setText(joyvalue);
btnConnect->setEnabled(true);
}
{
switch (socketError) {
break;
tr("The host was not found. Please check the "
"host name and port settings."));
break;
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
tr("The following error occurred: %1.")
.arg(tcpSocket->errorString()));
}
btnConnect->setEnabled(true);
}
void client::enablebtnConnect()
{
btnConnect->setEnabled((!networkSession || networkSession->isOpen()) &&
!hostLineEdit->text().isEmpty() &&
!portLineEdit->text().isEmpty());
btnConnect->setText("Connected");
}
void client::sessionOpened()
{
// Save the used configuration
QNetworkConfiguration config = networkSession->configuration();
if (config.type() == QNetworkConfiguration::UserChoice)
id
= networkSession
->sessionProperty
(QLatin1String("UserChoiceConfiguration")).
toString();
else
id = config.identifier();
settings.
setValue(QLatin1String("DefaultNetworkConfiguration"), id
);
settings.endGroup();
statusLabel->setText(tr("Run Server as well"));
enablebtnConnect();
}
#include <QtGui>
#include <QtNetwork>
#include "client.h"
//! [0]
client::client(QWidget *parent):
QDialog(parent), networkSession(0)
{
hostLabel = new QLabel(tr("&Server name:"));
portLabel = new QLabel(tr("S&erver port:"));
// find out which IP to connect to
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i) {
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
break;
}
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
hostLineEdit = new QLineEdit(ipAddress);
portLineEdit = new QLineEdit;
portLineEdit->setValidator(new QIntValidator(1, 65535, this));
hostLabel->setBuddy(hostLineEdit);
portLabel->setBuddy(portLineEdit);
statusLabel = new QLabel(tr("Run Server as well"));
btnConnect = new QPushButton(tr("Connect"));
btnConnect->setDefault(true);
btnConnect->setEnabled(false);
quitButton = new QPushButton(tr("Quit"));
tcpSocket = new QTcpSocket(this);
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(hostLineEdit, SIGNAL(textChanged(QString)),this, SLOT(enablebtnConnect()));
connect(portLineEdit, SIGNAL(textChanged(QString)),this, SLOT(enablebtnConnect()));
connect(btnConnect, SIGNAL(clicked()), this, SLOT(newconnect()));
connect(timer, SIGNAL(timeout()),this, SLOT(requestNewFortune()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(hostLabel, 0, 0);
mainLayout->addWidget(hostLineEdit, 0, 1);
mainLayout->addWidget(portLabel, 1, 0);
mainLayout->addWidget(portLineEdit, 1, 1);
mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
mainLayout->addWidget(btnConnect, 3, 0);
mainLayout->addWidget(quitButton, 3, 1);
setLayout(mainLayout);
setWindowTitle(tr("Fortune client"));
portLineEdit->setFocus();
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(sessionOpened()));
btnConnect->setEnabled(false);
statusLabel->setText(tr("Opening network session."));
networkSession->open();
}
}
void client::newconnect()
{
timer->start();
}
void client::requestNewFortune()
{
btnConnect->setEnabled(false);
blockSize = 0;
tcpSocket->abort();
tcpSocket->connectToHost(hostLineEdit->text(),
portLineEdit->text().toInt());
}
void client::readFortune()
{
//! [9]
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);
if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
//! [8]
//! [10]
in >> blockSize;
}
if (tcpSocket->bytesAvailable() < blockSize)
return;
//! [10] //! [11]
QString nextFortune;
in >> nextFortune;
if (nextFortune == joyvalue) {
QTimer::singleShot(0, this, SLOT(requestNewFortune()));
return;
}
//! [11]
//! [12]
joyvalue = nextFortune;
//! [9]
statusLabel->setText(joyvalue);
btnConnect->setEnabled(true);
}
void client::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Fortune client"),
tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Fortune client"),
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
QMessageBox::information(this, tr("Fortune client"),
tr("The following error occurred: %1.")
.arg(tcpSocket->errorString()));
}
btnConnect->setEnabled(true);
}
void client::enablebtnConnect()
{
btnConnect->setEnabled((!networkSession || networkSession->isOpen()) &&
!hostLineEdit->text().isEmpty() &&
!portLineEdit->text().isEmpty());
btnConnect->setText("Connected");
}
void client::sessionOpened()
{
// 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();
statusLabel->setText(tr("Run Server as well"));
enablebtnConnect();
}
To copy to clipboard, switch view to plain text mode
but is not showing me any output window nor any build issues.
Bookmarks