I'm trying to get my MainWindow application launch a few TcpServer threads to wait for connections and go from there.
I am able to connect to the open port, but nothing gets triggered inside of Qt for me to go any further.
MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "serverlistener.h"
#include <QtNetwork/QTcpServer>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
if (!serverListener.
listen(QHostAddress::LocalHost,
5600)) { qCritical("Error opening port 5600.");
serverListener.close();
return;
}
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "serverlistener.h"
#include <QtNetwork/QTcpServer>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
if (!serverListener.listen(QHostAddress::LocalHost, 5600)) {
qCritical("Error opening port 5600.");
serverListener.close();
return;
}
}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "serverlistener.h"
class ServerListener;
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
ServerListener serverListener;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "serverlistener.h"
class ServerListener;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
ServerListener serverListener;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
ServerListener.cpp:
#include "serverlistener.h"
#include "mainwindow.h"
#include <iostream>
#include <QDebug>
using namespace std;
ServerListener
::ServerListener(QObject *parent
) :{
// serverSocket = new QTcpServer(this);
cout << "Listening" << endl;
}
ServerListener::~ServerListener() {
cout << "Destruction" << endl;
serverSocket->close();
}
void ServerListener::incomingConnection(int descriptor) {
cout << "Incoming connection" << endl;
//ServerThread *thread = new ServerThread(descriptor, this);
}
#include "serverlistener.h"
#include "mainwindow.h"
#include <iostream>
#include <QDebug>
using namespace std;
ServerListener::ServerListener(QObject *parent) :
QTcpServer(parent)
{
// serverSocket = new QTcpServer(this);
cout << "Listening" << endl;
}
ServerListener::~ServerListener() {
cout << "Destruction" << endl;
serverSocket->close();
}
void ServerListener::incomingConnection(int descriptor) {
cout << "Incoming connection" << endl;
//ServerThread *thread = new ServerThread(descriptor, this);
}
To copy to clipboard, switch view to plain text mode
ServerListener.h:
#ifndef SERVERLISTENER_H
#define SERVERLISTENER_H
#include <QtNetwork/QTcpServer>
#include <QObject>
{
Q_OBJECT;
public:
explicit ServerListener
(QObject *parent
= 0);
~ServerListener();
protected:
void incomingConnection(int descriptor);
private:
};
#endif // SERVERLISTENER_H
#ifndef SERVERLISTENER_H
#define SERVERLISTENER_H
#include <QtNetwork/QTcpServer>
#include <QObject>
class ServerListener : public QTcpServer
{
Q_OBJECT;
public:
explicit ServerListener(QObject *parent = 0);
~ServerListener();
protected:
void incomingConnection(int descriptor);
private:
QTcpServer *serverSocket;
};
#endif // SERVERLISTENER_H
To copy to clipboard, switch view to plain text mode
Here's my goal:
[listen1]<-------|MainWindow|------->[listen2]
[client1->listen1]
[client2->listen2]
[client3->listen1]
...
[client10->listen1]
[listen1]<-------|MainWindow|------->[listen2]
[client1->listen1]
[client2->listen2]
[client3->listen1]
...
[client10->listen1]
To copy to clipboard, switch view to plain text mode
Basically, I want to make sure the listening functions don't block the rest of the application. The program I'm making is quite advanced and this is my first time using Qt. I absolutely love the language and editor, which is why I chose it over VS2010 or VC#. I just hope I can accomplish what I have in mind (arrays/structs of sockets and structuring communication between them as needed).
Thanks.
Bookmarks