Multi clients not working properly on Qt4 Multi thread server? - c++

Friends! I have written a code for a multi threaded TCP server in Qt4. The server is constantly listening for remote client connection requests. Whenever the client connects to the server, it takes its IP, Name and Port number and verifies if it is a recognized (authorized) client (that can be checked through a pre-stored text file having names, IPs and port numbers of all registered clients). If it is, the communication will proceed. Otherwise, the server terminates the connection.
EDIT1: here is the excerpt from myThread.cpp
void MyThread::run()
{
// Thread starts here
qDebug() << socketDescriptor << ("Starting thread...");
socket = new QTcpSocket();
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
emit error(socket->error());
return;
}
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection);
qDebug() << socketDescriptor << ("Client connected!");
exec();
}
ISSUE: Now the problem is when I run the program and connects the first client it works fine. But as soon as the 2nd client is connected (testing through telnet, for now) it starts behaving strange (no messages receiving from client 2 or client 2 completely freezing etc). Can someone test the code in their machine?
myserver.cpp
#include "myserver.h"
#include "ioprogram.h"
#include <QTcpSocket>
#include <string>
#include <iostream>
using namespace std;
MyServer::MyServer(QObject *parent): QTcpServer(parent)
{
QStringList accepted_ip_list; //List of remote IPs that can be accepted in QString list format
accepted_ip_list.append("127.0.0.1"); // IPv4 local address
accepted_ip_list.append("::1"); // IPv6 local address
accepted_ip_list.append("172.18.48.139");
// Convert from QString to integer format, generating new list
foreach (const QString &ip, accepted_ip_list)
{
QHostAddress host_address(ip);
my_accepted_ip_list.append(host_address);
}
/*
myserver = new QTcpServer(this);
connect(myserver, &QTcpServer::incomingConnection, this, &MyServer::incomingConnection);
myserver->listen(QHostAddress::Any, 1234);
*/
}
void MyServer::startServer()
{
if(!this->listen(QHostAddress::Any,1234))
{
qDebug() << "Could not start server.";
}
else
{
qDebug() << "Listening...";
}
}
void MyServer::incomingConnection(qintptr socketDescriptor)
{
QTcpSocket *socket = new QTcpSocket(this);
socket->setSocketDescriptor(socketDescriptor);
qDebug() << socketDescriptor << "Connecting...";
QHostAddress host_address = socket->peerAddress();
//quint32 ipv4 = host_address.toIPv4Address();
std::string ipv4 = host_address.toString().toStdString();
QByteArray ipv6 = QByteArray((char*)host_address.toIPv6Address().c, 16);
IOProgram test;
bool status= test.checkAuthenticity(ipv4);
if(status == false){
/*
while (myserver->hasPendingConnections())
{
QTcpSocket *socket = myserver->nextPendingConnection();
QHostAddress host_address = socket->peerAddress();
bool contains = false;
*/
for(int i=0; i < my_accepted_ip_list.size(); i++)
{
//quint32 accepted_ipv4 = my_accepted_ip_list[i].toIPv4Address();
std::string accepted_ipv4 = my_accepted_ip_list[i].toString().toStdString();
if(accepted_ipv4 == ipv4)
{
status = true;
qDebug() << "testing...";
break;
}
/* else
{
qDebug() << "Rejected!";
socket->abort(); // Reject peer by disconnecting it
socket->deleteLater(); // Schedule the socket removal from memory
}
*/
/*if(my_accepted_ip_list[i].isEqual(host_address,QHostAddress::ConvertV4MappedToIPv4))
{
contains = true;
break;
}*/
}
if(!socket->QTcpSocket::ConnectedState){
socket->abort(); // Reject peer by disconnecting it
socket->deleteLater(); // Schedule the socket removal from memory
}
if(status)
{
qDebug() << "Accepted!";
MyThread *thread = new MyThread(socketDescriptor, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
}
}
ioprogram.cpp
#include "ioprogram.h"
#include <QFile>
#include <QDebug>
#include <QTextStream>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
struct Data {
string Name;
string IP;
string Port;
};
vector<Data> AllData;
IOProgram::IOProgram()
{
}
void Read()
{
QFile file("C:/Users/Shahwani/Documents/Bremen/Sem3/Qt/MultiServerAugust/NodesList.txt");
if(file.open(QIODevice::ReadWrite | QIODevice::Text))
{
QTextStream stream(&file);
QString line;
do
{
line = stream.readLine();
qDebug() << line;
}while(!line.isNull());
file.close();
}
}
string IOProgram::inpop()
{
ifstream inFile;
inFile.open("NodesList.txt");
// inFile.clear();
// Check for error
if(inFile.fail()){
cerr << "Error opening file" << endl;
exit(1);
}
int count = 0;
string line;
char delim = '\t';
getline (inFile,line);
// Read the file until the end
while(!inFile.eof())
{
getline (inFile,line);
string token;
stringstream stream(line); // Splitting a string using a delimiter
vector<string> vect;
while(getline(stream, token, delim) )
{
vect.push_back(token);
}
Data d;
d.Name = vect[0];
d.IP = vect[1];
d.Port = vect[2];
vect.clear();
AllData.push_back(d);
count++;
}
cout << count << " nodes found!" << endl;
inFile.close();
// Check for authenticity and only allow the recognised IPs
// Write the active nodes in a separate file on the disk
ofstream outFile;
outFile.open("sample.txt");
Data activeList;
for(int i = 0; i < AllData.size(); i++)
{
activeList = AllData[i];
outFile << "Name: " << activeList.Name << "\tIP: " << activeList.IP << "\tPort: " << activeList.Port <<endl;
}
outFile.close();
return activeList.IP;
}
bool IOProgram::checkAuthenticity(string ip)
{
for(int i=0;i<AllData.size();i++)
{
Data check;
check = AllData[i];
if(ip == check.IP)
{
return true;
}
else
{
return false;
}
}
}
I guess I cannot put a lot of code in one question. Let me know in case someone needs rest of the files like mythread.cpp or header files to understand the issue and how I can share them.

Related

QTcpSocket emulate netcat 'conversation'

In my linux terminal I have the following 'conversation' that I want to run from qt using QTcpSocket:
S user#domain:~ $ netcat 1.1.1.2 9230
R HELO SOME MORE INFORMATION ABOUT THE DEVICE I CONNECTED TO
S DEVC 1.1.1.1 somePassword
R DEVC OK
S #AlarmCode=2,12
Where S is sended data and R is received data.
I made the following code, my header file:
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QTcpSocket>
#include <QObject>
class TCPClient : public QObject
{
Q_OBJECT
private:
QTcpSocket *tcpSocket = nullptr;
void handleHeloMessage(QString message);
void handleConnectToDeviceMessage(QString message);
void handleMessage(QString message);
bool handledHeloMessage = false;
bool handledConnectToDeviceMessage = false;
void sendDeviceToConnectTo();
private slots:
void onConnected();
void handleReadyRead();
public:
TCPClient();
};
#endif // TCPCLIENT_H
My cpp:
#include "tcpclient.h"
#include <QDataStream>
#include <QDebug>
const QString COMMAND_RESET_ALARM = "#AlarmCode=1,-";
const QString COMMAND_CONNECT_TO_DEVICE = "DEVC 1.1.1.1 somePassword";
const QString IP_DATA_SOURCE = "1.1.1.2";
const int PORT_DATA_SOURCE = 9230;
#include <QDebug>
TCPClient::TCPClient()
{
tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(connected()), this, SLOT(onConnected()));
connect(tcpSocket, SIGNAL(readyRead()), SLOT(handleReadyRead()));
tcpSocket->connectToHost(IP_DATA_SOURCE, PORT_DATA_SOURCE);
}
void TCPClient::handleHeloMessage(QString message)
{
if(message.left(4) == "HELO")
{
handledHeloMessage = true;
qDebug() << "Handled HELO \n" << message;
sendDeviceToConnectTo();
}
else{
qDebug() << "We did not receive halo but \n" << message;
}
}
void TCPClient::handleConnectToDeviceMessage(QString message)
{
qDebug() << "RECEIVED DEVICE\n" << message;
}
void TCPClient::handleMessage(QString message)
{
qDebug() << "We received data. "<< message;
}
void TCPClient::sendDeviceToConnectTo()
{
QByteArray br = COMMAND_CONNECT_TO_DEVICE.toUtf8();
tcpSocket->write(br);
qDebug() << "Sending: " << br;
}
void TCPClient::onConnected()
{
qDebug() << "we are now connected.";
}
void TCPClient::handleReadyRead()
{
QByteArray dataReceived = tcpSocket->readAll();
QString dataReceivedString = QString::fromStdString(dataReceived.toStdString());
if(!handledHeloMessage)
{
handleHeloMessage(dataReceivedString);
}
else if(!handledConnectToDeviceMessage)
{
handleConnectToDeviceMessage(dataReceivedString);
}
else{
handleMessage(dataReceivedString);
}
}
When I run my program I get:
we are now connected.
Handled HELO
"HELO SOME MORE INFORMATION ABOUT THE DEVICE I CONNECTED TO"
Sending: "DEVC 1.1.1.1 somePassword"
I was expecting to receive DEVC OK next but the above part is all I get.
Why dont I receive DEVC OK like the example from the terminal?
I found the problem, the message was not written until a new line was entered.
adding \n to the message did the job.

Can I use QUdpSockets wo polling or custom classes in Qt?

Here is a short UDP server example in Qt below which does work but what I don't like is that I'm polling to see if new data is available. I've come across some examples of a readyRead() but they all seem to introduce a qt class. Do I need to use a qt class in order to take advantage of the readyRead() signal?
Here is the working but simple UDP server implemented entirely in main:
#include <QDebug>
#include <QUdpSocket>
#include <QThread>
int main(int argc, char *argv[])
{
QUdpSocket *socket = new QUdpSocket();
u_int16_t port = 7777;
bool bindSuccess = socket->bind(QHostAddress::AnyIPv4, port);
if (!bindSuccess) {
qDebug() << "Error binding to port " << port << " on local IPs";
return a.exec();
}
qDebug() << "Started UDP Server on " << port << endl;
QHostAddress sender;
while (true) {
while (socket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(socket->pendingDatagramSize());
socket->readDatagram(datagram.data(),datagram.size(),&sender,&port);
qDebug() << "Message From :: " << sender.toString();
qDebug() << "Port From :: "<< port;
qDebug() << "Message :: " << datagram.data();
}
QThread::msleep(20);
}
return 0;
}
Here is an example of the readyRead() signal:
https://www.bogotobogo.com/Qt/Qt5_QUdpSocket.php
I haven't really figured out how to get this to work yet. I must be doing something wrong. Here is the UDP connection code i'm trying:
#include "myudp.h"
MyUDP::MyUDP(QObject *parent) : QObject(parent) {
}
void MyUDP::initSocket(u_int16_t p) {
port = p;
udpSocket = new QUdpSocket(this);
bool bindSuccess = udpSocket->bind(QHostAddress::LocalHost, port);
if (!bindSuccess) {
qDebug() << "Error binding to port " << port << " on local IPs";
return;
}
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
}
void MyUDP::readPendingDatagrams() {
QHostAddress sender;
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &port);
qDebug() << "Message From :: " << sender.toString();
qDebug() << "Port From :: " << port;
qDebug() << "Message :: " << datagram.data();
}
}
myudp.h
#include <QObject>
#include <QUdpSocket>
class MyUDP : public QObject
{
Q_OBJECT
public:
explicit MyUDP(QObject *parent);
void initSocket(u_int16_t p);
u_int16_t port;
QUdpSocket *udpSocket;
signals:
public slots:
void readPendingDatagrams();
};
new main.cpp
int main(int argc, char *argv[])
{
MyUDP *myUDP = new MyUDP(0);
myUDP->initSocket(port);
while (true) {
usleep(1000);
}
return 0;
}
I am testing with:
netcat 127.0.0.1 -u 7777
{"cid"="0x1234123412341", "fill_level"=3245 }<cr>
What you're doing wrong is that you're not letting Qt's event loop run. i.e. this is incorrect:
int main(int argc, char *argv[])
{
MyUDP *myUDP = new MyUDP(0);
myUDP->initSocket(port);
while (true) {
usleep(1000);
}
return 0;
}
... instead, you should have something like this:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// connect needs to occur after QCoreApplication declaration
MyUDP *myUDP = new MyUDP(0);
myUDP->initSocket(port);
return app.exec();
}
... it is inside the app.exec() call where a Qt application spends most of its time (app.exec() won't return until Qt wants to quit), and there is where Qt will handle your UDP socket's I/O and signaling needs.
Please modify your processPendingDatagrams like this, to let newer incoming data be processed:
void MyUDP::readPendingDatagrams() {
QHostAddress sender;
uint16_t port;
QByteArray datagram; // moved here
while (udpSocket->hasPendingDatagrams()) {
//QByteArray datagram; // you don't need this here
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &port);
qDebug() << "Message From :: " << sender.toString();
qDebug() << "Port From :: " << port;
qDebug() << "Message :: " << datagram.data();
}
// God knows why, there is always one more "dummy" readDatagram call to make,
// otherwise no new readyRead() will be emitted, and this function would never be called again
datagram.resize(udpSocket->pendingDatagramSize());
socket->readDatagram(datagram.data(),datagram.size(),&sender,&port);
}

