By the Client, it was written as such:
Qt Code:
{ QByteArray data; data.append(com); // "47Saman" if(clientSocket->write (data) == -1) { qDebug("sendCommand(QString) Failed"); } }To copy to clipboard, switch view to plain text mode
By the Client, it was written as such:
Qt Code:
{ QByteArray data; data.append(com); // "47Saman" if(clientSocket->write (data) == -1) { qDebug("sendCommand(QString) Failed"); } }To copy to clipboard, switch view to plain text mode
If you are writing "47Saman" to the socket, what do you expect to receive as first four bytes on the other end of the communication?
Wysota, client is sending me a jpeg file. in the server program, I used readAll() to read it all and pixmap it to a label. But, the image does not appear in the label (though the widget background flashes). To solve this, I was told that they would send the size of the jpeg file along with its content. Hence, I need to read the length first, and then realize how many bytes exactly to read.
Qt Code:
{ QPixmap pm; pm.loadFromData(data); ui->label->setPixmap(pm); }To copy to clipboard, switch view to plain text mode
Last edited by saman_artorious; 24th July 2013 at 18:57.
1. sizeof(char) != 4
2. int does not contain decimal digits, it is a binary value ranging from ~ -2^31 to 2^31 (which can hold 10 decimal digits) for a 32 bit integer memory layout
3. reading a value that has not been written cannot end well, can it?
4. you're doing a lot of other errors in your code which completely prove you have no idea what you are doing and just blindly trying to get things working without any understanding what is going on.
Sockets are not magical entities, they do not convert your data, add some artificial data or remove some data -- if you write a sequence of bytes to it, the exact same sequence of bytes will be read by the other end of the communication channel. Don't try to do network programming if you don't understand how it works. Spend two days reading about network programming instead of listening to hints you don't even understand.
Last edited by saman_artorious; 25th July 2013 at 09:47.
I want to read it as binary. appending data length at the beginning with this:
Qt Code:
void tcpClient::sendToServer() { QByteArray data; data.append(5); data.append("Saman"); if(clientSocket->write (data) == -1) { qDebug("write() failed"); } }To copy to clipboard, switch view to plain text mode
in the server, let's read the first 4 bytes, as the size of integer and then read the text.
Qt Code:
QByteArray text; int length; clientConnection->read((char*)&length, 4); qDebug() << length; text.reserve(length); clientConnection->read(text.data(), length); clientConnection->flush();To copy to clipboard, switch view to plain text mode
output:
length read is 1835094789 !
what am I missing?
QByteArray::append(5) appends one byte with a value of 5 not integer. Do it in such a way :
Qt Code:
clientSocket->write (5);//You are sending int(5) and on other side You can read int clientSocket->write("Saman");To copy to clipboard, switch view to plain text mode
wrong! that would prompt invalid conversion from int to const char*
OK, clientSocket->write(5) is only idea. This code should compile
saman_artorious read more about QDataStream.Qt Code:
QByteArray data; m_stream << 5 << "Saman"; clientSocket->write(data);To copy to clipboard, switch view to plain text mode
No, please, don't read about QDataStream. Ignore its existance completely for this project.
I did not know about QDataStream.
Now, it reads the integer fine. However, the amount of data it reads does not depend on how much I allocate according to length.
it always reads the whole thing. How can I control it?
Qt Code:
void Server::readState() { int len = 0; in >> len; qDebug() << len; QByteArray data; data.reserve(len); in >> data; // read 20 chars and not all qDebug() << data.data(); data.clear(); clientConnection->flush(); }To copy to clipboard, switch view to plain text mode
what I sent is of length 50, but only 20 chars would be read, though, it reads all 50 each time:
Qt Code:
void tcpClient::sendToServer() { QByteArray data; m_stream << 20 << "xxxxxxxxxx yyyyyyyyyy wwwwwwwwww zzzzzzzzzz aaaaaaaDD"; //text size = 50 clientSocket->write(data); clientSocket->flush(); }To copy to clipboard, switch view to plain text mode
output:
20
xxxxxxxxxx yyyyyyyyyy wwwwwwwwww zzzzzzzzzz aaaaaaaDD
Please stop trying things at random, this is getting out of hand. You went ahead with QDataStream; you should have read its documentation. Do you have any idea what the insertions (<<) and extractions (>>) do?
What you really need is read a bit about serialization and deserialization of basic C/C++ types. Forget about the network for now, just pretend you are designing a platform and architecture-independent binary file format in which to save/from which to load the piece of data you are interested in. This has nothing to do with Qt, by the way.
Yes, it is doing that because that is the correct behaviour for QDataStream. You don't "control it" but you could at least try to understand it and all that has preceded it in this thread.
If you are serious about understanding then try this experiment: using each approach write the data to a file (substitute a QFile for the QTcpSocket). These files represent exactly what you are sending over the wire. Look at the length of the files. Inspect the files byte by byte (a binary editor or Linux od command). Try changing the numbers and see which bytes change. Look at sizeof(int) how many bytes are you trying to read?
You are always reading it as binary, even if it is textual data. It is just a matter of interpreting the data.
You are missing a couple of hours of learning about computers, their memory and data layout of different data types.what am I missing?
1835094789d = 0x6D615305 = 0x0553616D (swapped byte order) = "\5Sam" interpreted as text. Does that ring a bell?
Bookmarks