Results 1 to 4 of 4

Thread: Simple Chat example

  1. #1
    Join Date
    Jun 2008
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Simple Chat example

    Hi

    I want to do something like this

    http://wiki.qtcentre.org/index.php?title=Simple_chat

    but I dont understand how it works.

    I dont understand when is readyRead() signal emited and where is data from write() actually written. And why in server receiveMessage() slot is write() called for each socket. Doesnt it call the same write() for one socket many times?

    I ll be glad if anybody can explain me this whole system.
    Thanks.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Simple Chat example

    Quote Originally Posted by Misenko View Post
    I dont understand when is readyRead() signal emited
    See QIODevice::readyRead() docs:
    This signal is emitted once every time new data is available for reading from the device. It will only be emitted again once new data is available, such as when a new payload of network data has arrived on your network socket, or when a new block of data has been appended to your device.
    Quote Originally Posted by Misenko View Post
    where is data from write() actually written.
    What do you mean with "where"? To the input/output device, a socket in this case.

    Quote Originally Posted by Misenko View Post
    And why in server receiveMessage() slot is write() called for each socket. Doesnt it call the same write() for one socket many times?
    In this particular example the client sends a message to the server and the server delivers the message to each and every client. So one gets even his/her own messages from the server. Of course this could be done differently, but this keeps things quite simple. Oh, and one can be sure that the connection is working if s/he can see his/her own messages.
    J-P Nurmi

  3. #3
    Join Date
    Jun 2008
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple Chat example

    Thanks.

    Yes a read the manual but I dont understant it very well

    But I stil dont know how is it possible

    In the client sendMessage() slot is write() function called. So is readyRead() signal emited after this function?
    I think it has to becouse this signal is connected to server receiveMessage() slot. But in this slot write() is called again.
    Isnt it called twice? And if readyRead() is emited after write why isnt message appent more time?

    Sorry if i am asking something stupid but i realy need it. Thanks

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Simple Chat example

    Quote Originally Posted by Misenko View Post
    In the client sendMessage() slot is write() function called. So is readyRead() signal emited after this function?
    I think it has to becouse this signal is connected to server receiveMessage() slot. But in this slot write() is called again.
    Isnt it called twice? And if readyRead() is emited after write why isnt message appent more time?
    Once you press return in the client, message data (a line) is written to the socket. Once data arrives to the server side, readyRead() signal is emitted. The server reads the data and writes it to each client. Once data reaches a client, readyRead() signal is emitted again but this time at the client side, of course. At this stage the message is appended to the text editor (message view).

    Due to the nature of TCP, both server and client sides use buffering. Actions are only taken once enough data (a complete line) has arrived. In case not enough data (an incomplete line) has arrived, the data is just stored to the buffer and the next readyRead() signal is waited for to continue reading.
    J-P Nurmi

Similar Threads

  1. QTcpSocket Chat : Mutiple Client using FD_SET and select()
    By cooler123 in forum Qt Programming
    Replies: 20
    Last Post: 15th January 2012, 19:57
  2. very simple and basic FAM/Gamin wrapper for qt
    By momesana in forum Qt-based Software
    Replies: 0
    Last Post: 8th April 2008, 12:46
  3. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  4. Creating simple text editor from the eamples code
    By overcast in forum Qt Programming
    Replies: 4
    Last Post: 14th February 2007, 15:46
  5. Widget for chat presentation
    By krivenok in forum Qt Programming
    Replies: 8
    Last Post: 24th January 2006, 13:37

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.