Modify "Modbus TCP" to "Modbus RTU" with QT - QModbusTcpClient

I would have to send Modbus request for specific data, my problem is that I have to use mobile communication, wifi, connect to a custom electronic card, which is right in Modbus RTU.
My working code connecting to the electronic board:
#include "connessione.h"
#include <QModbusTcpClient>
#include <QVariant>
#include <QModbusDataUnit>
#include <QDebug>
connessione::connessione(QObject *parent) : QObject(parent)
{
qDebug() << "here debug " << "ok";
clientX = new QModbusTcpClient();
clientX->setConnectionParameter(QModbusDevice::NetworkAddressParameter, "192.168.222.1");
clientX->setConnectionParameter(QModbusDevice::NetworkPortParameter, 5555);
if (clientX->connectDevice())
{
qDebug() << "connected: " << clientX->state();
}
else
{
qDebug() << "ERRORE" << clientX->errorString();
}
}
void connessione::clickButton(){
QModbusDataUnit readUnit(QModbusDataUnit::HoldingRegisters, 0, 1); // just read input register 40006
//qDebug() << "readUnit" << readUnit.RegisterType;
qDebug() << "readUnit" << clientX->state();
if (auto *reply = clientX->sendReadRequest(readUnit, 255)) // client id 255
{
if (!reply->isFinished())
{
// connect the finished signal of the request to your read slot
qDebug() << "connected" << reply->errorString();
connect(reply, &QModbusReply::finished, this, &connessione::readReady);
}
else
{
qDebug() << "Errore" << reply->errorString();
delete reply; // broadcast replies return immediately
}
}
else
{
qDebug() << "Errore" << reply->errorString();
// request error
}
}
void connessione::readReady()
{
QModbusReply *reply = qobject_cast<QModbusReply *>(sender());
if (!reply)
return;
if (reply->error() == QModbusDevice::NoError)
{
const QModbusDataUnit unit = reply->result();
int startAddress = unit.startAddress(); // the start address,
int value = unit.value(0); // value of the start address + 0
qDebug() << "NESSUN ERRORE" << reply->errorString();
}
else
{
qDebug() << "Errore readReady" << reply->errorString();
// reply error
}
reply->deleteLater(); // delete the reply
}
log string TCP sent:
D/libmodbusMobile.so( 8042): (null):0 ((null)): qt.modbus: (TCP
client) Sent TCP PDU: 0x0300000001 with tId: 2
this is right: 0x0300000001
But unfortunately, my electronic card, the integrated firmware I can not modify, is right with Modbus RTU, so I should change 0x0300000001 to 0x010300000001C1C2 where C1 and C2 are the checksums.
I believe that QModbusDataUnit generate buffer to send. So how to change it? Exist manual solution where I build the buffer?
how to change it and create custom send buffer like the example?
Thanks

