PDA

View Full Version : QTcpSocket reading problem.



vani.pv
3rd September 2012, 12:47
I have an application using QTcpSocket.

for reading data i am using readline() as follows.

socket.write(buf,buf.length);
while(!socket.canReadLine());
socket.read(rbuf,socket.bytesAvailable);

But there is no data in my buf after reading and my gui getting freeze at bcoz of while loop.

can any one tell me how to wait until data available in the socket?

Thanks...

Qtonimo
3rd September 2012, 13:39
Use readyRead Signal. Write a class, which has several members (QByteArray buffer, QTcpSocket socket, ...) and a slot readyReadSlot(). Connect
the readyRead Signal of the socket to your slot.

Call socket.readAll() in your readyReadSlot() and append the returned QByteArray to your buffer member. Then check, if all data is in your buffer. If yes then process your data, if not return and wait for the next readyRead Signal of your socket.

decipher
4th September 2012, 07:16
on button click do this

sock.connectToHost(ipaddr, port);
QObject::connect(&sock, SIGNAL(readyRead()), this, SLOT(read()));
In read()


while(sock.canReadLine())
{
QString line;
line.setLength(4096);
line = sock.readLine(); // read the line of text
line=line.trimmed();

qDebug()<<"read"<<line;
}

Hope this helps
On exit click
close socket
disconnect object

viulskiez
5th September 2012, 18:47
Don't forget to read QAbstractSocket documentation and learn about blocking and nonblocking socket.

vani.pv
6th September 2012, 07:08
Thanks to all.........:)