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. Send and receive strings with QTcpSocket
QtWS25 Last Chance

Send and receive strings with QTcpSocket

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcpsockettcpclientserver
6 Posts 4 Posters 3.1k Views
  • 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.
  • C Offline
    C Offline
    Captain Matsuyo
    wrote on 3 Jun 2019, 21:00 last edited by
    #1

    Hello, I have a project with a window which displays some numeric informations. This informations come from a tcp server so I tried to add a tcp client in my program with these lines below that is supposed to send a message to my server regarding to it, the message is not received, could I have some help please ?
    My code :

        //Creation socket
        QTcpSocket socMeteo;
        socMeteo.connectToHost("192.168.1.35",10001);
        
        //Envoi de donnees
        QTextStream texte(&socHauteur);
        //texte<<phrase<<endl;
        texte<<"Bonsoir"<<endl;
    

    Thank you very much

    J 1 Reply Last reply 4 Jun 2019, 04:50
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 Jun 2019, 21:04 last edited by
      #2

      Hi and welcome to devnet,

      You are not checking whether the connection is successful, that would be the first step.

      How is your server implemented ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • C Offline
        C Offline
        Captain Matsuyo
        wrote on 3 Jun 2019, 21:27 last edited by
        #3

        My server is at school so right now I'm using Hercule setup utiliy to simulate a tcp server
        0_1559597036206_f7d387b0-ade2-4ee6-836f-55854a4951b6-image.png
        So how should I proceed to check if the connection is successful or not please ? I'm sorry I'm using QT Creator since April so I don't have a lot of knowledge

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 3 Jun 2019, 22:01 last edited by
          #4

          This has nothing to do with Qt Creator. Qt Creator is just an IDE.

          In any case, check waitForConnected

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • C Captain Matsuyo
            3 Jun 2019, 21:00

            Hello, I have a project with a window which displays some numeric informations. This informations come from a tcp server so I tried to add a tcp client in my program with these lines below that is supposed to send a message to my server regarding to it, the message is not received, could I have some help please ?
            My code :

                //Creation socket
                QTcpSocket socMeteo;
                socMeteo.connectToHost("192.168.1.35",10001);
                
                //Envoi de donnees
                QTextStream texte(&socHauteur);
                //texte<<phrase<<endl;
                texte<<"Bonsoir"<<endl;
            

            Thank you very much

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 4 Jun 2019, 04:50 last edited by
            #5

            @Captain-Matsuyo

            besides what @SGaist said about the connected state of your socket, this

            QTcpSocket socMeteo;

            is on the stack. My guess is, the QTcpSocket - object gets destroyed before the os has any chance of actually sending anything over the socket


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            5
            • V Offline
              V Offline
              VRonin
              wrote on 4 Jun 2019, 08:53 last edited by VRonin 6 Apr 2019, 09:08
              #6

              QTcpSocket socMeteo;
              QTextStream texte(&socHauteur);

              These are 2 different sockets. What are you trying to do?

              It should be something like:

              auto socMeteo = new QTcpSocket(this);
              socMeteo->connectToHost("192.168.1.35",10001);
              connect(socMeteo,&QTcpSocket::connected,socMeteo,[socMeteo]()->void{
                  QTextStream texte(socMeteo);
                  texte<<"Bonsoir\n";
              });
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              6

              6/6

              4 Jun 2019, 08:53

              • Login

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