Qt UDP read error

I try to write a little client-server program. A client should detect all servers running in the same LAN. I tried to implement a UDP-Broadcast with qt but my read function returns -1 all the time. If I call the dataAvailable()-function on the socket, it says that 4 bytes are available to read, but for some reason it fails to read them.
For now I'm trying to receive the broadcast on the same machine and in the same program
Here's my main:
#include <iostream>
#include <QString>
#include <QThread>
#include "../include/network.h"
using namespace std;
using namespace network;
int main () {
Network *n = new Network ();
n->broadcast("test");
if (n->dataAvailable()) {
cout << "Data available: ";
cout << n->getData().toStdString() << std::endl;
} else {
cout << "No data" << endl;
}
delete n;
}
and my Network-class:
#ifndef NETWORK
#define NETWORK
#include <QObject>
#include <QUdpSocket>
#include <iostream>
namespace network {
class Network : public QObject {
Q_OBJECT
public:
const static int BROADCAST_PORT = 52433;
explicit Network(QObject *parent = 0) {
socket = new QUdpSocket ();
socket->bind (QHostAddress::Any, BROADCAST_PORT);
}
~Network () {
delete socket;
}
bool dataAvailable () {
return socket->hasPendingDatagrams();
}
QString getData () {
if (!dataAvailable()) {
return "";
}
char *data = 0;
std::cout << socket->pendingDatagramSize() << std::endl;
std::cout << QString("recv: %1")
.arg(socket->readDatagram(data,
socket->pendingDatagramSize())).toStdString();
return QString(data);
}
void broadcast(QString data) {
QUdpSocket *broadcast = new QUdpSocket ();
broadcast->connectToHost(QHostAddress::Broadcast, BROADCAST_PORT);
std::cout << broadcast->write(data.toStdString().c_str())
<< std::endl << std::endl;
delete broadcast;
}
private:
QUdpSocket *socket;
};
}
#endif // NETWORK
the Output is the following:
4
Data available: 4
recv -1
Which means
4 Bytes are sent from the broadcast
the socket detects that 4 bytes are available
the attempt to read the 4 bytes fails
the QString returned by getData() is empty
Unfortunately I could not find any possibility to get more information on the error
You definitely need to allocate memory for reading datagram. Now you try to write it to null pointer.
// char *data = 0;
std::cout << socket->pendingDatagramSize() << std::endl;
QVector<char> buffer(socket->pendingDatagramSize()); // create buffer
std::cout << QString("recv: %1")
.arg(socket->readDatagram(buffer.data(),
socket->pendingDatagramSize())).toStdString();

