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
Qt Code:
  1. #ifndef CLIENT_H
  2. #define CLIENT_H
  3.  
  4. #include <QDialog>
  5. #include <QTcpSocket>
  6. #include<Qtimer>
  7.  
  8. QT_BEGIN_NAMESPACE
  9. class QLabel;
  10. class QLineEdit;
  11. class QTcpSocket;
  12. class QNetworkSession;
  13. QT_END_NAMESPACE
  14.  
  15. //! [0]
  16. class client : public QDialog
  17. {
  18. Q_OBJECT
  19.  
  20. public:
  21. client(QWidget *parent = 0);
  22. QTimer *timer;
  23.  
  24. private:
  25.  
  26. void requestNewFortune();
  27. void readFortune();
  28. void newconnect();
  29. void displayError(QAbstractSocket::SocketError socketError);
  30. void enablebtnConnect();
  31. void sessionOpened();
  32. QLabel *hostLabel;
  33. QLabel *portLabel;
  34. QLineEdit *hostLineEdit;
  35. QLineEdit *portLineEdit;
  36. QLabel *statusLabel;
  37. QPushButton *btnConnect;
  38. QPushButton *quitButton;
  39. QDialogButtonBox *buttonBox;
  40.  
  41. QTcpSocket *tcpSocket;
  42. QString joyvalue;
  43. quint16 blockSize;
  44.  
  45. QNetworkSession *networkSession;
  46. };
  47. //! [0]
  48.  
  49. #endif
To copy to clipboard, switch view to plain text mode 


and code is

