[SOLVED] Sending two string values using QTcpSocket's methods?
-
wrote on 27 May 2013, 11:56 last edited by
I would like to send two QLineEdit field's string values to a Server (written in Java). How I can send two values simultaneously? Either by using
@socket->write();@or
@socket->writeData();@
? I guess using QTextStream's methods we could but which one's to use?
I've used write() as below:
@socket->write("username");
socket->write("password");@But the above has went as single string to the server like usernamepassword!
-
wrote on 27 May 2013, 12:49 last edited by
Hi,
what do you mean with "How I can send two values simultaneously"?TCP has not concept like "packet" so all data are sent through a single stream; you should separate data in some way.
-
wrote on 27 May 2013, 12:53 last edited by
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...
-
wrote on 27 May 2013, 13:20 last edited by
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.
-
wrote on 27 May 2013, 13:21 last edited by
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.
-
wrote on 27 May 2013, 19:11 last edited by
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.
-
wrote on 28 May 2013, 04:45 last edited by
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]
-
wrote on 28 May 2013, 05:03 last edited by
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.
-
wrote on 28 May 2013, 09:14 last edited by
@
uname = uname + "\n";
socket->write(uname.data()); // there are multiple ways to get a ByteArray please select the one that fits your encoding
socket->write(uname.toAscii());
socket->write(uname.toLatin1());
socket->write(uname.toLocal8Bit());
.
.
.
@ -
wrote on 28 May 2013, 10:17 last edited by
Thank you all, I've done it, sending is a success. While receiving I get an empty string. Do I need to use QDataStream or QTextStream for incoming text from Server? Briefly let me know the code :) Thank you ...
-
wrote on 30 May 2013, 01:23 last edited by
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.
-
wrote on 30 May 2013, 05:17 last edited by
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/
1/12