Results 1 to 3 of 3

Thread: QTcpServer

  1. #1
    Join Date
    Sep 2011
    Posts
    45
    Thanks
    17
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer

    Hello!

    I would like to create a kind of chat with a help of classes QTcpServer and QTcpSocket. But probably I'm missimg something definitely important. So, my idea is to re-implement the QTcpServer::incomingConnection method, get the socket's descriptor and the socket itself, put it in QMap<int, QTcpSocket>. And when the signal readyRead() will be emited, start to search in a dictionary the client's socket by key's value. But here is the main problem: how we can get that value? Yes, we can use this->socketDescriptor() method, but it will return only the server descriptor, not the client's descriptor. I need some hint. In which way I need to move? And how I can get the client's socket descriptor, which is sending the signal at any one time?

    For reading I'm using the standart QTcpSocket method: readAll(): qDebug() << this->currentSocket->readAll().data();

    Is my approach wrong?

    Thank you beforehand for answers.


    ADDED:

    this->currentSocket->socketDescriptor() will return only the latest connected socket's descriptor... And that is the problem. Yes, I can get the readyRead() signal. But I can't get any descriptors, which belong to the client that sent a data-packet. I need to keep each channel open until client will disconnect.


    Added after 52 minutes:


    I figured it out by myself=)) So, I still can use the magic line: QTcpSocket *currentSocket = (QTcpSocket *)this->sender();
    In this case I receive the object which sent the readyRead() - signal. In my humble opinion this one is solution.
    Question for guru: is it only approach? Or I missed something once again? =)
    Last edited by DmitryNik; 30th September 2011 at 17:59.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpServer

    In my opinion the best approach is to use proper encapsulation. If you have a class for handling client connections, you'll end up with a situation where one socket is bound with exactly one instance of the connection class. Therefore you immediately know which client you are handling because each handler object only deals with one socket.

    Qt Code:
    1. class Handler : public QObject {
    2. Q_OBJECT
    3. public:
    4. Handler(QTcpSocket *clientSocket, QObject *parent = 0) : QObject(parent), sock(clientSocket) {
    5. connect(sock, SIGNAL(readyRead()), this, SLOT(handleInput()));
    6. }
    7. private slots:
    8. void handleInput() {
    9. QByteArray data = sock->readAll();
    10. doStuffWith(data);
    11. }
    12. private:
    13. QTcpSocket *sock;
    14. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    DmitryNik (1st October 2011)

  4. #3
    Join Date
    Sep 2011
    Posts
    45
    Thanks
    17
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer

    Thank you!
    That's what I missed.

Similar Threads

  1. No such QTcpServer file??
    By lengshuang in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2009, 06:29
  2. QTcpServer + QThread
    By Alex Snet in forum Qt Programming
    Replies: 2
    Last Post: 14th April 2009, 21:46
  3. QTcpServer and GDB
    By baray98 in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2009, 08:02
  4. Qtcpserver problem
    By kingslee in forum Qt Programming
    Replies: 3
    Last Post: 3rd September 2008, 18:34
  5. Replies: 1
    Last Post: 18th June 2006, 10:12

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.