Hello,
I'm writing an application to view MRI Images sent over a TCP connection. My program works well in a single thread but the problems arise when I try to run the server part in a separate thread.
I get the error "-1" when I try to set the descriptor of the socket :
if (clientSocket->setSocketDescriptor(socketId))
{
emit error(clientSocket->error());
cout << "setSocketDescriptor error : " << clientSocket->error() << endl;
return;
}
if (clientSocket->setSocketDescriptor(socketId))
{
emit error(clientSocket->error());
cout << "setSocketDescriptor error : " << clientSocket->error() << endl;
return;
}
To copy to clipboard, switch view to plain text mode
In the main window, I create a server object which starts listening :
cerr << "Failed to open bind port" << endl;
}
if (!server->listen(QHostAddress(QHostAddress::Any/*ipAddress*/), serverPort)) {
cerr << "Failed to open bind port" << endl;
}
To copy to clipboard, switch view to plain text mode
Here is my implementation of the Server class :
#include "server.h"
#include "serverthread.h"
#include <iostream>
using namespace std;
{
}
void Server::incomingConnection(int socketId)
{
cout << " socketID : " << socketId << endl;
ServerThread *serverThread = new ServerThread(socketId, NULL);
connect(serverThread, SIGNAL(finished()), serverThread, SLOT(deleteLater()));
emit clientConnected();
serverThread->start();
}
#include "server.h"
#include "serverthread.h"
#include <iostream>
using namespace std;
Server::Server(QObject *parent)
: QTcpServer(parent)
{
}
void Server::incomingConnection(int socketId)
{
cout << " socketID : " << socketId << endl;
ServerThread *serverThread = new ServerThread(socketId, NULL);
connect(serverThread, SIGNAL(finished()), serverThread, SLOT(deleteLater()));
emit clientConnected();
serverThread->start();
}
To copy to clipboard, switch view to plain text mode
The code of the ServerThread class :
#include "serverthread.h"
#include "clientsocket.h"
#include <QtNetwork>
#include <iostream>
using namespace std;
ServerThread
::ServerThread(int socketDescriptor,
QObject *parent
) : QThread(parent
), socketId
(socketId
) {
socketId = socketDescriptor;
}
void ServerThread::run()
{
cout << " socketID 2 : " << socketId << endl;
ClientSocket *clientSocket = new ClientSocket(/*this*/NULL);
if (clientSocket->setSocketDescriptor(socketId)) {
emit error(clientSocket->error());
cout << "setSocketDescriptor error : " << clientSocket->error() << endl;
return;
}
}
#include "serverthread.h"
#include "clientsocket.h"
#include <QtNetwork>
#include <iostream>
using namespace std;
ServerThread::ServerThread(int socketDescriptor, QObject *parent)
: QThread(parent), socketId(socketId)
{
socketId = socketDescriptor;
}
void ServerThread::run()
{
cout << " socketID 2 : " << socketId << endl;
ClientSocket *clientSocket = new ClientSocket(/*this*/NULL);
if (clientSocket->setSocketDescriptor(socketId)) {
emit error(clientSocket->error());
cout << "setSocketDescriptor error : " << clientSocket->error() << endl;
return;
}
}
To copy to clipboard, switch view to plain text mode
And finally the ClientSocket class :
#include <QtNetwork>
#include <cstdlib>
#include <iostream>
#include "clientsocket.h"
using namespace std;
ClientSocket
::ClientSocket(QObject *parent
){
connect(this, SIGNAL(readyRead()), this, SLOT(readClient()));
connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
}
void ClientSocket::readClient()
{
QFile dumpFile
("/home/meylan/QT/IRMServer/dump.log");
qint64 bytesDispo;
qint8 data;
while (this->waitForReadyRead(500)) {
bytesDispo = this->bytesAvailable();
cout << endl << "Taille du paquet : " << bytesDispo << endl;
for (int i = 0; i < bytesDispo; i++) {
in >> data;
dump << data;
}
}
dumpFile.close();
close();
}
{
switch (socketError) {
cout << "Remote host closed !" << endl;
break;
cout <<"Host not found !" << endl;
break;
cout << "Connection refused !" << endl;
break;
default:
cout << this->error() << endl;
}
}
#include <QtNetwork>
#include <cstdlib>
#include <iostream>
#include "clientsocket.h"
using namespace std;
ClientSocket::ClientSocket(QObject *parent)
: QTcpSocket(parent)
{
connect(this, SIGNAL(readyRead()), this, SLOT(readClient()));
connect(this, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
}
void ClientSocket::readClient()
{
QFile dumpFile("/home/meylan/QT/IRMServer/dump.log");
dumpFile.open(QIODevice::WriteOnly);
QDataStream dump(&dumpFile);
QDataStream in(this);
qint64 bytesDispo;
qint8 data;
in.setVersion(QDataStream::Qt_4_1);
while (this->waitForReadyRead(500)) {
bytesDispo = this->bytesAvailable();
cout << endl << "Taille du paquet : " << bytesDispo << endl;
for (int i = 0; i < bytesDispo; i++) {
in >> data;
dump << data;
}
}
dumpFile.close();
close();
}
void ClientSocket::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
cout << "Remote host closed !" << endl;
break;
case QAbstractSocket::HostNotFoundError:
cout <<"Host not found !" << endl;
break;
case QAbstractSocket::ConnectionRefusedError:
cout << "Connection refused !" << endl;
break;
default:
cout << this->error() << endl;
}
}
To copy to clipboard, switch view to plain text mode
Do you have any idea of what I'm doing wrong ?
Thanks in advance for your help.
Bookmarks