PDA

View Full Version : QTcpSocket Write & \0 \x00



NewGuy5800
25th April 2010, 01:34
Hello, I'm pretty new to QT well even C in general, so please bear with me ^^

Basically I'm playing around with sockets and such, and I need to write a strick to the socket server with "\x00" attached to the end of the string, but apparently it's not as easy as that.

for example if I do
socket->write("data\x00");
it will only send "data" and not the appending null character..

So I was hoping you guys could point me in the right direction :)

wysota
25th April 2010, 10:23
\0 is interpreted as the end of a string so appending it to a string will have no useful result without telling the callee the size of the character array. So either don't append null bytes to the array (as it already contains a null character at the end) and/or pass the size of the array to the callee. Or use QByteArray:

QByteArray ba = "data";
ba.append('\0');
socket->write(ba);