2 Attachment(s)
Crash of the Server Application
Hi,
When I connect with the Server Application I receive the Crash Error on the line:
Code:
if( !socket->setSocketDescriptor( m_descriptor ) )
Please run the project "ServerTimer" than run "ViewerServerTimer" and click the button "Connect"
ServerTimer.pro
Code:
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
Code:
#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
#include "serverthread.h"
{
public:
Server();
protected:
void incomingConnection(int descriptor);
private:
ServerThread *thread;
};
#endif // SERVER_H
server.cpp
Code:
#include "server.h"
{
}
void Server::incomingConnection( int descriptor )
{
thread = new ServerThread( descriptor, this );
connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );
thread->start();
}
serverthread.h
Code:
#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:
};
#endif // SERVERTHREAD_H
serverthread.cpp
Code:
#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;
}
connect(timer, SIGNAL(timeout()), this, SLOT(sendData()));
timer->start(1000);
}
void ServerThread::sendData() {
//socket->write( "data" );
}
main.cpp
Code:
#include <QCoreApplication>
#include "server.h"
int main(int argc, char *argv[])
{
Server server;
{
qCritical( "Cannot listen to port 1234." );
return 1;
}
return a.exec();
}
Thank you
Re: Crash of the Server Application
Please, download and run my application. It's very important for me. Please, help me. I don't know where the mistake.
Re: Crash of the Server Application
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:
Code:
ServerThread
::ServerThread( int descriptor,
QObject *parent
): QThread( parent
), m_descriptor
( descriptor
), timer
( 0 ), socket
( 0 ) {
}
or
Code:
ServerThread
::ServerThread( int descriptor,
QObject *parent
):{
m_descriptor = descriptor;
timer = 0;
socket = 0;
}
You do not need threads in this example.
Re: Crash of the Server Application
Thank you!
I wrote it:
Code:
void ServerThread::run()
{
....
}
Quote:
You do not need threads in this example.
I will have the many clients.
Output:
Quote:
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
Re: Crash of the Server Application
Quote:
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.
Quote:
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.
Re: Crash of the Server Application
Quote:
Originally Posted by
ChrisW67
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…
- QThreads General Usage
I found the solution for my task: C++ Qt 70 Advanced Asynchronous QTcpServer with QThreadPool