Hi,
When I connect with the Server Application I receive the Crash Error on the line:
if( !socket->setSocketDescriptor( m_descriptor ) )
if( !socket->setSocketDescriptor( m_descriptor ) )
To copy to clipboard, switch view to plain text mode
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
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
To copy to clipboard, switch view to plain text mode
server.h
#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
#include "serverthread.h"
{
public:
Server();
protected:
void incomingConnection(int descriptor);
private:
ServerThread *thread;
};
#endif // 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
To copy to clipboard, switch view to plain text mode
server.cpp
#include "server.h"
{
}
void Server::incomingConnection( int descriptor )
{
thread = new ServerThread( descriptor, this );
connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );
thread->start();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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:
};
#endif // 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
To copy to clipboard, switch view to plain text mode
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;
}
connect(timer, SIGNAL(timeout()), this, SLOT(sendData()));
timer->start(1000);
}
void ServerThread::sendData() {
//socket->write( "data" );
}
#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" );
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QCoreApplication>
#include "server.h"
int main(int argc, char *argv[])
{
Server server;
{
qCritical( "Cannot listen to port 1234." );
return 1;
}
return a.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Thank you
Bookmarks