Okay, I tried something similar to the Threaded Fortune server example. The end result of the code was this -
hostingserver.h ( subclass of QTcpserver )
Q_OBJECT
public:
hostingserver
(QObject *parent
= 0);
~hostingserver();
protected:
void incomingConnection(int socketDescriptor);
private:
};
class hostingserver : public QTcpServer {
Q_OBJECT
public:
hostingserver(QObject *parent = 0);
~hostingserver();
protected:
void incomingConnection(int socketDescriptor);
private:
};
To copy to clipboard, switch view to plain text mode
hostingserver.cpp -
hostingserver
::hostingserver(QObject *parent
) :{
}
hostingserver::~hostingserver()
{
}
void hostingserver::incomingConnection(int socketDescriptor)
{
clienthandler *thread = new clienthandler(socketDescriptor,this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
hostingserver::hostingserver(QObject *parent) :
QTcpServer(parent)
{
}
hostingserver::~hostingserver()
{
}
void hostingserver::incomingConnection(int socketDescriptor)
{
clienthandler *thread = new clienthandler(socketDescriptor,this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
To copy to clipboard, switch view to plain text mode
the clienthandler is a QThread -
class clienthandler
: public QThread{
Q_OBJECT
public:
clienthandler
(int socketDescriptor,
QObject *parent
);
~clienthandler();
public: //implemented from QRunnable
void run();
signals:
private:
int socketDescriptor;
};
class clienthandler : public QThread
{
Q_OBJECT
public:
clienthandler(int socketDescriptor, QObject *parent);
~clienthandler();
public: //implemented from QRunnable
void run();
signals:
void error(QTcpSocket::SocketError socketError);
private:
int socketDescriptor;
};
To copy to clipboard, switch view to plain text mode
clienthandler implementation -
clienthandler::clienthandler(
int aSocketDescriptor,
QObject *parent
) : QThread(parent
),socketDescriptor
(aSocketDescriptor
) {
}
clienthandler::~clienthandler()
{
}
void clienthandler::run()
{
QFile fn
("c:\\server_files\\server_log.txt");
fn.
write(QString("run impl called").
toAscii());
if (!clientConnSocket.setSocketDescriptor(socketDescriptor)) {
emit error(clientConnSocket.error());
return;
}
fn.
write(QString("\nsetSocketDescriptor called").
toAscii());
bool isCorrect = clientConnSocket.canReadLine();
if( !isCorrect )
fn.
write(QString("\nLine cannot be read").
toAscii());
if(clientConnSocket.canReadLine())
{
QString curData
(clientConnSocket.
readLine());
if ( tokens[0] == "GET" )
{
//try to send a file's contents
fn.
write(QString("GET obtained").
toAscii());
//1. Small sized html file
QFile htmlfile
("c:\\server_files\\index.html");
return;
QString content_type
= "text/html;";
//os.setAutoDetectUnicode(true);
os << "HTTP/1.0 200 Ok\r\n"
"Content-Type: "
<< content_type
<<"charset=\"utf-8\"\r\n"
<<"\r\n";
os.flush();
// Streaming the file
clientConnSocket.write(block);
}
}
clientConnSocket.disconnectFromHost();
fn.close();
}
clienthandler::clienthandler(
int aSocketDescriptor,QObject *parent) :
QThread(parent),socketDescriptor(aSocketDescriptor)
{
}
clienthandler::~clienthandler()
{
}
void clienthandler::run()
{
QFile fn("c:\\server_files\\server_log.txt");
fn.open(QIODevice::WriteOnly | QIODevice::Text);
fn.write(QString("run impl called").toAscii());
QTcpSocket clientConnSocket;
if (!clientConnSocket.setSocketDescriptor(socketDescriptor)) {
emit error(clientConnSocket.error());
return;
}
fn.write(QString("\nsetSocketDescriptor called").toAscii());
bool isCorrect = clientConnSocket.canReadLine();
if( !isCorrect )
fn.write(QString("\nLine cannot be read").toAscii());
if(clientConnSocket.canReadLine())
{
QString curData(clientConnSocket.readLine());
QStringList tokens = curData.split(QRegExp("[ \r\n][ \r\n]*"));
if ( tokens[0] == "GET" )
{
//try to send a file's contents
fn.write(QString("GET obtained").toAscii());
//1. Small sized html file
QFile htmlfile("c:\\server_files\\index.html");
if (!htmlfile.open(QIODevice::ReadOnly))
return;
QString content_type = "text/html;";
QTextStream os( &clientConnSocket );
//os.setAutoDetectUnicode(true);
os << "HTTP/1.0 200 Ok\r\n"
"Content-Type: "
<< content_type
<<"charset=\"utf-8\"\r\n"
<<"\r\n";
os.flush();
// Streaming the file
QByteArray block = htmlfile.readAll();
clientConnSocket.write(block);
}
}
clientConnSocket.disconnectFromHost();
fn.close();
}
To copy to clipboard, switch view to plain text mode
clientConnSocket.canReadLine() always returns false in the above code. Whta should I do for this ?
Bookmarks