Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[solved]problem regarding when the host is not listening and the client attempts to read data in tcpsocket tcpserver program.............
Actually i am trying connect server and client tcpsocket and tcpserver class. In my program i have a refresh button, whenever the client clicks the button if the host is connected data will be recieved if not the connection should be closed. Receiving of data every time is works fine, but if the server is not ready and client clicks the refresh button everything will go wrong, there after no data will be received at the client end even if the client is sending the data, I tried closing tcpsocket connection if the server is not ready even after that i am getting the same problem, here is my code snippet please help
blockSize = 0;
if(tcpSocket->state() == QTcpSocket::UnconnectedState)
when the tcpsocket is ready for reading
i am calling a method where after reading the data i am closing the connection
[edit: code tags added, koahnig]
I have a doubt what is the difference between tcpSocket->disconnectFromHost(); and tcpSocket->close() and if i am closing the tcpsocket connection how to open it again?
To my understanding you will try to close something what is not open. This makes no sense and is the source of your problem.
AFAIK the "SocketState":http://qt-project.org/doc/qt-4.8/qabstractsocket.html#SocketState-enum is UnConnected at the beginning. When you are connecting to a host the state will change following the sequence of the different states.
Attempts to close the socket. If there is pending data waiting to be written, QAbstractSocket will enter ClosingState and wait until all data has been written. Eventually, it will enter UnconnectedState and emit the disconnected() signal.
connectToHost will open the connection again.
[edit, code added, koahnig]
Please use "code wrapping":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 in your posts. Otherwise they are not readable.
There is a fundamental difference between the two cases. A signal is typically connected to a slot and will be triggered when a signal has been emitted. "Checkout this description.":http://qt-project.org/doc/qt-4.8/signalsandslots.html The slot routine will be called only when the signal is triggered. This consumes only computer power when required.
With the second you are checking if data is available, so read it. This will require some sort of "endless" loop consuming eventually all computation power.
Sir in my program i want to run my data transfer part in thread so i am using thread. my run function consist
socket.----connectToHost( "127.0.0.1", 52222 );
actually this is not working, but i can't use @ connect(&socket,SIGNAL(readyRead()),this,SLOT(readData()));
@ because socket is local variable, if i use socket as global variable and initialize it in constructor or any other function of thread class i will get this error
@Cannot create children for a parent that is in a different thread.@
Can we change or override the signature of readyread() so that i can pass the local socket object?
The question is if you really need to use a thread. If the only purpose is to make sure that the socket is receiving the data when it should this is not required. The event loop will ensure that you will receive a notification through the readyRead signal.
If you really have to use QThread please have a look to "this wiki article.":http://qt-project.org/wiki/Threads_Events_QObjects
Probably you should also have a look to the fortune examples, "fortune server":http://qt-project.org/doc/qt-4.8/network-fortuneserver.html and "Fortune client.":http://qt-project.org/doc/qt-4.8/network-fortuneclient.html
sir actually i want dynamically data should appear on the screen when client receives the data, i mean i don't want use any buttons , so thought of using thread, if this can be done in gui thread please suggest how to do it..
Thank you sir,
problem solved :) :)