Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Sending two string values using QTcpSocket's methods?
Servers for Qt installer are currently down

[SOLVED] Sending two string values using QTcpSocket's methods?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 7 Posters 16.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    srivatsa
    wrote on 27 May 2013, 11:56 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 27 May 2013, 12:49 last edited by
      #2

      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.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        srivatsa
        wrote on 27 May 2013, 12:53 last edited by
        #3

        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...

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on 27 May 2013, 13:20 last edited by
          #4

          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.

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • 8 Offline
            8 Offline
            8majkel8
            wrote on 27 May 2013, 13:21 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tobias.hunger
              wrote on 27 May 2013, 19:11 last edited by
              #6

              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.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                srivatsa
                wrote on 28 May 2013, 04:45 last edited by
                #7

                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]

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ChrisW67
                  wrote on 28 May 2013, 05:03 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    KA51O
                    wrote on 28 May 2013, 09:14 last edited by
                    #9

                    @
                    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());
                    .
                    .
                    .
                    @

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      srivatsa
                      wrote on 28 May 2013, 10:17 last edited by
                      #10

                      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 ...

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        ChrisW67
                        wrote on 30 May 2013, 01:23 last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          qxoz
                          wrote on 30 May 2013, 05:17 last edited by
                          #12

                          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 Reply Last reply
                          0

                          1/12

                          27 May 2013, 11:56

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved