Arpitgarg
26th March 2011, 17:35
Hi,
I just did couple of changes in Fortune client examplelike
1. changed name enableGetfortune to enablebtnConnect.
2. Added a timer so as have socket connection automatically
the header is
#ifndef CLIENT_H
#define CLIENT_H
#include <QDialog>
#include <QTcpSocket>
#include<Qtimer>
QT_BEGIN_NAMESPACE
class QDialogButtonBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QTcpSocket;
class QNetworkSession;
QT_END_NAMESPACE
//! [0]
class client : public QDialog
{
Q_OBJECT
public:
client(QWidget *parent = 0);
QTimer *timer;
private:
void requestNewFortune();
void readFortune();
void newconnect();
void displayError(QAbstractSocket::SocketError socketError);
void enablebtnConnect();
void sessionOpened();
QLabel *hostLabel;
QLabel *portLabel;
QLineEdit *hostLineEdit;
QLineEdit *portLineEdit;
QLabel *statusLabel;
QPushButton *btnConnect;
QPushButton *quitButton;
QDialogButtonBox *buttonBox;
QTcpSocket *tcpSocket;
QString joyvalue;
quint16 blockSize;
QNetworkSession *networkSession;
};
//! [0]
#endif
and code is
#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::NetworkSessionRequir ed) {
// 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();
}
but is not showing me any output window nor any build issues.
Pls check it out.
Thanks
Arpit
I just did couple of changes in Fortune client examplelike
1. changed name enableGetfortune to enablebtnConnect.
2. Added a timer so as have socket connection automatically
the header is
#ifndef CLIENT_H
#define CLIENT_H
#include <QDialog>
#include <QTcpSocket>
#include<Qtimer>
QT_BEGIN_NAMESPACE
class QDialogButtonBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QTcpSocket;
class QNetworkSession;
QT_END_NAMESPACE
//! [0]
class client : public QDialog
{
Q_OBJECT
public:
client(QWidget *parent = 0);
QTimer *timer;
private:
void requestNewFortune();
void readFortune();
void newconnect();
void displayError(QAbstractSocket::SocketError socketError);
void enablebtnConnect();
void sessionOpened();
QLabel *hostLabel;
QLabel *portLabel;
QLineEdit *hostLineEdit;
QLineEdit *portLineEdit;
QLabel *statusLabel;
QPushButton *btnConnect;
QPushButton *quitButton;
QDialogButtonBox *buttonBox;
QTcpSocket *tcpSocket;
QString joyvalue;
quint16 blockSize;
QNetworkSession *networkSession;
};
//! [0]
#endif
and code is
#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::NetworkSessionRequir ed) {
// 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();
}
but is not showing me any output window nor any build issues.
Pls check it out.
Thanks
Arpit