ada10
21st September 2010, 10:00
I am facing some issue related to http file download using QT APIs. My simple HTTP Client application sends a HTTP get request using QNetworkRequest. The code is as shown below –
QNetworkAccessManager * manager = new QNetworkAccessManager(this);
QNetworkRequest getReq;
getReq.setUrl( QUrl( fileurl ) );
QNetworkReply *reply = manager->get( getReq );
connect( reply, SIGNAL( readyRead()),this,SLOT( moreData()));
connect( reply, SIGNAL( error ( QNetworkReply::NetworkError ) ),
this,SLOT( errorInTransaction( QNetworkReply::NetworkError ) ) );
I have tested the client application using simple QT Http Server implementation ( which uses QTcpServer API ).
The client is able to download small sized files ( < 1MB ), but when I try to download large files ( > 1MB ) , there is an error on the server side & client application does not get the file contents.
This is the scenario when I run both applications on the phone. Has someone faced this issue and come up with probable solutions ?
The server code is as shown below –
serverwindow::serverwindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::serverwindow),clientConnection(0)
{
ui->setupUi(this);
ui->textEdit->append("\nServer started");
tcpServer = new QTcpServer(this);
ui->textEdit->append("\nServer listening for connections ....");
tcpServer->listen(QHostAddress::Any,80);
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(AcceptClientConn()));
}
//---------------- AcceptClientConn() ------------------
void serverwindow::AcceptClientConn()
{
ui->textEdit->append("\nNew client connection received");
clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
connect( clientConnection, SIGNAL(readyRead()),
this, SLOT(readClient()) );
}
//-------------------------------------- readClient() -------------------------
void serverwindow::readClient()
{
//read the data obtained from client
QTcpSocket* clientSocket = (QTcpSocket*)sender();
if(clientSocket->canReadLine())
{
QString curData(clientSocket->readLine());
QStringList tokens = curData.split(QRegExp("[ \r\n][ \r\n]*"));
if ( tokens[0] == "GET" )
{
//try to send a file's contents
//6. song file
QFile htmlfile("c:\\server_files\\song.mp3"); //3MB file
if (!htmlfile.open(QIODevice::ReadOnly))
return;
QByteArray block = htmlfile.readAll();
int sz = block.size();
QString content_type = "audio/mpeg;";
QTextStream os( clientSocket );
os << "HTTP/1.0 200 Ok\r\n"
"Content-Type: "<< content_type
<<"charset=\"utf-8\"\r\n"
<<"Content-Length: "<<sz
"\r\n\r\n";
os.flush();
// Streaming the file
clientSocket->write(block);
}
}
clientSocket->close();
}
QNetworkAccessManager * manager = new QNetworkAccessManager(this);
QNetworkRequest getReq;
getReq.setUrl( QUrl( fileurl ) );
QNetworkReply *reply = manager->get( getReq );
connect( reply, SIGNAL( readyRead()),this,SLOT( moreData()));
connect( reply, SIGNAL( error ( QNetworkReply::NetworkError ) ),
this,SLOT( errorInTransaction( QNetworkReply::NetworkError ) ) );
I have tested the client application using simple QT Http Server implementation ( which uses QTcpServer API ).
The client is able to download small sized files ( < 1MB ), but when I try to download large files ( > 1MB ) , there is an error on the server side & client application does not get the file contents.
This is the scenario when I run both applications on the phone. Has someone faced this issue and come up with probable solutions ?
The server code is as shown below –
serverwindow::serverwindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::serverwindow),clientConnection(0)
{
ui->setupUi(this);
ui->textEdit->append("\nServer started");
tcpServer = new QTcpServer(this);
ui->textEdit->append("\nServer listening for connections ....");
tcpServer->listen(QHostAddress::Any,80);
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(AcceptClientConn()));
}
//---------------- AcceptClientConn() ------------------
void serverwindow::AcceptClientConn()
{
ui->textEdit->append("\nNew client connection received");
clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
connect( clientConnection, SIGNAL(readyRead()),
this, SLOT(readClient()) );
}
//-------------------------------------- readClient() -------------------------
void serverwindow::readClient()
{
//read the data obtained from client
QTcpSocket* clientSocket = (QTcpSocket*)sender();
if(clientSocket->canReadLine())
{
QString curData(clientSocket->readLine());
QStringList tokens = curData.split(QRegExp("[ \r\n][ \r\n]*"));
if ( tokens[0] == "GET" )
{
//try to send a file's contents
//6. song file
QFile htmlfile("c:\\server_files\\song.mp3"); //3MB file
if (!htmlfile.open(QIODevice::ReadOnly))
return;
QByteArray block = htmlfile.readAll();
int sz = block.size();
QString content_type = "audio/mpeg;";
QTextStream os( clientSocket );
os << "HTTP/1.0 200 Ok\r\n"
"Content-Type: "<< content_type
<<"charset=\"utf-8\"\r\n"
<<"Content-Length: "<<sz
"\r\n\r\n";
os.flush();
// Streaming the file
clientSocket->write(block);
}
}
clientSocket->close();
}