PDA

View Full Version : Simple Chat example



Misenko
5th July 2008, 17:18
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.

jpn
5th July 2008, 17:59
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.


where is data from write() actually written.
What do you mean with "where"? To the input/output device, a socket in this case.


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. :)

Misenko
5th July 2008, 18:56
Thanks.

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

But I stil dont know how is it possible :o

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

jpn
5th July 2008, 19:19
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 (http://en.wikipedia.org/wiki/Transmission_Control_Protocol), 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.