Results 1 to 8 of 8

Thread: network client socket without using signals and slots

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows

    Question network client socket without using signals and slots

    Hello,

    I need to use a QTcpSocket for a 3D application (client side). My app has his own event loop, and I can't use signals and slots as the normal way (An QCoreApplication is not created). So I am trying to find an alternativ way.

    I made a test application, and remarked that functions waitForXxx of the socket classes can't being used in my case, because I've to block my 3D application for waiting for events, and I can't permit that in my render loop :/

    So I'm trying to understand how the eventsLoops of Qt are capturing signals for doing a similar thing in my event loop but it's pretty complicated ^^ (function QEventLoop:rocessEvents)

    What I want exactly :
    Adding a refresh function for verifying connection, disconnection, readyRead or readyWrite states of the socket.
    State function returns a resfreshed state of the socket.

    Someone can give me somes ideas for realising that ?
    thanks.

    here the code of my test application (we have to type "enter" to connect or disconnect):
    Qt Code:
    1. #include <QtCore>
    2. #include <QtNetwork>
    3. #include <windows.h>
    4. #include <iostream>
    5.  
    6. void connectOrDisconnect(QTcpSocket * socket)
    7. {
    8. if ( socket != NULL )
    9. {
    10. if ( socket->state() == QAbstractSocket::UnconnectedState )
    11. {
    12. socket->connectToHost("localhost", 1337);
    13. }
    14. else if ( socket->state() == QAbstractSocket::ConnectedState )
    15. {
    16. socket->disconnectFromHost();
    17. }
    18. else
    19. {
    20. socket->abort();
    21. }
    22. }
    23. }
    24.  
    25. int main(int argc, char *argv[])
    26. {
    27. QTcpSocket * socket = new QTcpSocket;
    28. int stateEnter;
    29.  
    30. while (1)
    31. {
    32. int lastStateEnter = stateEnter;
    33. stateEnter = GetKeyState(VK_RETURN);
    34. if ( lastStateEnter != stateEnter && GetKeyState(VK_RETURN) < 0 )
    35. {
    36. connectOrDisconnect( socket );
    37. }
    38.  
    39. if ( socket->state() == QAbstractSocket::UnconnectedState && socket->waitForConnected(100) )
    40. std::cout << "disconnected" << std::endl;
    41. if ( socket->state() == QAbstractSocket::ConnectedState && socket->waitForDisconnected(100) )
    42. std::cout << "disconnected" << std::endl;
    43. if ( socket->waitForReadyRead(100) )
    44. std::cout << "ReadyRead" << std::endl;
    45. if ( socket->bytesAvailable() )
    46. std::cout << QString::number(socket->bytesAvailable()).toStdString() << " bytes available" << std::endl;
    47.  
    48. switch ( socket->state() )
    49. {
    50. case QAbstractSocket::UnconnectedState:
    51. std::cout << "UnconnectedState" << std::endl;
    52. break;
    53. case QAbstractSocket::HostLookupState:
    54. std::cout << "HostLookupState" << std::endl;
    55. break;
    56. case QAbstractSocket::ConnectingState:
    57. std::cout << "ConnectingState" << std::endl;
    58. break;
    59. case QAbstractSocket::ConnectedState:
    60. std::cout << "ConnectedState" << std::endl;
    61. break;
    62. case QAbstractSocket::BoundState:
    63. std::cout << "BoundState" << std::endl;
    64. break;
    65. case QAbstractSocket::ClosingState:
    66. std::cout << "ClosingState" << std::endl;
    67. break;
    68. case QAbstractSocket::ListeningState:
    69. std::cout << "ListeningState" << std::endl;
    70. break;
    71. default:
    72. std::cout << "Unknow" << std::endl;
    73. break;
    74. }
    75. }
    76. }
    To copy to clipboard, switch view to plain text mode 

    If found this code in QAbstractSocket::waitForConnected :
    Qt Code:
    1. if (d->socketEngine && d->socketEngine->waitForWrite(timeout, &timedOut) && !timedOut) {
    2. d->_q_testConnection();
    3. } else {
    4. d->_q_connectToNextAddress();
    5. }
    To copy to clipboard, switch view to plain text mode 
    but I don't know what is d ? and socketEngine (maybe the platforme dependant socket object) ?
    Last edited by erqsor; 17th March 2011 at 05:22.

  2. #2
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: network client socket without using signals and slots

    Quote Originally Posted by erqsor View Post
    but I don't know what is d ? and socketEngine (maybe the platforme dependant socket object) ?
    see src:
    d created by macros Q_D(QAbstractSocket);

    from qglobal:


    Qt Code:
    1. #define Q_D(Class) Class##Private * const d = d_func()
    2. #define Q_Q(Class) Class * const q = q_func()
    To copy to clipboard, switch view to plain text mode 
    Try read Qt documentation before ask stupid question.

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: network client socket without using signals and slots

    Throw your network stuff into another thread and use the WatFor* methods is probably the easiest way if you don't want (or can't) use signals and slots.

  4. #4
    Join Date
    Jun 2010
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: network client socket without using signals and slots

    OK, thanks

    That's a good Idea squidge, but can I create and start a QThread if I don't have a QApplication in my process ?

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: network client socket without using signals and slots

    I've no idea, try it and see I'd imagine only the signal stuff wouldn't work, so you'd probably have to override the run method so it doesn't start an event loop?

    If QThread doesn't work there are many other ways of creating a new thread.

  6. #6
    Join Date
    Jun 2010
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: network client socket without using signals and slots

    Hum, OK thanks I will try

  7. #7
    Join Date
    Jun 2010
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: network client socket without using signals and slots

    Done. It's working fine, with some mutex, but I'm afraid that the 3D application will be slowed by a heavy network communication :/ (cause of the locks of the 3D application thread when accessing to the socket).
    But I will test that when my project will be more advanced

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

    Default Re: network client socket without using signals and slots

    I would suggest to try and rethink your design and integrate everything with QCoreApplication. Qt event loop is constructed in such a way that it allows easy integration with other event loops. You can even substitute the regular event loop with your own by implementing the QAbstractEventDispatcher interface.
    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.


Similar Threads

  1. Network Chat Client in a multihomed environment.
    By drescherjm in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2010, 17:28
  2. simple socket client example
    By Tomasz in forum Newbie
    Replies: 4
    Last Post: 26th July 2010, 13:15
  3. Client Socket
    By electronicboy in forum General Programming
    Replies: 1
    Last Post: 12th October 2009, 12:20
  4. Programming client-server with socket in multi-languages
    By philiptine in forum Qt Programming
    Replies: 3
    Last Post: 7th September 2007, 07:35
  5. Network Socket Programming
    By Walsi in forum Newbie
    Replies: 4
    Last Post: 19th June 2007, 10:04

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.