Results 1 to 20 of 24

Thread: using socket descriptor to identify the clients connected to the Threded server

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default using socket descriptor to identify the clients connected to the Threded server

    Hi

    How to use the socketDescriptor to identify the clients currently connected to threaded server.

    thread is created when any client is connected to the server..
    void FortuneServer::incomingConnection(int socketDescriptor)
    {
    FortuneThread *thread = new FortuneThread(socketDescriptor);
    ..
    thread->start();
    }

    ... in run()
    {
    set the socket Descriptor for the tcpSocket..
    connect(this->tcpSocket, SIGNAL(readyRead()), this, SLOT(ready1), Qt::UniqueConnection);
    }

    ready1()
    {
    read the request from the clients & serve....)
    }

    need to write a function which sends some information to all the connected clients...
    how to do this..

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    To send a message to all clients, you'll need to keep a list of all connected clients.

    Why do you want to use threads? Are the clients/server synchronous (blocking) or asynchronous (non-blocking)?
    If you do not need synchronous connections, then do not use threads, just keep a list of the client sockets.

    Edit:
    Ohh... and do not add this line:
    connect(this->tcpSocket, SIGNAL(readyRead()), this, SLOT(ready1), Qt::UniqueConnection);
    in the run() function of a thread!
    Add it to the constructor of your thread, or a function that sets the socket for that thread.
    Last edited by tbscope; 13th May 2010 at 14:54. Reason: add more comments

  3. #3
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    Thanks for reply..
    I need Asynchronous communication..
    how can i connect multiple clients without using threads..
    (My server has SQLite DB connection.. just information.. if it changes the conclusion of using thread or not using thread..)

    & How to get the list of all connected clients..?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    In your server create a list for client connections.
    QList<QTcpSocket *> clientConnections;

    Then, when you receive an new incomming connection, add the socket to the clientConnections list.
    Connect the most useful signals of the client socket to slots in your server. For example, disconnected() and readyRead().

    Example:
    Qt Code:
    1. void YourServer::incomingConnection(int socketDescriptor)
    2. {
    3. QTcpSocket *clientConnection = new QTcpSocket;
    4. clientConnection->setSocketDescriptor(socketDescriptor);
    5.  
    6. clientConnections->append(clientConnection);
    7.  
    8. connect(clientConnection, SIGNAL(...), this, SLOT(...));
    9.  
    10. // send some welcome to your socket (example)
    11. sendWelcome(clientConnection);
    12. }
    To copy to clipboard, switch view to plain text mode 


    When you want to send something to all client connections:

    Qt Code:
    1. void sendToAllConnections(...)
    2. {
    3. foreach(QTcpSocket *clientConnection, clientConnections) {
    4. clientConnection->write(...);
    5. }
    6.  
    7. etc...
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    Thanks a lot for the valuable suggestions, I will try multiple client connection without using threads.
    but using threads it was working fine.. with some run time warnings.. like "QObject: Cannot create children for a parent that is in a different thread. (Parent is FortuneThread(0x23bb7a0), parent's thread is QThread(0x1b166a0), current thread is FortuneThread(0x23bb7a0)" now whenconnect(this->tcpSocket, SIGNAL(readyRead()), this, SLOT(ready1), Qt::UniqueConnection); in the constructor of the thread the runtime warning vanished.. now in the run() i have only
    {
    while(1)
    {
    }
    }
    no other than this if i remove the while().. only first time it reads and write. i want to know the reasons & the logic.. can i ask u to put light on this or suggest me some resources where i will get the good knowledge.

  6. #6
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    How to pass the list of clientConnections in sendToAllConnections functions

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    You do not, it is already there.
    In my example above, when I write
    void sendToAllConnections(...)
    I really meant:
    void YourServer::sendToAllConnections(...)

    You add QList<QTcpSocket *> clientConnections; to your server class. Then it is available in every method of that class. Basic C++

    Qt Code:
    1. class YourServer : public QTcpServer
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ...
    7. void sendToAllConnections(const QString& message);
    8.  
    9. private:
    10. QList<QTcpSocket *> clientConnections;
    11. }
    12.  
    13. void YourServer::sendToAllConnections(const QString& message)
    14. {
    15. ...
    16. // Be sure to also check if the list isn't empty etc...
    17. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to tbscope for this useful post:

    meena (19th May 2010)

  9. #8
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    I have created a GUI project, in server class is i have imported QTcpServer & QTcpSocket. & using the incomingConnection() in this class. but this server is not able to read or write to the client.but there is connection established b/w client & server. few code lines to support this explanation..
    server.h
    namespace Ui {
    class Server;
    }

    class Server : public QWidget {
    Q_OBJECT
    public:

    protected:
    void incomingConnection(int socketDescriptor);

    private:
    QTcpServer *tcpServer;
    QTcpSocket *clientConnection;

    private slots:
    void Transactions();
    };
    server.cpp
    tcpServer = new QTcpServer(this);
    QHostAddress ipadd("..");
    tcpServer->listen(ipadd, p.no)

    void Server::incomingConnection(int socketDescriptor)
    {
    clientConnection = new QTcpSocket(this);
    clientConnection->setSocketDescriptor(socketDescriptor);
    connect(this->clientConnection, SIGNAL(readyRead()), this, SLOT(Transactions()));
    clientConnection->write("Hello from Server to client");
    }

    void Server::Transactions()//to read & write back..
    {
    char buf[1024];
    qint64 lineLength = clientConnection->read(buf, sizeof(buf));
    if (lineLength != -1) {

    }
    }

  10. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    Please take at least a little bit of effort to check your code yourself.
    If you do not see your error then I think it's better to start on something easier.

    You do not receive data from a client because there is no client.
    Connect the incomingConnection signal to your incomingConnection slot (which also should be defined as a slot).

    This is VERY basic!

  11. #10
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    k but
    if i declare incomingConnection as a slot it is telling
    Object::connect: No such slot QTcpServer::incomingConnection(int) in server.cpp

    as in qtcpserver.h this is defined as
    protected:
    virtual void incomingConnection(int handle);

  12. #11
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: using socket descriptor to identify the clients connected to the Threded server

    Here you go:
    This will be much better, but check the code yourself as I didn't test it.

    Qt Code:
    1. class YourServer : public QTcpServer
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. YourServer(QObject *parent = 0);
    7. void sendToAllConnections(const QString& message);
    8.  
    9. private slots:
    10. void slotNewConnection();
    11.  
    12. private:
    13. QList<QTcpSocket *> clientConnections;
    14. }
    15.  
    16. YourServer::YourServer(QObject *parent)
    17. : QTcpServer(parent)
    18. {
    19. connect(this, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
    20. }
    21.  
    22. void YourServer::slotNewConnection()
    23. {
    24. QTcpSocket *clientConnection;
    25.  
    26. while (hasPendingConnections()) {
    27. clientConnection = nextPendingConnection();
    28.  
    29. clientConnections->append(clientConnection);
    30.  
    31. connect(clientConnection, SIGNAL(...), this, SLOT(...));
    32. }
    33. }
    34.  
    35. void YourServer::sendToAllConnections(const QString& message)
    36. {
    37. foreach(QTcpSocket *clientConnection, clientConnections) {
    38. clientConnection->write(message.toUtf8());
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 30th November 2010, 21:09
  2. Replies: 7
    Last Post: 10th May 2010, 11:26
  3. Socket Descriptor
    By ManuMies in forum Qt Programming
    Replies: 1
    Last Post: 17th March 2009, 09:42
  4. remote port of a socket that hasn't connected yet
    By spraff in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2008, 17:24
  5. Staying connected to a MySQL server
    By fnmblot in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2007, 10:39

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
  •  
Qt is a trademark of The Qt Company.