Send and receive strings with QTcpSocket
-
wrote on 3 Jun 2019, 21:00 last edited by
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
-
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 ?
-
wrote on 3 Jun 2019, 21:27 last edited by
-
This has nothing to do with Qt Creator. Qt Creator is just an IDE.
In any case, check waitForConnected
-
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
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
-
wrote on 4 Jun 2019, 08:53 last edited by VRonin 6 Apr 2019, 09:08
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"; });
4/6