Results 1 to 5 of 5

Thread: Handle more than one QTcpSockets with one QTcpServer. How?

  1. #1
    Join Date
    Jun 2010
    Posts
    31
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Handle more than one QTcpSockets with one QTcpServer. How?

    Hi all,

    some background information:
    I have a simple application with running a server. Several other apps should be able to connect as client to it.

    Now I'm using QTcpServer. After call listen() the server is able to accept incoming clients.
    I have connected the newConnection() signal to a private slot clientConnected(). According as the docs I call within nextPendingConnection() to get an instance of QTcpSocket. Then I connect the signal disconnected() to slot deleteLater() of that instance.

    If several clients connects to the server there are several QTcpSocket instances. I store the reference of instances to a list because the server should send data to all connected clients.

    Now the problem:
    If one client aborts the connection how I can remove the reference of that QTcpSocket instance from my list?
    The disconnect() signal is handled within the scope of QTcpSocket. I should have a method like QTcpServer::connectionAborted(QTcpSocket* client).

    Have anybody an idea?

    Thanks, Jack

  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: Handle more than one QTcpSockets with one QTcpServer. How?

    I assume you mean "aborts the connection" is the same as closing the connection (or disconnecting)?

    If so, don't connect the QTcpSocket::disconnected() signal to the deleteLater() slot. Instead create a connectionAborted() slot and link it the disconnected() signal to this slot.
    In the connectionAborted slot remove the item from your list and delete the socket using deleteLater() for example

  3. #3
    Join Date
    Jun 2010
    Posts
    31
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Handle more than one QTcpSockets with one QTcpServer. How?

    Hi,

    create an own slot connectionAborted() is nice. But how can I find out inside that method which QTcpSocket instance has called him?

    Connecting QTcpSocket::disconnected() to MyServer::connectionAborted() means that all QTcpSocket instances calls the same method.

    Which one I should remove?

    But I see one chance: In connectionAborted() I can check what QTcpSocket instance has state != SocketState::ConnectedState, then call deleteLater() and remove it from list.

    I try it. Thanks.

  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: Handle more than one QTcpSockets with one QTcpServer. How?

    Use sender() and cast it to a QTcpSocket

    Some people don't like this and use a signal mapper.

    Edit: See also here:
    http://www.qtcentre.org/wiki/index.p...ithout_threads

  5. #5
    Join Date
    Jun 2010
    Posts
    31
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Handle more than one QTcpSockets with one QTcpServer. How?

    Thanks. The wiki link is great.

    I think it's nicer to clean the client list with identify the instance by sender().

    And: It's long time ago I worked with Qt. I didn't know the sender() method.
    But I like it instead of MFC.

Similar Threads

  1. Extend QTcpServer to handle multiple clients
    By DiamonDogX in forum Qt Programming
    Replies: 5
    Last Post: 24th February 2010, 19:49
  2. Multiple connections with QTcpSockets
    By DrDonut in forum Qt Programming
    Replies: 1
    Last Post: 11th September 2009, 10:58
  3. QTCPSockets
    By Thunder in forum Qt Programming
    Replies: 0
    Last Post: 6th August 2008, 18:19
  4. QTcpSockets and QThreads
    By TheRonin in forum Newbie
    Replies: 3
    Last Post: 21st June 2006, 09:41
  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.