0

I need to send a string line from a C# client (3.5) to a QT server. Everything works ok except one thing that on the server side I have to check a size of the line which is first 2 bytes. (I can't just make an infinite loop of listing the client). I try to put the size of the line to a first byte of the line but it reads wrongly on the server side. So the question is how to put a length of the string into first 2 bytes of the string?.

In QT it's done like this:

QByteArray arrBlock;
QDataStream out(&arrBlock, QIODevice::WriteOnly);
out << quint16(0) << "string";
out.device() -> seek(0);
out << **quint16(arrBlock.size() - sizeof(quint16));**
socket->write(arrBlock);

but in C# it's only

NetworkStream serverStream;
byte[] bytes = ...GetBytes("string");
serverStream.write
serverStream.flush

I couldn't find the way to write size to a message to the server.

Vadim
  • 85
  • 1
  • 9
  • First of all, your question says "last 2 bytes" but the code says you want "first 2 bytes"? Secondly, why not put the actual data into a collection that have a length property, like a `QString`? Then you can write the length and data at once. – Some programmer dude May 19 '14 at 09:41
  • No I mean like `QString data("string"); out << qint16(data.length()) << data; socket->write(...);`` – Some programmer dude May 19 '14 at 10:45
  • Do you mean something like "3|abc"? on the server side it won't be read correctly. `code` in >> block; //here it will be more than 10 symbols number. if(socket->bytesAvailable()<(qint16)block) {/*I got here where I shouldn't be cuz my block is actually 3 bytes. I need to get to else and it's possible to do having written actual size of a line*/ QByteArray data; data=socket->read(socket->bytesAvailable()); tmp.append(data); if (tmp.size() < (int)block) return; } `code` – Vadim May 19 '14 at 10:46
  • I didn't find anything like out << ... in C#. That stuff with streams I have to do in C# – Vadim May 19 '14 at 10:47

0 Answers0