QTcpSocket and multiples clients (signal issue) - c++

I'm developing a server that responds multiple connections from a client. But I have a problem where I have not found any solution.
I created a class (TcpSocket) for QTcpSocket, which separates the signals and slots for each connection.
But my server is not recognizing the signal of this class, it looks for QTcpSocket, and not by TcpSocket.
I'll post the code, maybe you understand better, because my English is not good.
tcpsocket.h
#ifndef TCPSOCKET_H
#define TCPSOCKET_H
#include <QObject>
#include <QtNetwork>
class TcpSocket: public QTcpSocket
{
Q_OBJECT
QTcpSocket *Socket;
public:
TcpSocket (QTcpSocket *);
virtual ~TcpSocket();
public slots:
void slotReadyRead();
void slotConnected();
void slotDisconnected();
signals:
void dataReady (TcpSocket *sckt);
void newConnection(TcpSocket *sckt);
void lostConnection(TcpSocket *sckt);
};
#endif // TCPSOCKET_H
tcpsocket.cpp
#include "tcpsocket.h"
TcpSocket::TcpSocket(QTcpSocket * socket)
: Socket (socket)
{
connect(Socket, SIGNAL(readyRead()), this, SLOT (slotReadyRead()));
connect(Socket, SIGNAL(connected()), this, SLOT(slotConnected()));
connect(Socket, SIGNAL(disconnected()), this, SLOT(slotDisconnected()));
}
TcpSocket::~TcpSocket()
{
}
void TcpSocket::slotReadyRead()
{
emit dataReady(this);
}
void TcpSocket::slotConnected()
{
emit newConnection(this);
}
void TcpSocket::slotDisconnected()
{
emit lostConnection(this);
}
server.h
#ifndef SERVER_H
#define SERVER_H
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include "custom/player.h"
#include "events.h"
#include "socketmanager.h"
#include "tcpsocket.h"
#include <QMessageBox>
namespace Ui {
class Server;
}
class Server : public QMainWindow
{
Q_OBJECT
public:
explicit Server(QWidget *parent = 0);
int max_connections;
~Server();
private slots:
...
void client_Disconnected(TcpSocket *socket);
void client_SendedBytes(qint64 bytes);
void client_GetBytes(TcpSocket* socket);
...
private:
Ui::Server *ui;
QTcpServer *server;
QList<TcpSocket *> client;
...
};
#endif // SERVER_H
server.cpp
int j; // connection count
void Server::server_Connected()
{
client.insert(j, (TcpSocket*)server->nextPendingConnection());
TcpSocket *sckt = client[j];
// error:
connect(sckt, SIGNAL(newConnection(TcpSocket*)), this, SLOT(client_GetBytes(TcpSocket*)));
connect(sckt, SIGNAL(lostConnection(TcpSocket*)), this, SLOT(client_Disconnected(TcpSocket*)));
QByteArray block;
QTextStream out(&block, QIODevice::WriteOnly);
out << "accepted";
ui->log->append(QString("Host connected: %1, index %2").arg(sckt->localAddress().toString()).arg(j));
std::string stdString = "accepted";
QByteArray byteArray(stdString.c_str(), stdString.length());
qint64 len = sckt->write(byteArray);
if(len != byteArray.size())
ui->log->append("Error!");
sckt->flush();
j++;
}
void Server::client_Disconnected(TcpSocket *socket)
{
...
}
void Server::client_GetBytes(TcpSocket *socket)
{
...
}
The error:
QObject::connect: No such signal QTcpSocket::newConnection(TcpSocket*) in ..\SOLEditorServer\server.cpp:45
QObject::connect: (receiver name: 'Server')
QObject::connect: No such signal QTcpSocket::lostConnection(TcpSocket*) in ..\SOLEditorServer\server.cpp:46
QObject::connect: (receiver name: 'Server')
also full code (server)
#include "server.h"
#include "ui_server.h"
#include <QHostAddress>
Server::Server(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Server)
{
max_connections = 20;
SM = new SocketManager();
ui->setupUi(this);
server = new QTcpServer(this);
connect(server, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(server_Error()));
connect(server, SIGNAL(newConnection()), this, SLOT(server_Connected()));
event = new Events(ui->log);
setWindowFlags(Qt::WindowCloseButtonHint);
}
Server::~Server()
{
delete ui;
}
void Server::on_pushButton_clicked()
{
QHostAddress host;
host.setAddress(ui->txt_ip->text());
server->listen(host, ui->txt_port->value());
ui->log->append("Connected!");
}
void Server::server_Error()
{
ui->log->append(server->errorString());
}
int j;
void Server::server_Connected()
{
client.insert(j, (TcpSocket*)server->nextPendingConnection());
TcpSocket *sckt = client[j];
connect(sckt, SIGNAL(newConnection(TcpSocket*)), this, SLOT(client_GetBytes(TcpSocket*)));
connect(sckt, SIGNAL(lostConnection(TcpSocket*)), this, SLOT(client_Disconnected(TcpSocket*)));
QByteArray block;
QTextStream out(&block, QIODevice::WriteOnly);
out << "accepted";
ui->log->append(QString("Host connected: %1, index %2").arg(sckt->localAddress().toString()).arg(j));
std::string stdString = "accepted";
QByteArray byteArray(stdString.c_str(), stdString.length());
qint64 len = sckt->write(byteArray);
if(len != byteArray.size())
ui->log->append("Error!");
sckt->flush();
j++;
}
void Server::client_Disconnected(TcpSocket *socket)
{
ui->log->append(QString("Desconectado. (%1)").arg(socket->localAddress().toString()));
client.removeAt(client.indexOf(socket));
}
void Server::client_SendedBytes(qint64 bytes)
{
QString dataxD = QString::number(bytes);
ui->log->append(QString("%1 bytes enviados.").arg(dataxD));
}
void Server::client_GetBytes(TcpSocket *socket)
{
QByteArray buffer;
buffer.resize(socket->bytesAvailable());
socket->read(buffer.data(), buffer.size());
QString data(buffer);
if(data.startsWith("REGISTER "))
{
QString received = data.split("REGISTER ")[1];
ui->log->append(received);
QString user = received.split(":")[0];
QString key = received.split(":")[1];
playerList.append(user);
playerKey.append(key);
event->eventNewPlayer(user);
SM->sendPacketToAll(client, QString("GREETING %1").arg(user));
} else if(data.startsWith("CHAT("))
{
QString UserData = data.split("CHAT(")[1].split(")")[0];
if(!checkUser(UserData))
{
ui->log->append("Username without a valid hash!");
return;
}
QString User = getUsernameFromData(UserData);
QString Message = data.split(QString("CHAT(%1) ").arg(UserData))[1];
event->eventNewChatmessage(Message, User);
SM->sendPacketToAll(client, QString("CHAT(%1) %2").arg(User).arg(Message));
}
}
bool Server::checkUser(QString usernamedata)
{
if(!usernamedata.contains(":"))
return false;
QString username = usernamedata.split(":")[0];
QString key = usernamedata.split(":")[1];
if(!playerList.contains(username) || !playerKey.contains(key))
return false;
int playerIndex = playerList.indexOf(username);
QString hashFromList = playerKey[playerIndex];
if(hashFromList != key)
return false;
return true;
}
bool Server::checkUser2(QString username, QString key)
{
if(!playerList.contains(username) || !playerKey.contains(key))
return false;
int playerIndex = playerList.indexOf(username);
QString hashFromList = playerKey[playerIndex];
if(hashFromList != key)
return false;
return true;
}
QString Server::getUsernameFromData(QString usernamedata)
{
if(!usernamedata.contains(":"))
return "Unknow";
QString username = usernamedata.split(":")[0];
return username;
}
QString Server::getUserkeyFromData(QString usernamedata)
{
if(!usernamedata.contains(":"))
return "Unknow";
QString key = usernamedata.split(":")[1];
return key;
}
void Server::on_pushButton_2_clicked()
{
/*std::string stdString = "BANNED";
QByteArray byteArray(stdString.c_str(), stdString.length());
clientConnection->write(byteArray);*/
}
full server.h
#ifndef SERVER_H
#define SERVER_H
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include "custom/player.h"
#include "events.h"
#include "socketmanager.h"
#include "tcpsocket.h"
#include <QMessageBox>
namespace Ui {
class Server;
}
class Server : public QMainWindow
{
Q_OBJECT
public:
explicit Server(QWidget *parent = 0);
int max_connections;
~Server();
private slots:
void on_pushButton_clicked();
void server_Error();
void server_Connected();
void client_Disconnected(TcpSocket *socket);
void client_SendedBytes(qint64 bytes);
void client_GetBytes(TcpSocket* socket);
void on_pushButton_2_clicked();
bool checkUser(QString usernamedata);
bool checkUser2(QString username, QString key);
QString getUsernameFromData(QString usernamedata);
QString getUserkeyFromData(QString usernamedata);
private:
Ui::Server *ui;
QTcpServer *server;
QList<TcpSocket *> client;
QStringList playerList;
QStringList playerKey;
Events *event;
SocketManager *SM;
};
#endif // SERVER_H