Qt Code:
  1. #include <QtGui>
  2. #include <QtNetwork>
  3.  
  4. #include "client.h"
  5.  
  6. //! [0]
  7. client::client(QWidget *parent):
  8. QDialog(parent), networkSession(0)
  9. {
  10. hostLabel = new QLabel(tr("&Server name:"));
  11. portLabel = new QLabel(tr("S&erver port:"));
  12.  
  13. // find out which IP to connect to
  14. QString ipAddress;
  15. QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
  16. // use the first non-localhost IPv4 address
  17. for (int i = 0; i < ipAddressesList.size(); ++i) {
  18. if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
  19. ipAddressesList.at(i).toIPv4Address()) {
  20. ipAddress = ipAddressesList.at(i).toString();
  21. break;
  22. }
  23. }
  24. // if we did not find one, use IPv4 localhost
  25. if (ipAddress.isEmpty())
  26. ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
  27.  
  28. hostLineEdit = new QLineEdit(ipAddress);
  29. portLineEdit = new QLineEdit;
  30. portLineEdit->setValidator(new QIntValidator(1, 65535, this));
  31.  
  32. hostLabel->setBuddy(hostLineEdit);
  33. portLabel->setBuddy(portLineEdit);
  34.  
  35. statusLabel = new QLabel(tr("Run Server as well"));
  36.  
  37. btnConnect = new QPushButton(tr("Connect"));
  38. btnConnect->setDefault(true);
  39. btnConnect->setEnabled(false);
  40.  
  41. quitButton = new QPushButton(tr("Quit"));
  42.  
  43.  
  44. tcpSocket = new QTcpSocket(this);
  45. connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
  46.  
  47. connect(hostLineEdit, SIGNAL(textChanged(QString)),this, SLOT(enablebtnConnect()));
  48. connect(portLineEdit, SIGNAL(textChanged(QString)),this, SLOT(enablebtnConnect()));
  49. connect(btnConnect, SIGNAL(clicked()), this, SLOT(newconnect()));
  50. connect(timer, SIGNAL(timeout()),this, SLOT(requestNewFortune()));
  51. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
  52. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
  53.  
  54. QGridLayout *mainLayout = new QGridLayout;
  55. mainLayout->addWidget(hostLabel, 0, 0);
  56. mainLayout->addWidget(hostLineEdit, 0, 1);
  57. mainLayout->addWidget(portLabel, 1, 0);
  58. mainLayout->addWidget(portLineEdit, 1, 1);
  59. mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
  60. mainLayout->addWidget(btnConnect, 3, 0);
  61. mainLayout->addWidget(quitButton, 3, 1);
  62. setLayout(mainLayout);
  63.  
  64. setWindowTitle(tr("Fortune client"));
  65. portLineEdit->setFocus();
  66.  
  67. QNetworkConfigurationManager manager;
  68. if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
  69. // Get saved network configuration
  70. QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
  71. settings.beginGroup(QLatin1String("QtNetwork"));
  72. const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
  73. settings.endGroup();
  74.  
  75. // If the saved network configuration is not currently discovered use the system default
  76. QNetworkConfiguration config = manager.configurationFromIdentifier(id);
  77. if ((config.state() & QNetworkConfiguration::Discovered) !=
  78. QNetworkConfiguration::Discovered) {
  79. config = manager.defaultConfiguration();
  80. }
  81.  
  82. networkSession = new QNetworkSession(config, this);
  83. connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
  84.  
  85. btnConnect->setEnabled(false);
  86. statusLabel->setText(tr("Opening network session."));
  87. networkSession->open();
  88. }
  89. }
  90.  
  91.  
  92. void client::newconnect()
  93. {
  94. timer->start();
  95.  
  96. }
  97.  
  98. void client::requestNewFortune()
  99. {
  100. btnConnect->setEnabled(false);
  101. blockSize = 0;
  102. tcpSocket->abort();
  103. tcpSocket->connectToHost(hostLineEdit->text(),
  104. portLineEdit->text().toInt());
  105. }
  106. void client::readFortune()
  107. {
  108. //! [9]
  109. QDataStream in(tcpSocket);
  110. in.setVersion(QDataStream::Qt_4_0);
  111.  
  112. if (blockSize == 0) {
  113. if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
  114. return;
  115. //! [8]
  116.  
  117. //! [10]
  118. in >> blockSize;
  119. }
  120.  
  121. if (tcpSocket->bytesAvailable() < blockSize)
  122. return;
  123. //! [10] //! [11]
  124.  
  125. QString nextFortune;
  126. in >> nextFortune;
  127.  
  128. if (nextFortune == joyvalue) {
  129. QTimer::singleShot(0, this, SLOT(requestNewFortune()));
  130. return;
  131. }
  132. //! [11]
  133.  
  134. //! [12]
  135. joyvalue = nextFortune;
  136. //! [9]
  137. statusLabel->setText(joyvalue);
  138. btnConnect->setEnabled(true);
  139. }
  140. void client::displayError(QAbstractSocket::SocketError socketError)
  141. {
  142. switch (socketError) {
  143. case QAbstractSocket::RemoteHostClosedError:
  144. break;
  145. case QAbstractSocket::HostNotFoundError:
  146. QMessageBox::information(this, tr("Fortune client"),
  147. tr("The host was not found. Please check the "
  148. "host name and port settings."));
  149. break;
  150. case QAbstractSocket::ConnectionRefusedError:
  151. QMessageBox::information(this, tr("Fortune client"),
  152. tr("The connection was refused by the peer. "
  153. "Make sure the fortune server is running, "
  154. "and check that the host name and port "
  155. "settings are correct."));
  156. break;
  157. default:
  158. QMessageBox::information(this, tr("Fortune client"),
  159. tr("The following error occurred: %1.")
  160. .arg(tcpSocket->errorString()));
  161. }
  162.  
  163. btnConnect->setEnabled(true);
  164. }
  165.  
  166. void client::enablebtnConnect()
  167. {
  168. btnConnect->setEnabled((!networkSession || networkSession->isOpen()) &&
  169. !hostLineEdit->text().isEmpty() &&
  170. !portLineEdit->text().isEmpty());
  171.  
  172. btnConnect->setText("Connected");
  173.  
  174.  
  175. }
  176.  
  177. void client::sessionOpened()
  178. {
  179. // Save the used configuration
  180. QNetworkConfiguration config = networkSession->configuration();
  181. QString id;
  182. if (config.type() == QNetworkConfiguration::UserChoice)
  183. id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
  184. else
  185. id = config.identifier();
  186.  
  187. QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
  188. settings.beginGroup(QLatin1String("QtNetwork"));
  189. settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id);
  190. settings.endGroup();
  191.  
  192. statusLabel->setText(tr("Run Server as well"));
  193.  
  194. enablebtnConnect();
  195. }
To copy to clipboard, switch view to plain text mode 
but is not showing me any output window nor any build issues.
Pls check it out.

Thanks
Arpit