PDA

View Full Version : Crash of the Server Application



8Observer8
1st September 2013, 13:15
Hi,

When I connect with the Server Application I receive the Crash Error on the line:


if( !socket->setSocketDescriptor( m_descriptor ) )

Please run the project "ServerTimer" than run "ViewerServerTimer" and click the button "Connect"

ServerTimer.pro

QT += core
QT += network
QT -= gui

TARGET = ServerTimer
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
server.cpp \
serverthread.cpp

HEADERS += \
server.h \
serverthread.h


server.h


#ifndef SERVER_H
#define SERVER_H

#include <QTcpServer>
#include "serverthread.h"

class Server : public QTcpServer
{
public:
Server();

protected:
void incomingConnection(int descriptor);

private:
ServerThread *thread;
};

#endif // SERVER_H


server.cpp


#include "server.h"

Server::Server() : QTcpServer()
{
}

void Server::incomingConnection( int descriptor )
{
thread = new ServerThread( descriptor, this );

connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );
thread->start();
}


serverthread.h


#ifndef SERVERTHREAD_H
#define SERVERTHREAD_H

#include <QThread>
#include <QTimer>
#include <QTcpSocket>

class ServerThread : public QThread
{
public:
ServerThread( int descriptor, QObject *parent );

void run();

public slots:
void sendData();

private:
int m_descriptor;

private:
QTimer *timer;
QTcpSocket *socket;
};

#endif // SERVERTHREAD_H


serverthread.cpp


#include "serverthread.h"

ServerThread::ServerThread( int descriptor, QObject *parent ) : QThread( parent )
{
m_descriptor = descriptor;
}

void ServerThread::run()
{
if( !socket->setSocketDescriptor( m_descriptor ) )
{
qDebug( "Socket error!" );
return;
}

timer = new QTimer;
connect(timer, SIGNAL(timeout()), this, SLOT(sendData()));
timer->start(1000);
}

void ServerThread::sendData() {
//socket->write( "data" );
}


main.cpp


#include <QCoreApplication>
#include "server.h"

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

Server server;
if( !server.listen( QHostAddress::Any, 1234 ) )
{
qCritical( "Cannot listen to port 1234." );
return 1;
}

return a.exec();
}


Thank you

8Observer8
1st September 2013, 21:36
Please, download and run my application. It's very important for me. Please, help me. I don't know where the mistake.

ChrisW67
2nd September 2013, 00:50
The member variable 'socket' is never initialised to point a QTcpSocket allocated in free store. It is an invalid pointer when you try to use it in run().


Can I suggest you develop the habit of always initialising pointer member variables to 0 or a valid pointer in the class constructor initialisation list or body. A zero value in a pointer is immediately obvious on your debugger where the random value that might otherwise be there is not.

Something like:


ServerThread::ServerThread( int descriptor, QObject *parent ):
QThread( parent ), m_descriptor( descriptor ), timer( 0 ), socket( 0 )
{
}

or


ServerThread::ServerThread( int descriptor, QObject *parent ):
QThread( parent )
{
m_descriptor = descriptor;
timer = 0;
socket = 0;
}


You do not need threads in this example.

8Observer8
2nd September 2013, 05:11
Thank you!

I wrote it:


void ServerThread::run()
{
socket = new QTcpSocket;
....
}



You do not need threads in this example.
I will have the many clients.

Output:


QObject: Cannot create children for a parent that is in a different thread.
(Parent is QThread(0x547258), parent's thread is QThread(0x5416a8), current thre
ad is QThread(0x547258)
Object::connect: No such slot QThread::sendData() in ../ServerTimer/serverthread
.cpp:20

ChrisW67
2nd September 2013, 07:05
I will have the many clients.
That still does not require threads. If the typical response to a connection is short then adding threads probably complicate matters for no benefit.



QObject: Cannot create children for a parent that is in a different thread.
(Parent is QThread(0x547258), parent's thread is QThread(0x5416a8), current thread is QThread(0x547258)
Object::connect: No such slot QThread::sendData() in ../ServerTimer/serverthread.cpp:20

This is one of those complications.

Consult the Fortune Server Example and the Threaded Fortune Server examples in Assistant.

8Observer8
3rd September 2013, 10:36
Consult the Fortune Server Example and the Threaded Fortune Server examples in Assistant.

Thank you. I need to study about threads:
- You’re doing it wrong… (http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/)
- QThreads General Usage (http://qt-project.org/wiki/QThreads_general_usage)

I found the solution for my task: C++ Qt 70 Advanced Asynchronous QTcpServer with QThreadPool (http://voidrealms.com/viewtutorial.aspx?id=39)