grisson
9th August 2010, 14:06
Hi.
I'm trying to understand the read behavior of QTcpSocket::read(maxSize) function.
i have the following code (on the server):
#include "server.h"
#include <QTcpSocket>
#include <iostream>
server::server()
{
s=new QTcpServer;
}
void server::active(){
s->listen(QHostAddress::Any,5500);
connect(s,SIGNAL(newConnection()),this,SLOT(nc())) ;
}
void server::nc(){
QTcpSocket *so=s->nextPendingConnection();
while(true){
std::cout <<"wait read..."<<std::endl;
so->waitForReadyRead(5000000);
QByteArray a=so->read(1000);
if (a.endsWith("\r\n"))
a.resize(a.length()-2);
if (a.startsWith("quit"))
break;
//just to see readed text, this line will be deleted
std::cout <<a.length()<<"--"<<QString(a).toStdString()<<std::endl;
}
so->disconnectFromHost();
}
and for testing, i use putty to connect in raw mode to the server.
So, if i send with putty these lines:
testline1
the server will output this
wait read...
9--testline1
wait read...
0--
wait read...
analizing the behavior, line 1,2 and 3 are ok, but line 4 is unexpected(or not?)... if i input other lines like:
testline1
testline222222
lastwasempty
note that line 3 is empty
the server output is:
wait read...
9--testline1
wait read...
0--
wait read...
14--testline222222
wait read...
0--
wait read...
0--
wait read...
12--lastwasempty
wait read...
0--
wait read...
like previois example example, i can't understand why lines 4,8 and 14 are present...(obviously same thing for the "wait read" related lines, but this is not important)
some explanations?
I'm trying to understand the read behavior of QTcpSocket::read(maxSize) function.
i have the following code (on the server):
#include "server.h"
#include <QTcpSocket>
#include <iostream>
server::server()
{
s=new QTcpServer;
}
void server::active(){
s->listen(QHostAddress::Any,5500);
connect(s,SIGNAL(newConnection()),this,SLOT(nc())) ;
}
void server::nc(){
QTcpSocket *so=s->nextPendingConnection();
while(true){
std::cout <<"wait read..."<<std::endl;
so->waitForReadyRead(5000000);
QByteArray a=so->read(1000);
if (a.endsWith("\r\n"))
a.resize(a.length()-2);
if (a.startsWith("quit"))
break;
//just to see readed text, this line will be deleted
std::cout <<a.length()<<"--"<<QString(a).toStdString()<<std::endl;
}
so->disconnectFromHost();
}
and for testing, i use putty to connect in raw mode to the server.
So, if i send with putty these lines:
testline1
the server will output this
wait read...
9--testline1
wait read...
0--
wait read...
analizing the behavior, line 1,2 and 3 are ok, but line 4 is unexpected(or not?)... if i input other lines like:
testline1
testline222222
lastwasempty
note that line 3 is empty
the server output is:
wait read...
9--testline1
wait read...
0--
wait read...
14--testline222222
wait read...
0--
wait read...
0--
wait read...
12--lastwasempty
wait read...
0--
wait read...
like previois example example, i can't understand why lines 4,8 and 14 are present...(obviously same thing for the "wait read" related lines, but this is not important)
some explanations?