[SOLVED] Sending two string values using QTcpSocket's methods?
-
Hello mcosta, thank you for the reply, but how can I seperate data? Normally we send username and password as two values stored in 2 variables for verification in the database. Am I wrong? Please tell me if I can use QDataStream or QTextStream methods for my problem...
-
Hi,
TCP uses a single stream to send data; when you write
@
stream->write("username")
stream->write("password")
@on the stream you find usernamepassword.
You could use for example
@
stream->write("username")
stream->write(":")
stream->write("password")
@on the stream you find username:password; the server can now separate items
Little Tip: If you have this kind of problem, probably you must define a better client/server interface.
-
QTextStream can be constructed based on existing device -> "see documentation":http://qt-project.org/doc/qt-4.8/qtextstream.html#QTextStream-2.
I think this method could work too. I guess Java server reads incoming data like from console.
@//new line is the separator
QString username, password;
socket->write(username + "\n");
socket->write(password "\n");@Generally in plain socket communication you have to create some kind of protocol.
Tip: create http server handling custom REST api and then make qt call http requests. This way your server is more accessible (firewall could block socket port you are communicating on). This way you can pass arguments in post or url query.
-
Which encoding of the string is expected by the server? Many languages use an 8bit unicode encoding (utf-8), while Qt uses a 16bit encoding (utf-16).
Sending a password over TCP is a BAD idea: It will go over the network in clear text and everybody can sniff it from there with standard tools. Send cryptocraphic hash sum of the password instead. Use salted hashes to prevent dictionary attacks.
-
Thats right, when I write like this:
@QString uname = "sri";
QString upwd = "abc";socket->write("sri \n");
socket->write("abc");
@its working!, but when I declare directly the variable name inside @write@ method like this
@
socket->write(uname "\n");
socket->write(upwd);
@I get the following error:
@error: no matching function for call to ‘QTcpSocket::write(QString&)’@
[quote author="8majkel8" date="1369660913"]QTextStream can be constructed based on existing device -> "see documentation":http://qt-project.org/doc/qt-4.8/qtextstream.html#QTextStream-2.
I think this method could work too. I guess Java server reads incoming data like from console.
@//new line is the separator
QString username, password;
socket->write(username + "\n");
socket->write(password "\n");@Generally in plain socket communication you have to create some kind of protocol.
Tip: create http server handling custom REST api and then make qt call http requests. This way your server is more accessible (firewall could block socket port you are communicating on). This way you can pass arguments in post or url query.[/quote]
-
The error message is correct. You are passing a QString to a function expecting a const char* or a reference to a QByteArray. You need to convert the QString to a suitable encoding using QString::toAscii(), toLatin1(), toUtf8(), toLocal8Bit() etc. as appropriate for the data in the string and server's expectation of encoding.
-
-
How do you know sending has worked if you are not able to receive what was supposedly sent?
Anyway, you would only use QDataStream if the sender was also a Qt program using QDataStream. You could use QTextStream to process data from a text sender. You use neither. In either case you would apply them to a QByteArray/QBuffer containing whatever passes for a "complete" protocol data unit.
-
As ChrisW67 said, if client and server are Qt programm, you can use QDataStream.
Look at this: "Qt Network Communication":http://qt-project.org/forums/viewreply/125058/