Results 1 to 3 of 3

Thread: non blocking QTcpSocket

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default non blocking QTcpSocket

    I made simple chat based in client-server model, I am able to send messages from client to server but not in reverse. I tried to implement my chat in non blocking mode, but it seems not work for me.
    chat-0.4.tar.bz2

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: non blocking QTcpSocket

    Here are the problems int you code

    Client
    Qt Code:
    1. GuiClient::GuiClient(QWidget *parent) : QMainWindow(parent)
    2. , ui(new Ui::GuiClient()) //Add this
    3. {
    4.  
    5. ui->setupUi(this);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Server
    Qt Code:
    1. class GuiServer : public QMainWindow
    2. {
    3. ...
    4. private:
    5. ...
    6. QTcpSocket *client; //Add this
    7. };
    8.  
    9. GuiServer::GuiServer(QWidget *parent) : QMainWindow(parent)
    10. , ui(new Ui::GuiServer())
    11. , client(0)
    12. {
    13.  
    14. ui->setupUi(this);
    15. }
    16.  
    17. void GuiServer::readMsg()
    18. {
    19. // TcpSocket* client = (QTcpSocket*)sender(); //This will not work, and don't use this in your code, until you completely understand how signals, slots work.
    20.  
    21. if(client) //user stored member variable, should be 0 if no client is connected
    22. {
    23. ... //Add all existing code here
    24. }
    25. }
    26.  
    27. void GuiServer::incomingConnection()
    28. {
    29. if(client) //user stored member variable, should be 0 if no client is connected
    30. delete client;
    31. client = server->nextPendingConnection();
    32. ...
    33. }
    34.  
    35. void GuiServer::sendMsg()
    36. {
    37. // TcpSocket* client = (QTcpSocket*)sender(); //This will not work, and don't use this in your code, until you completely understand how signals, slots work.
    38.  
    39. if(client) //user stored member variable, should be 0 if no client is connected
    40. {
    41. ... //Add all existing code here
    42. }
    43. }
    44.  
    45. void GuiServer::disconnected()
    46. {
    47. client = 0; //no client present
    48. qDebug("A client disconnected");
    49. }
    To copy to clipboard, switch view to plain text mode 


    Also this kind of server will be able to talk to only one client at a time. Multiple client handling is not implement, if tired to connect, server may crash

  3. The following user says thank you to Santosh Reddy for this useful post:

    SIFE (1st June 2011)

  4. #3
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: non blocking QTcpSocket

    Just two newbie question's:
    Qt Code:
    1. GuiServer::GuiServer(QWidget *parent) : QMainWindow(parent)
    2. , ui(new Ui::GuiServer())
    3. , client(0)
    To copy to clipboard, switch view to plain text mode 
    Are you here just instancing client object or it is some thing else.
    Qt Code:
    1. TcpSocket* client = (QTcpSocket*)sender();
    To copy to clipboard, switch view to plain text mode 
    What is this exactly mean, because I just copy it from other source code and no look after search to found what is mean.

Similar Threads

  1. Blocking QTcpSocket for read/write operations
    By vcernobai in forum Qt Programming
    Replies: 3
    Last Post: 15th June 2020, 08:43
  2. QTcpSocket::connectToHost() is blocking the GUI!
    By jackmack in forum Qt Programming
    Replies: 9
    Last Post: 18th January 2011, 13:46
  3. Non-Gui thread blocking gui
    By Valheru in forum Qt Programming
    Replies: 17
    Last Post: 12th February 2010, 11:11
  4. Non blocking Gui Qthread in qt3
    By triperzonak in forum Newbie
    Replies: 2
    Last Post: 13th September 2008, 14:06
  5. Blocking ftp
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 21st December 2007, 08:18

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.