Why client dont connect to server? Qt

Programm works, but client can't connect to the server. (i run 2 examples of programm: client and server). I can't find where is my mistake.
I wrote the codes below.You will see what i want to do if you look at main function.
//main.cpp
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//выбор клиента или сервера
cout << "1.Client\n2.Server\n";
switch (_getch())
{
case 49:
{
cout<<"client init\n";
Client* cli = new Client("localhost",1922);
string line;
while(line!="exit") {
cout << "Message : ";
cin >> line;
cli->SendData(QString(line.c_str()));
}
break;
}
case 50:
{
cout<<"server init\n";
Server* srv = new Server(0, 1922);
break;
}
}
return a.exec();
}
//server.h
class Server : public QTcpServer {
Q_OBJECT public : Server(QObject *parent = 0, quint16 port = 1922);
virtual ~Server();
private
slots:
void acceptConnection();
void startRead();
void disconnected();
private:
QTcpServer *tcpServer;
QTcpSocket *client;
};
//server.cpp
Server::Server(QObject *parent, quint16 port) : QTcpServer(parent) {
//tcpServer = new QTcpServer(this);
connect(this, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
if (!this->listen(QHostAddress::Any, port))
std::cout << "unable to start server\n"
<< this->errorString().toUtf8().constData() << endl;
else
std::cout << "server started\n";
}
Server::~Server() {
//delete client;
close();
}
void Server::acceptConnection() {
std::cout << "new connection!\n";
client = nextPendingConnection();
connect(client, SIGNAL(readyRead()), this, SLOT(startRead()));
connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
qDebug() << "New client from:" << client->peerAddress().toString();
}
void Server::startRead() {
client = (QTcpSocket *)sender();
while (client->canReadLine()) {
QString line = QString::fromUtf8(client->readLine()).trimmed();
qDebug() << "Client :" << line;
client->write(QString("Server : I've taken your message (:\n").toUtf8());
}
}
void Server::disconnected() {
qDebug() << "Client disconnected:" << client->peerAddress().toString();
client->write(QString("Server : I wish you didn't leave ):\n").toUtf8());
}
//} <-- EDIT: THIS IS PROBABLY AN EXTRA
//***************************************************************
//client.h
class Client : public QObject {
Q_OBJECT public : Client(const QString &add, int port, QObject *obj = 0);
void SendData(QString data);
virtual ~Client();
int status();
QString err;
private
slots:
void ReadData();
void slotConnected();
void slotError(QAbstractSocket::SocketError);
private:
QTcpSocket *socket;
};
//client.cpp
Client::Client(const QString &add, int port, QObject *obj) : QObject(obj) {
//create socket
socket = new QTcpSocket(this);
//connect
socket ->connectToHost(add, port);
connect(socket, SIGNAL(readyRead()), SLOT(ReadData()));
connect(socket, SIGNAL(connected()), SLOT(slotConnected()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this,
SLOT(slotError(QAbstractSocket::SocketError)));
}
Client::~Client() {
socket->close();
delete socket;
}
void Client::SendData(QString data) {
if (!data.isEmpty()) {
QByteArray arrBlock;
QDataStream out(&arrBlock, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_1);
out << quint16(0) << QTime::currentTime() << data;
out.device()->seek(0);
out << quint16(arrBlock.size() - sizeof(quint16));
socket->write(arrBlock);
socket->flush();
}
}
void Client::ReadData() {
QDataStream in(socket);
in.setVersion(QDataStream::Qt_5_1);
while (socket->canReadLine()) {
QString line = QString::fromUtf8(socket->readLine()).trimmed();
qDebug() << line;
}
}
void Client::slotConnected() {
socket->write(QString("Client : Server connection has been made (: \n")
.toUtf8());
}
void Client::slotError(QAbstractSocket::SocketError err) {
QString strError =
"Error: " + (err == QAbstractSocket::HostNotFoundError
? "The host was not found."
: err == QAbstractSocket::RemoteHostClosedError
? "The remote host is closed."
: err == QAbstractSocket::ConnectionRefusedError
? "The connection was refused."
: QString(socket->errorString()));
std::cout << strError.toUtf8().constData() << endl;
}
int Client::status() { return socket->state(); }
help me pls!
It's probably becouse of the while loop in main.cpp, it blocks client's event loop, and will return to event loop right after 'exit' will be typed. I mean this lines:
while (line != "exit") {
cout << "Message : ";
cin >> line;
cli.SendData(QString(line.c_str()));
}
How this can be avoided: main.cpp MUST reach return a.exec(); line to start event loop (i am excluding some ugly processEvent solutions right away ).
To send commands to cmd and NOT block event loop i used class i saw somewhere here on stackoverflow:
example of main.cpp:
QCoreApplication a(argc, argv);
qDebug()<<"Press 'q' to quit";
QTcpServer server;
qDebug()<<"Server is started -"<<server.isListening();
// Console reader to filter console input
ConsoleReader reader;
QObject::connect(&reader,SIGNAL(shutdown()),&a,SLOT(quit()));
return a.exec();
aaand behold, ConsoleReader class, header:
#ifndef CONSOLEREADER_H
#define CONSOLEREADER_H
#pragma once
#include <QObject>
#include <QSocketNotifier>
class ConsoleReader : public QObject
{
Q_OBJECT
public:
explicit ConsoleReader(QObject *parent = 0);
~ConsoleReader();
signals:
void shutdown();
public slots:
void text();
private:
QSocketNotifier* notifier;
};
#endif // CONSOLEREADER_H
source:
#include "consolereader.h"
#include <QTextStream>
#include <QDebug>
#include <unistd.h> //Provides STDIN_FILENO
ConsoleReader::ConsoleReader(QObject *parent) :
QObject(parent)
{
notifier = new QSocketNotifier(STDIN_FILENO, QSocketNotifier::Read);
connect(notifier, SIGNAL(activated(int)), this, SLOT(text()));
}
void ConsoleReader::text()
{
QTextStream qin(stdin);
QString line = qin.readLine();
if (line==QString("q")){
qDebug()<<"Shutting down the server..";
emit shutdown();
}
else qDebug()<<"Unknown command: "<<line;
}
ConsoleReader::~ConsoleReader(){
delete notifier;
}
In your main function, you create both the client and the server on the stack, which will then be deleted when they go out of scope of the switch statement.
You need to dynamically allocate the objects on the heap: -
Server* pServer = new Server(0, 1922);
Client* pClient = new Client("localhost" 1922);
Although the client will remain, due to the while loop after its creation, the server will be created, start listening, then be deleted, along with the QTcpSocket, as it is has the server as its parent.
As I mentioned in my comment there is no need to create a separate QTCpServer as the server isa QTcpServer. So where you have:
Server::Server(QObject *parent, quint16 port) : QTcpServer(parent) {
tcpServer = new QTcpServer(this);
connect(this, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
if (!tcpServer->listen(QHostAddress::Any, port))
std::cout << "unable to start server\n"
<< tcpServer->errorString().toUtf8().constData() << endl;
else
std::cout << "server started\n";
}
Change to:
Server::Server(QObject *parent, quint16 port) : QTcpServer(parent) {
connect(this, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
if (!this->listen(QHostAddress::Any, port))
std::cout << "unable to start server\n"
<< this->errorString().toUtf8().constData() << endl;
else
std::cout << "server started\n";
}
I think the problem could be that its trying to do stuff with a different object rather than the 'this' object.