Results 1 to 4 of 4

Thread: TCP Connection

  1. #1
    Join Date
    Feb 2014
    Posts
    7

    Default TCP Connection

    Hey guys i have a small question.

    I would like to connect with several servers by using one thread with different
    sockets. It works for one socket but doesnt connect with the other server.

    TCP: is the name of the thread.

    Qt Code:
    1. ...
    2. header
    3. ...
    4.  
    5. QTcpSocket *socket[10];
    6. ...
    7. ...
    8. source
    9. ...
    10.  
    11. void TCP::run()
    12. {
    13. initSocket();
    14. }
    15.  
    16. void TCP::initSocket()
    17. {
    18. for(int i=0;i<2;i++)
    19. {
    20. socket[i] = new QTcpSocket();
    21.  
    22. connect(socket[i], SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
    23. connect(socket[i], SIGNAL(connected()),this, SLOT(connected()), Qt::DirectConnection);
    24. connect(socket[i], SIGNAL(disconnected()),this, SLOT(stopConnection()), Qt::DirectConnection);
    25. connect(socket[i], SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
    26.  
    27. startConnection(i);
    28. }
    29. exec();
    30. }
    31.  
    32.  
    33. void TCP::startConnection(int conNUM)
    34. {
    35. QHostAddress addr(address[conNUM]);
    36. socket[conNUM]->connectToHost(addr, port[conNUM]);
    37. }
    38.  
    39. .....
    To copy to clipboard, switch view to plain text mode 

    Thanks

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TCP Connection

    How You recognize in slot the signal sender ?

  3. #3
    Join Date
    Feb 2014
    Posts
    7

    Default Re: TCP Connection

    Qt Code:
    1. void TCP::connected()
    2. {
    3. QObject* obj = sender();
    4. if( obj == socket[0] )
    5. {
    6. qDebug()<<address[0]<<endl;
    7. }
    8.  
    9. if( obj == socket[1] )
    10. {
    11. qDebug()<<address[1]<<endl;
    12. }
    13.  
    14. ..
    15. }
    To copy to clipboard, switch view to plain text mode 

    but he jumps every time to obj == socket[1].

  4. #4
    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: TCP Connection

    My suggestion would be to create a "connection" class that has all the slots and create one for each connection.
    Each object would get the host address in its constructor, creates the socket, connects the socket, and starts its connection.

    No need for Qt:irectConnection (both socket and connection object live in the TCP thread), no need for if/else in the slots

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 2nd April 2014, 08:41
  2. Qml and Xml connection
    By vinayaka in forum Newbie
    Replies: 2
    Last Post: 2nd October 2012, 11:31
  3. Replies: 0
    Last Post: 11th November 2011, 20:18
  4. Replies: 1
    Last Post: 2nd April 2010, 07:42
  5. Connection with MS SQL
    By manish_pesit in forum Qt Programming
    Replies: 4
    Last Post: 13th September 2006, 08:47

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.