I'm trying to integrate a QTcpServer into my program. My initial attempt was to use the Fortune client to connect to my server, and my server return a simple string. I'm not sure why, but the Fortune client will not connect.
I'm running both my program and the Fortune Client on my computer, using a connection address of Localhost and port 1000.
Here is my code:
Server.h
#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
{
Q_OBJECT
public:
protected slots :
void incomingConnection(int socketDescriptor);
};
#endif // SERVER_H
#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
class Server : public QTcpServer
{
Q_OBJECT
public:
Server(QObject *parent = 0);
protected slots :
void incomingConnection(int socketDescriptor);
};
#endif // SERVER_H
To copy to clipboard, switch view to plain text mode
Server.cpp
#include "Server.h"
#include "ServerThread.h"
#include <QDebug>
{
connect(this, SIGNAL(newConnection()), this, SLOT(incommingConnection(socketDescriptor)));
}
void Server::incomingConnection(int socketDescriptor)
{
qDebug() << "Incomming!";
ServerThread *thread = new simonServerThread(socketDescriptor, fortune, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
#include "Server.h"
#include "ServerThread.h"
#include <QDebug>
Server::Server(QObject *parent)
: QTcpServer(parent)
{
connect(this, SIGNAL(newConnection()), this, SLOT(incommingConnection(socketDescriptor)));
}
void Server::incomingConnection(int socketDescriptor)
{
qDebug() << "Incomming!";
QString fortune = "Test";
ServerThread *thread = new simonServerThread(socketDescriptor, fortune, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
To copy to clipboard, switch view to plain text mode
ServerThread.h
#ifndef SERVERTHREAD_H
#define SERVERTHREAD_H
#include <QThread>
#include <QTcpSocket>
class ServerThread
: public QThread {
Q_OBJECT
public:
ServerThread
(int socketDescriptor,
const QString &fortune,
QObject *parent
);
void run();
signals:
private:
int socketDescriptor;
};
#endif // SERVERTHREAD_H
#ifndef SERVERTHREAD_H
#define SERVERTHREAD_H
#include <QThread>
#include <QTcpSocket>
class ServerThread : public QThread
{
Q_OBJECT
public:
ServerThread(int socketDescriptor, const QString &fortune, QObject *parent);
void run();
signals:
void error(QTcpSocket::SocketError socketError);
private:
int socketDescriptor;
QString text;
};
#endif // SERVERTHREAD_H
To copy to clipboard, switch view to plain text mode
ServerThread.cpp
#include "ServerThread.h"
ServerThread
::ServerThread(int socketDescriptor,
const QString &fortune,
QObject *parent
) : QThread(parent
), socketDescriptor
(socketDescriptor
), text
(fortune
) {
}
void ServerThread::run()
{
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket.error());
return;
}
out << (quint16)0;
out << text;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket.write(block);
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}
#include "ServerThread.h"
ServerThread::ServerThread(int socketDescriptor, const QString &fortune, QObject *parent)
: QThread(parent), socketDescriptor(socketDescriptor), text(fortune)
{
}
void ServerThread::run()
{
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket.error());
return;
}
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << text;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket.write(block);
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}
To copy to clipboard, switch view to plain text mode
I'm calling the server using the following:
Server server;
QMessageBox::critical(this, tr
("Threaded Fortune Server"),
tr("Unable to start the server: %1.")
.arg(server.errorString()));
server.close();
}
Server server;
if (!server.listen(QHostAddress::Any, 1000)) {
QMessageBox::critical(this, tr("Threaded Fortune Server"),
tr("Unable to start the server: %1.")
.arg(server.errorString()));
server.close();
}
To copy to clipboard, switch view to plain text mode
Thanks for trying to help!
Bookmarks