Results 1 to 4 of 4

Thread: How to create QTCPSocket object with nextPendingConnection?

  1. #1
    Join Date
    Aug 2015
    Posts
    4
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Cool How to create QTCPSocket object with nextPendingConnection?

    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 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to create QTCPSocket object with nextPendingConnection?

    QTcpServer::nextPendingConnection() returns an open QTcpSocket connected to the client. Why are you trying to create another one? This other socket you never connect or open...

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create QTCPSocket object with nextPendingConnection?

    And you never use the socket returned by nextPendingConnection(), you are passing it as the parent of SockClient.

    Cheers,
    _

  4. #4
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create QTCPSocket object with nextPendingConnection?

    void Server::incomingConnection()
    {
    QTcpSocket *newsocket = m_tcpServer->nextPendingConnection();

    connect(newsocket,SIGNAL(readyRead()),this,SLOT(re adyRead()));
    }

Similar Threads

  1. Can I create object in QML?
    By chong_kimkeang in forum Newbie
    Replies: 16
    Last Post: 23rd November 2012, 03:18
  2. Replies: 0
    Last Post: 10th November 2012, 09:32
  3. Replies: 6
    Last Post: 22nd December 2011, 22:03
  4. QTcpServer nextPendingConnection() problem
    By teodori.serge in forum Qt Programming
    Replies: 1
    Last Post: 6th November 2010, 21:02
  5. Replies: 2
    Last Post: 23rd June 2007, 10:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.