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()
{
}
void server::active(){
connect(s,SIGNAL(newConnection()),this,SLOT(nc()));
}
void server::nc(){
while(true){
std::cout <<"wait read..."<<std::endl;
so->waitForReadyRead(5000000);
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
and for testing, i use putty to connect in raw mode to the server.
So, if i send with putty these lines:
testline1
testline1
To copy to clipboard, switch view to plain text mode
the server will output this
wait read...
9--testline1
wait read...
0--
wait read...
wait read...
9--testline1
wait read...
0--
wait read...
To copy to clipboard, switch view to plain text mode
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
testline1
testline222222
lastwasempty
To copy to clipboard, switch view to plain text mode
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...
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...
To copy to clipboard, switch view to plain text mode
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?
Bookmarks