Results 1 to 8 of 8

Thread: network client socket without using signals and slots

Threaded 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.

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.