get the error:

> QIODevice::write (QTcpSocket): device not open.
After trying , I think problem is passing parameter server->nextPendingConnection() into object. Can someone has idea how to do it correctly?

My understanding is that object for `socketClient` is not initialised properly.
I'm using Ubuntu with Qt.

I am implementing server using Qt. The server part has two classes based on `QTcpServer` and `QTcpSocket`.
say `Server` and `SocketClient`.
I am creating object of SocketClient in server and for testing purpose I opened telnet session and wants to see that server write "hello" on terminal. But somehow its not working. Can someone please advice me where I am making mistake.

Qt Code:
  1. Server::Server(QObject *parent) : QObject(parent)
  2. {
  3. server_obj = new QTcpServer( this );
  4. }
  5.  
  6. void Server::startServer()
  7. {
  8. connect( server_obj, SIGNAL( newConnection() ), this, SLOT( incomingConnection() ) );
  9. if( !server_obj->listen( QHostAddress::Any, 9999) )
  10. {
  11. qDebug() << " Server failed to get started";
  12. }
  13. else
  14. {
  15. qDebug() << " Server started"; // this is successful
  16. }
  17. }
  18.  
  19.  
  20. void Server::incomingConnection()
  21. {
  22. socketforClient = new SockClient( server_obj->nextPendingConnection() );// has a doubt on nextPendingconection?? May be Problem HERE..
  23.  
  24. //only for testing remove it
  25. socketforClient->writeToClient();
  26. }
To copy to clipboard, switch view to plain text mode 
Class for Client

Qt Code:
  1. * Description: Its a constructor.I have changed default constructor to add QTcpSocket* object in parameter.I used this constructor in void Server::incomingConnection()
  2. */
  3. SockClient::SockClient(QObject *parent,QTcpSocket* socket ) : QObject(parent)
  4. {
  5. socketClient = new QTcpSocket( socket ); // PROBLEM HERE ?
  6.  
  7. qDebug() << " we are in constructor of 1st sockclient ";
  8.  
  9. }
  10.  
  11.  
  12.  
  13. // this is for testing purpose only
  14.  
  15. void SockClient::writeToClient()
  16. {
  17. socketClient->write(" hello world\r\n");
  18. socketClient->flush();
  19. socketClient->waitForBytesWritten(3000);
  20.  
  21. }
To copy to clipboard, switch view to plain text mode 

//header file of SockClient

Qt Code:
  1. class SockClient : public QObject
  2. {
  3. Q_OBJECT
  4. public:
  5.  
  6. explicit SockClient( QObject *parent, QTcpSocket* socket= 0 ); // I have created
  7. void writeToClient(); // This is for testing purpose
  8. ~SockClient();
  9. signals:
  10.  
  11. private slots:
  12. void readClient();
  13.  
  14. private:
  15. void sendResponsetoMops();
  16.  
  17. QTcpSocket *socketClient;
  18.  
  19. quint16 nextBlockSize;
  20.  
  21. public slots:
  22. };
To copy to clipboard, switch view to plain text mode