Fixed!
Modifications:
class TcpSocket: public QTcpSocket // old
class TcpSocket: public QObject // new
// old
void dataReady (TcpSocket *sckt);
void newConnection(TcpSocket *sckt);
void lostConnection(TcpSocket *sckt);
// new
void dataReady(QTcpSocket *sckt);
void newConnection(QTcpSocket *sckt);
void lostConnection(QTcpSocket *sckt);
// old
TcpSocket::TcpSocket(QTcpSocket * socket)
: Socket (socket)
{
connect(Socket, SIGNAL(readyRead()), this, SLOT (slotReadyRead()));
connect(Socket, SIGNAL(connected()), this, SLOT(slotConnected()));
connect(Socket, SIGNAL(disconnected()), this, SLOT(slotDisconnected()));
}
// new
TcpSocket::TcpSocket (QTcpSocket * socket)
: Socket (socket)
{
this->sock = socket;
connect(socket, SIGNAL(readyRead()), this, SLOT (slotReadyRead()));
connect(socket, SIGNAL(connected()), this, SLOT(slotConnected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(slotDisconnected()));
}
// old
void TcpSocket::slotReadyRead()
{
emit dataReady(this);
}
void TcpSocket::slotConnected()
{
emit newConnection(this);
}
void TcpSocket::slotDisconnected()
{
emit lostConnection(this);
}
// new
void TcpSocket::slotReadyRead()
{
emit dataReady(this->sock);
}
void TcpSocket::slotConnected()
{
emit newConnection(this->sock);
}
void TcpSocket::slotDisconnected()
{
emit lostConnection(this->sock);
}
// old
public:
TcpSocket (QTcpSocket *);
virtual ~TcpSocket();
// new
public:
TcpSocket (QTcpSocket *);
QTcpSocket *sock;
Done, now just use connect() function, like this:
QTcpSocket *socket = (your QTcpSocket);
TcpSocket *tcpSocket = new TcpSocket(socket);
connect(tcpSocket, SIGNAL(dataReady(QTcpSocket*)), this, SLOT(your_slot(QTcpSocket*)));
You really helped me, and sorry. Good luck, if anyone has the same problem that I had (not solved), please contact me!

Related

I have created a TCPSocket in QT, but how do i implement received data from QTCPSocket?

As the title says, i have created a client which receives data from my server. So far i can print it in my console, but i would like to work with it. (The data that the client recevies is from my Smartphones accelerometer sensor)
My Socket.h class:
#ifndef SOCKETTEST_H
#define SOCKETTEST_H
#include <QObject>
#include <QDebug>
#include <QTcpSocket>
#include <QAbstractSocket>
class SocketTest : public QObject
{
Q_OBJECT
public:
explicit SocketTest(QObject *parent = nullptr);
void Test();
signals:
public slots:
void connected();
void disconnected();
void bytesWritten( qint64 bytes);
void readyRead();
private:
QTcpSocket *socket;
};
#endif // SOCKETTEST_H
and socket.cpp :
#include "sockettest.h"
SocketTest::SocketTest(QObject *parent)
: QObject{parent}
{
}
void SocketTest::Test()
{
socket = new QTcpSocket(this);
connect (socket, SIGNAL(connected()),this, SLOT(connected()));
connect (socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
connect (socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
connect (socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
qDebug() <<"Connecting...";
socket->connectToHost("MY IP", PORT);
if(!socket->waitForConnected(2000))
{
qDebug() <<"Error" << socket->errorString();
}
}
void SocketTest::connected()
{
qDebug() <<"Connected!";
socket->write("\r\n\r\n\r\n\r\n");
}
void SocketTest::disconnected()
{
qDebug() <<"Disconnected!";
}
void SocketTest::bytesWritten(qint64 bytes)
{
qDebug() <<"We wrote: " <<bytes;
}
void SocketTest::readyRead()
{
qDebug() << socket->readAll();
}
typically what i want to create, in another class, is an if statment which moves an Object based on the sent data :
if( received_data == 5 ){
this->setX(this->x()+1);
}

Catching an incorrect link error QNetworkAccessManager

How to catch incorrect link errors? For example:
ftp://cddis.gsfc.nasa.gov/pub/slr/data/npt_crd/gracea/2010/gracea_20100101.npt.Z
-- this is incorrect link
Why doesn't any error signal work? Or How to determine the correct links? Please, write an example of correct work with errors.
#ifndef WIDGET_H
#define WIDGET_H
#include "Downloader.h"
#include <QWidget>
#include <QNetworkReply>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void onDownloadButtonClicked();
void onSelectTargetFolderButtonClicked();
void onCancelButtonClicked();
void onUpdateProgress(qint64 bytesReceived, qint64 bytesTotal);
void onResult(QNetworkReply *reply);
private:
Ui::Widget *ui;
Downloader m_downloader;
};
#endif // WIDGET_H
Widget.cpp
#include "Widget.h"
#include "ui_Widget.h"
#include <QFileDialog>
#include <QStandardPaths>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->downloadPushButton, &QPushButton::clicked, this, &Widget::onDownloadButtonClicked);
connect(ui->selectTargetFolderPushButton, &QPushButton::clicked, this, &Widget::onSelectTargetFolderButtonClicked);
connect(ui->cancelPushButton, &QPushButton::clicked, this, &Widget::onCancelButtonClicked);
connect(&m_downloader, &Downloader::updateDownloadProgress, this, &Widget::onUpdateProgress);
connect(&m_downloader.manager, &QNetworkAccessManager::finished, this, &Widget::onResult);
}
Widget::~Widget()
{
delete ui;
}
void Widget::onDownloadButtonClicked()
{
m_downloader.get(ui->targetFolderLineEdit->text(), ui->urlLineEdit->text());
}
void Widget::onSelectTargetFolderButtonClicked()
{
QString targetFolder = QFileDialog::getExistingDirectory(this,
tr("Select folder"),
QStandardPaths::writableLocation(QStandardPaths::DownloadLocation),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
ui->targetFolderLineEdit->setText(targetFolder);
}
void Widget::onCancelButtonClicked()
{
m_downloader.cancelDownload();
ui->downloadProgressBar->setMaximum(100);
ui->downloadProgressBar->setValue(0);
}
void Widget::onUpdateProgress(qint64 bytesReceived, qint64 bytesTotal)
{
ui->downloadProgressBar->setMaximum(bytesTotal);
ui->downloadProgressBar->setValue(bytesReceived);
}
void Widget::onResult(QNetworkReply *reply)
{
if (reply->error()) {
qDebug() << "Error";
qDebug() << reply->error();
} else {
qDebug() << "Done";
}
}
Downloader.h
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QNetworkAccessManager>
#include <QNetworkReply>
//class QNetworkReply;
class QFile;
class Downloader : public QObject
{
Q_OBJECT
using BaseClass = QObject;
public:
explicit Downloader(QObject* parent = nullptr);
bool get(const QString& targetFolder, const QUrl& url);
QNetworkReply* currentReply {nullptr};
QNetworkAccessManager manager;
QString fullName;
public slots:
void cancelDownload();
signals:
void updateDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
private slots:
void onReadyRead();
void onReply(QNetworkReply* reply);
void errorSlot(QNetworkReply::NetworkError er);
private:
QFile* m_file {nullptr};
};
#endif // DOWNLOADER_H
Downloader.cpp
#include "Downloader.h"
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QFile>
#include <QDir>
Downloader::Downloader(QObject* parent) :
BaseClass(parent)
{
connect(&manager, &QNetworkAccessManager::finished, this, &Downloader::onReply);
}
bool Downloader::get(const QString& targetFolder, const QUrl& url)
{
qDebug() << "get";
if (targetFolder.isEmpty() || url.isEmpty())
{
return false;
}
fullName = targetFolder + QDir::separator() + url.fileName();
m_file = new QFile(fullName);
if (!m_file->open(QIODevice::WriteOnly))
{
delete m_file;
m_file = nullptr;
return false;
}
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
currentReply = manager.get(request);
connect(currentReply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
this, &Downloader::errorSlot);
connect(currentReply, &QNetworkReply::readyRead, this, &Downloader::onReadyRead);
connect(currentReply, &QNetworkReply::downloadProgress, this, &Downloader::updateDownloadProgress);
return true;
}
void Downloader::onReadyRead()
{
qDebug() << "onReadyRead";
if (m_file)
{
m_file->write(currentReply->readAll());
}
}
void Downloader::cancelDownload()
{
qDebug() << "cancelDownload";
if (currentReply)
{
currentReply->abort();
}
}
void Downloader::onReply(QNetworkReply* reply)
{
qDebug() << "onReply";
if (reply->error() == QNetworkReply::NoError)
{
m_file->flush();
m_file->close();
}
else
{
m_file->remove();
}
delete m_file;
m_file = nullptr;
reply->deleteLater();
}
void Downloader::errorSlot(QNetworkReply::NetworkError er)
{
qDebug() << er;
}

Qt5 multi-threading: signals work in one-way only

Here: Qt multi-thread with GUI I've learned how to create a background worker (the Engine class).
In that class, I have a QSerialPort object, that runs in the main thread (see How to setup QSerialPort on a separate thread?).
I'm using the signal/slot mechanism to send/receive data. But it works in one-way only. The signals emitted from the Engine object ("worker thread") are received by the QSerialPort ("main thread"). The viceversa doesn't work: any signal emitted from QSerialPort is not received by the Engine.
engine.h
#ifndef ENGINE_H
#define ENGINE_H
#include <QObject>
#include <QTimer>
#include "myserial.h"
class Engine : public QObject
{
Q_OBJECT
public:
explicit Engine(QObject *parent = 0);
private:
QTimer m_timer;
MySerial m_serial;
signals:
void serialSendMessage(QByteArray data);
private slots:
void lineReceived(QByteArray line);
void foo();
public slots:
void run();
void open(QString port, quint32 baudrate);
void close();
};
#endif // ENGINE_H
engine.c
#include "engine.h"
#include <QDebug>
Engine::Engine(QObject *parent) : QObject(parent)
{
connect(&m_timer, &QTimer::timeout, this, &Engine::foo);
m_timer.setInterval(200);
}
// THIS IS NEVER EXECUTED!
void Engine::lineReceived(QByteArray line)
{
qDebug() << line;
}
// THIS IS RECEIVED BY QSERIALPORT
void Engine::foo()
{
emit serialSendMessage("Hello World!");
}
void Engine::run()
{
// THIS DOESN'T WORK!
connect(&m_serial, &MySerial::lineReceived, this, &Engine::lineReceived);
// THIS WORK!
connect(this, &Engine::serialSendMessage, &m_serial, &MySerial::sendMessage);
}
void Engine::open(QString port, quint32 baudrate)
{
m_serial.open(port, baudrate);
QTimer::singleShot(0, &m_timer, static_cast<void (QTimer::*)(void)>(&QTimer::start));
}
void Engine::close()
{
m_serial.close();
}
MySerial.h
#ifndef MYSERIAL_H
#define MYSERIAL_H
#include <QObject>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
class MySerial : public QSerialPort {
Q_OBJECT
public:
explicit MySerial(QObject *parent = 0);
bool open(QString port, quint32 baudrate);
using QSerialPort::open;
QByteArray sendMessage(QByteArray data, bool nmea);
signals:
void lineReceived(QByteArray line);
private slots:
void onReadyRead();
};
#endif // MYSERIAL_H
MySerial.c
#include "myserial.h"
#include <QDebug>
MySerial::MySerial(QObject *parent) : QSerialPort(parent) {
}
bool MySerial::open(QString port, quint32 baudrate)
{
disconnect(this, 0, 0, 0);
connect(this, &FemtoSerial::readyRead, this, &MySerial::onReadyRead);
setPortName(port);
if (!open(QIODevice::ReadWrite)) return false;
setDataBits(QSerialPort::Data8);
setParity(QSerialPort::NoParity);
setStopBits(QSerialPort::OneStop);
setBaudRate(baudrate);
setFlowControl(QSerialPort::NoFlowControl);
return true;
}
void MySerial::onReadyRead() {
static QList<QByteArray> lines;
static QByteArray buffer;
buffer += readAll();
int index = buffer.indexOf("\r");
while (index != -1) {
lines.append(buffer.left(index + 1));
buffer = buffer.mid(index + 1);
index = buffer.indexOf("\r");
}
// THIS SIGNAL IS EMITTED!
while (!lines.isEmpty()) emit lineReceived(lines.takeFirst());
}
QByteArray MySerial::sendMessage(QByteArray data) {
write(data);
return data;
}
EDIT
Trying to add a QEventLoop:
void Engine::run()
{
QEventLoop loop;
connect(&m_serial, &MySerial::lineReceived, this, &Engine::lineReceived);
connect(this, &Engine::serialSendMessage, &m_serial, &MySerial::sendMessage);
loop.exec();
}
The behavior is the same: the data is sent, but the receiving slot is never executed.
bool MySerial::open(QString port, quint32 baudrate)
{
//<s>disconnect(this, 0, 0, 0);</s> // <--- strikeout
connect(this, &FemtoSerial::readyRead, this, &MySerial::onReadyRead);
// ...
}
From the documentation:
Disconnect everything connected to an object's signals
disconnect(myObject, 0, 0, 0);
it means from the signals of myObject not the other way round.
That line prevents the slot in Engine to be executed because it has just disconnected from the source signal.
Engine might disconnect when the port closes, to avoid multiple connections on the next opening.

how to send an audio stream via tcp in a thread in Qt?

I'm trying to send an audio stream from a microphone via TCP in a thread other than the main thread. Therefore I've set up a server an I'm listening for incoming connections. Once a client connects, I want to send it the audio stream. I've read the Qt Documentation about QTcpServer, Multithreading, and QtMultimedia. I've also looked at this website for example code about multithreading and TCP servers in Qt. The problem is this message that I get during runtime:
QObject: Cannot create children for a parent that is in a different thread. (Parent is QNativeSocketEngine(0xae802a78), parent's thread is MyThread1(0x1856d28), current thread is QThread(0x1838aa0)
this is my code:
in file myserver.h
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QtNetwork>
#include <QTcpServer>
#include <mythread.h>
#include <QtMultimediaKit/QAudioFormat>
#include <QtMultimediaKit/QAudioInput>
#include <QtMultimediaKit/QAudioDeviceInfo>
class MyServer : public QTcpServer
{
Q_OBJECT
public:
explicit MyServer(QObject *parent = 0);
void startServer();
signals:
public slots:
protected:
void incomingConnection(int socketDescriptor);
private:
QAudioInput *audioInput;
};
#endif // MYSERVER_H
in file myserver.cpp
#include "myserver.h"
MyServer::MyServer(QObject *parent) :
QTcpServer(parent)
{
}
void MyServer::startServer()
{
int port = 12345;
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
for (int i = 0; i < ipAddressesList.size(); ++i)
{
if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address())
{
ipAddress = ipAddressesList.at(i).toString();
break;
}
}
if(!this->listen( QHostAddress(ipAddress),port ) )
{
qDebug() << "Could not start server";
}
else
{
qDebug() << "The server is running on\n\nIP: "<< ipAddress;
qDebug() << "\nport: " << this->serverPort() << "\n\n";
}
}
void MyServer::incomingConnection(int socketDescriptor)
{
qDebug() << socketDescriptor << " Connecting...";
QAudioFormat format;
format.setSampleRate(44100);
format.setChannelCount(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format))
{
qWarning()<<"default format not supported try to use nearest";
format = info.nearestFormat(format);
}
audioInput = new QAudioInput(format, this);
MyThread1 *thread = new MyThread1(socketDescriptor,audioInput, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
in file mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QTcpSocket>
#include <QDebug>
#include <QtMultimediaKit/QAudioInput>
class MyThread1 : public QThread
{
Q_OBJECT
public:
explicit MyThread1( int ID, QAudioInput *spk, QObject *parent = 0);
void run();
signals:
void error(QTcpSocket::SocketError socketerror);
public slots:
void sendData();
void disconnected();
private:
QTcpSocket *socket;
int socketDescriptor;
QAudioInput *speaker;
};
#endif // MYTHREAD_H
in file mythread.cpp
#include "mythread.h"
MyThread1::MyThread1( int ID, QAudioInput *spk, QObject *parent) :
QThread(parent)
{
this->socketDescriptor = ID;
this->speaker = spk;
}
void MyThread1::run()
{
qDebug() << " Thread started";
socket = new QTcpSocket();
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
emit error(socket->error());
return;
}
connect( speaker->start(), SIGNAL( readyRead() ), this, SLOT( sendData() ) , Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
qDebug() << socketDescriptor << " Microphone connected";
exec();
}
void MyThread1::sendData()
{
QByteArray Data = speaker->start()->readAll();
QDataStream l_vStream(&Data, QIODevice::WriteOnly);
l_vStream.setByteOrder(QDataStream::LittleEndian);
socket->write(Data, Data.length());
socket->waitForBytesWritten();
}
void MyThread1::disconnected( )
{
qDebug() << socketDescriptor << "Mic Disconnected";
speaker->stop();
delete speaker;
socket->deleteLater();
exit(0);
}
in file main.cpp
#include <QCoreApplication>
#include <myserver.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyServer server;
server.startServer();
return a.exec();
}
Any help would really be appreciated. Thanks.

Connection to server fails in qt

I created a small server/client application, and for testing I put the server/client function into a separate application.
The main client functions are
Client::Client(QString purpose) : networkSession(0)
{
Client::purpose = purpose;
tcpSocket = new QTcpSocket;
Client::blockSize = 0;
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired)
{
// Get saved network configuration
QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
settings.beginGroup(QLatin1String("QtNetwork"));
const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
settings.endGroup();
// If the saved network configuration is not currently discovered use the system default
QNetworkConfiguration config = manager.configurationFromIdentifier(id);
if ((config.state() & QNetworkConfiguration::Discovered) !=
QNetworkConfiguration::Discovered) {
config = manager.defaultConfiguration();
}
networkSession = new QNetworkSession(config, this);
connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
}
qDebug() << "Client set up, waiting";
}
void Client::connectToServer(QString ipAddr, quint32 port)
{
qDebug() << "Connecting to Host on port " << port << ' ' << (quint16)port;
tcpSocket->connectToHost(ipAddr, port);
emit this->connectionResult((tcpSocket->state() == QAbstractSocket::UnconnectedState)?false:true);
if (tcpSocket->waitForConnected(1000))
qDebug("Connected!");
qDebug() << "Am I connected" << tcpSocket->state();
std::cout << "Am I not connected" << tcpSocket->state();
}
and the server-functions:
Server::Server(QString ipAddr, quint32 port, QString purpose)
: tcpServer(0), networkSession(0)
{
Server::clientConnection = NULL;
Server::purpose = purpose;
Server::port = port;
QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
// Get saved network configuration
QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
settings.beginGroup(QLatin1String("QtNetwork"));
const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
settings.endGroup();
// If the saved network configuration is not currently discovered use the system default
QNetworkConfiguration config = manager.configurationFromIdentifier(id);
if ((config.state() & QNetworkConfiguration::Discovered) !=
QNetworkConfiguration::Discovered) {
config = manager.defaultConfiguration();
}
networkSession = new QNetworkSession(config, this);
connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
//statusLabel->setText(tr("Opening network session."));
networkSession->open();
} else {
sessionOpened();
}
//connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(openNewConnection()));
//connect(tcpServer, &QTcpServer::newConnection, this, &Server::openNewConnection);
}
void Server::sessionOpened()
{
// Save the used configuration
if (networkSession) {
QNetworkConfiguration config = networkSession->configuration();
QString id;
if (config.type() == QNetworkConfiguration::UserChoice)
id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
else
id = config.identifier();
QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
settings.beginGroup(QLatin1String("QtNetwork"));
settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id);
settings.endGroup();
}
tcpServer = new QTcpServer(this);
if (!tcpServer->listen(QHostAddress::Any, Server::port)) {
return;
}
qDebug() << "Server listening on: " << tcpServer->serverPort();
//! [0]
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i) {
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
break;
}
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
//! [1]
}
void Server::openNewConnection(void)
{
qDebug() << "New conn incoming!";
Server::clientConnection = tcpServer->nextPendingConnection();
QVariant ipAddr_QVar(clientConnection->peerAddress().toString());
qDebug() << "Got new connection!";
emit gotNewConnection(ipAddr_QVar);
}
The main problem here is that even if I get a "Connected" from
if (tcpSocket->waitForConnected(1000))
qDebug("Connected!");
qDebug() << "Am I connected" << tcpSocket->state();
in the client function, but the server function openNewConnection() never gets called. Why? How can I find the bug?
If a minimal working example is necessary, I can provide the whole code, but here I just wanted to provide the most important functions.
Edit:
client.h:
#ifndef CLIENT_H
#define CLIENT_H
//#include <QDialog>
#include <iostream>
#include <QTcpSocket>
class QComboBox;
class QDialogButtonBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QTcpSocket;
class QNetworkSession;
class Client : public QObject
{
Q_OBJECT
private:
QTcpSocket *tcpSocket;
QString currentFortune;
quint16 blockSize;
QPair<QString, QPair<QString, QVariant> > data;
QNetworkSession *networkSession;
QString purpose;
signals:
void gotData(QPair<QString, QPair<QString, QVariant> >);
void noConnection(void);
void connectionResult(bool);
void isDisconnect(bool);
public slots:
void displayError(QAbstractSocket::SocketError socketError);
void sessionOpened();
void getInfo();
void readData();
void connectToServer(QString ipAddr, quint32 port);
void disconnectFromServer();
private:
public:
Client(QString purpose);
};
#endif // CLIENT_H
server.h:
#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
#include <iostream>
//#include <QtTest/QTest>
#include <QSignalSpy>
#include <QTcpSocket>
#include <QDebug>
//#include <QMessageBox>
#include <QNetworkInterface>
#include <typeinfo>
#include <QStringList>
//#include <QSignalSpy>
QT_BEGIN_NAMESPACE
class QTcpServer;
class QNetworkSession;
QT_END_NAMESPACE
class Server : public QObject
{
Q_OBJECT
public slots:
void sessionOpened();
void sendFortune(void);
void sendData(QPair<QString, QPair<QString, QVariant> > data);
void sendFile(QVariant fileName);
void disconnectServer(void);
void openNewConnection(void);
signals:
void gotNewConnection(QVariant);
private:
QString purpose;
QTcpServer *tcpServer;
QString ipAddr;
quint32 port;
QTcpSocket *clientConnection;
quint32 BlockSize;
bool firstTime;
QSignalSpy * m_pSignalSpy;
QStringList fortunes;
QNetworkSession *networkSession;
//QNetworkConfiguration config;
public:
Server(QString ipAddr, quint32 port, QString purpose = "");
};
#endif // SERVER_H
The problem lies with your QNetworkSession, you have declared it in scope. Meaning once you leave that function the QNetworkSession gets destroyed. A destroyed object cannot emit a signal. Maybe make it a member variable or construct it in your header.