QSerial without QThreads - qt

I create a "server" lib with a Qt GUI.
I don't and can't use QThreads because this is supposed to be as independent from Qt as possible, and because I have other threads already working like a Ethernet part.
The thread ExternalRs232Thread() is lauched by the public function RunExternalRs232()
RunExternalRs232() opens the serial port, returns -1 if can't open serial, and if is ok runs the function ExternalRs232Thread() in a detached thread.
Initially, I have tried to run this with Serialib, but this never works properly with the Qt project. So I decided to give QSerialPort a try like this:
Server.h
#ifndef SERVER_H
#define SERVER_H
#include <iostream>
#include <winsock2.h>
#include <windows.h>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <time.h>
#include <chrono>
#include <mutex>
#include <QObject>
#include "libs/json.hpp"
#include <QtSerialPort>
#include "hdlccsvparser.h"
using namespace std;
using json = nlohmann::json;
using std::chrono::milliseconds;
using std::chrono::duration_cast;
using std::chrono::seconds;
using std::chrono::system_clock;
struct Contact {
int port; //udp port of te contact
time_t time; //last communication date ( is still active ? )
};
class Server : public QObject
{
Q_OBJECT
public:
Server();
virtual ~Server();
[...]
//return 0 if sucess , 1 port is not usabe, 2 port already in use
int RunExternalRs232(string PortCom, unsigned int baudrate);
int StopExternalRs232(string PortCom);
[...]
//clean exit
void ExitServer();
signals:
[...]
private:
[...]
// External ThreadS and exiting loop of threadS in set (if key d'ont exist anymore, exit loop)
mutex mtxExternalRs232;
unordered_set<string> ExternalRs232PortActiveList;
void ExternalRs232Thread(QSerialPort* Rs232Connection, string port);
[...]
};
#endif // SERVER_H
Server.cpp
#include "Server/Server.h"
#include <winsock2.h>
/* Public Part*/
[...]
int Server::RunExternalRs232(string PortCom, unsigned int baudrate){
//PortCom = (char *)"COM1";
//baudrate = 9600;
QSerialPort Rs232Connection;
// Connection to serial port
// COMxx
Rs232Connection.setPortName(QString::fromStdString(PortCom));
Rs232Connection.setBaudRate(baudrate);
Rs232Connection.setDataBits(QSerialPort::Data8);
Rs232Connection.setParity(QSerialPort::NoParity);
Rs232Connection.setStopBits(QSerialPort::OneStop);
Rs232Connection.setFlowControl(QSerialPort::NoFlowControl);
int i = Rs232Connection.open(QIODevice::ReadWrite);
// If connection fails, errorOpening != 1
if(i != 1){
return i;
}
mtxExternalRs232.lock();
ExternalRs232PortActiveList.insert(PortCom);
mtxExternalRs232.unlock();
cout << "External Rs232 starting on " << PortCom << endl;
stringstream sstmp;
sstmp << PortCom;
emit LogMessage(QString::fromStdString("External Rs232 starting on " + sstmp.str()),0);
std::thread thServer(&Server::ExternalRs232Thread,this,&Rs232Connection,PortCom);
thServer.detach();
return 0;
}
int Server::StopExternalRs232(string PortCom){
PortCom = "COM1";
mtxExternalRs232.lock();
/*
if(ExternalRs232PortActiveList.count(PortCom) != 1){ //???
//port not in use
cerr << "close Failed Rs232 " << PortCom << endl;
mtxExternalRs232.unlock();
return 1;
}*/
cout << "close External Rs232 " << PortCom << endl;
ExternalRs232PortActiveList.erase(PortCom);
mtxExternalRs232.unlock();
return 0;
mtxExternalRs232.lock();
cout << ExternalRs232PortActiveList.count(PortCom) << " here " << endl;
mtxExternalRs232.unlock();
}
[...]
/* Private Part */
/* Thread part */
[...]
void Server::ExternalRs232Thread(QSerialPort * Rs232Connection, string port){
//char/binary read on rs232
char tmp;
//if rs232 has data
bool res = false;
stringstream message;
Rs232Connection->write("H");
mtxExternalRs232.lock();
int iscount = ExternalRs232PortActiveList.count(port);
mtxExternalRs232.unlock();
while(iscount == 1){
res = false;
if(!Rs232Connection->waitForReadyRead(300)){
//no data skipp
} else {
QByteArray datas = Rs232Connection->readAll();
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
string tmp = codec->toUnicode(datas).toStdString();
cout << tmp << endl;
}
mtxExternalRs232.lock();
iscount = ExternalRs232PortActiveList.count(port);
mtxExternalRs232.unlock();
}
Rs232Connection->flush();
Rs232Connection->close();
}
[...]
/* functions part */
[...]
Server::Server(){
//init winsock
if(WSAStartup(MAKEWORD(2,2),&wsa) != 0){
cerr << "Could not init winsock2 : " << WSAGetLastError();
}
//creating a socket
if((s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET){
cerr << "Could not create a socket : " << WSAGetLastError();
}
}
Server::~Server(){
//dtor
}
at the end, I get
QObject::startTimer: Timers can only be used with threads started with QThreads
So how is possible to run Serial Rs232 without using QThreads?

Related

udp binded/connected but not datagram received

I am new to Qt, c++, recently I am trying to use UDP to receive data on my raspberrypi1 from another raspberrypi2 (multicast). I am able to bind both of them but I can't receive the data (nopendingdatagram). I wonder what I did wrong here. (As you might notice, the code below was taken from examples found online). Thank you in advanced for helping me.
// myudp.h
#ifndef MYUDP_H
#define MYUDP_H
#include <QObject>
#include <QUdpSocket>
class MyUDP : public QObject
{
Q_OBJECT
public:
explicit MyUDP(QObject *parent = 0);
//void HelloUDP();
signals:
public slots:
void readyRead();
private:
QUdpSocket *socket;
};
#endif // MYUDP_H
// myudp.cpp
#include "myudp.h"
MyUDP::MyUDP(QObject *parent) :
QObject(parent)
{
// create a QUDP socket
socket = new QUdpSocket(this);
bool result = socket->bind(QHostAddress("224.224.0.2"), 10002);
if(result)
{
qDebug() << "Socket Connected";
}
else
{
qDebug() << "Socket Not Connected";
}
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
void MyUDP::readyRead()
{
// when data comes in
bool data_pending = socket->hasPendingDatagrams();
qDebug() << data_pending;
if(data_pending)
{
QByteArray buffer;
buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(buffer.data(), buffer.size(),
&sender, &senderPort);
qDebug() << "Message from: " << sender.toString();
qDebug() << "Message port: " << senderPort;
qDebug() << "Message: " << buffer;
}
else
{
qDebug() << "No data";
}
}
#include <QCoreApplication>
#include "myudp.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyUDP client;
client.readyRead();
return a.exec();
}
The result is as follows:
Socket Connected
false
No data

cannot resolve Qt's QMetaObject::invokeMethod: No such method error

I am trying to develop a generic function which determines whether two QObjects are equal. In order for this to be possible, the functions being compared must have an 'equals' method that compares various function values in each and returns true if they are all equal. Slso, this 'equal' method must be declared with Q_INVOKABLE.
However, when I attempt to call invokeMethod for the 'equals' method, it fails an an error "QMetaObject::invokeMethod: No such method F1::equals(QObject*)(QObject*)" is displayed.
Here is my test project and files:
Project file:
CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
HEADERS += \
f1.h \
assert1.h
assert1.h
#ifndef ASSERT1_H
#define ASSERT1_H
#include <QObject>
#include <QDebug>
class Assert1 : public QObject
{
Q_OBJECT
public:
explicit Assert1(QObject *parent = nullptr) {}
static bool isEqual(QString msg, QObject* o1, QObject* o2)
{
if(o1 != nullptr && o2 != nullptr)
{
if(o1->metaObject()->className() != o2->metaObject()->className())
{
qDebug() << msg << " not same class type!";
return false;
}
const QMetaObject* metaObject =o1->metaObject();
int ix = metaObject->indexOfMethod(QMetaObject::normalizedSignature("equals(QObject *)"));
qDebug() << QMetaObject::normalizedSignature("equals(QObject *)");
if(ix == -1)
{
qDebug() << msg << tr("indexOfMethod(\"equals\") returns %1").arg(ix);
return false;
}
else
{
bool rslt = false;
if(!QMetaObject::invokeMethod(o1, QMetaObject::normalizedSignature("equals(QObject *)"),
Qt::DirectConnection,
Q_RETURN_ARG(bool, rslt),
Q_ARG(QObject*, o2)))
qDebug() << msg << tr("invoke method 'equals' failed for %1").arg(o1->metaObject()->className());
if(!rslt)
qDebug() << msg << tr(" objects not equal");
return false;
}
}
qDebug() << msg << "not equal";
}
signals:
public slots:
};
#endif // ASSERT1_H
f1.h
#ifndef F1_H
#define F1_H
#include <QObject>
#include <QDebug>
class F1 : public QObject
{
Q_OBJECT
public:
explicit F1(int p1, QString p2, QObject *parent = nullptr) : QObject(parent)
{
this->p1 = p1;
this->p2 = p2;
}
void setP1(int p) {this->p1 = p;}
void setP2(QString p) {this->p2 = p;}
Q_INVOKABLE bool equals(QObject* other)
{
if(qobject_cast<F1*>(other) != nullptr)
{
if(this->p1 != ((F1*)other)->p1)
return false;
if(this->p2 != ((F1*)other)->p2)
return false;
}
return true;
}
Q_INVOKABLE QString toString()
{
qDebug() << "p1 '" << p1 << " p2 = '" << p2 << "'";
}
signals:
public slots:
private:
int p1;
QString p2;
};
#endif // F1_H
main.cpp
#include <QCoreApplication>
#include "f1.h"
#include "assert1.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
F1* tf1 = new F1(1, "1");
F1* tf2 = new F1(1, "a");
F1* tf3 = new F1(1, "a");
F1* tf4 = new F1(4, "abc");
qDebug() << "tf1->equals(tf4) returns: " << (tf1->equals(tf4)?"true":"false");
qDebug() << "tf2->equals(tf3) returns: " << (tf2->equals(tf3)?"true":"false");
Assert1::isEqual("should be equal", (QObject*)tf2, (QObject*)tf3);
//return a.exec();
}
running the test produces this output:
Debugging starts
tf1->equals(tf4) returns: false
tf2->equals(tf3) returns: false
"equals(QObject*)"
QMetaObject::invokeMethod: No such method F1::equals(QObject*)(QObject*)
"should be equal" "invoke method 'equals' failed for F1"
How do I get invokeMethod to work?
The error in your case is that invokeMethod only waits for the name of the Q_SLOT or Q_INVOKABLE, does not need or want the signature, but you are passing it QMetaObject::normalizedSignature("equals(QObject*)") which returns "equals(QObject*)", so the solution is just to pass equals:
if(!QMetaObject::invokeMethod(o1, "equals",
Qt::DirectConnection,
Q_RETURN_ARG(bool, rslt),
Q_ARG(QObject*, o2)))

how to set SO_REUSEADDR on the socket used by QTcpServer?

I have been using a QTcpServer subclass as a http server, but now I need to reuse the server port.
I have tried to set ShareAddress | ReuseAddressHint in a QTcpSocket, it seems worked, because the same port can be bound twice. But I did not find a way to get a QTcpSocket object from an existing QTcpServer object.
I also used the socketDescriptor() to get the native socket, because I want to use the linux C way to setsockopt, but I don't know how to use linux C code with Qt code together to set socket options.(I followed the Qt style until now.)
I am on ubuntu and Qt5.4. And I am stuck...
Any help would be appreciated. Thanks in advance.
Because SO_REUSEPORT needs to be set before bind/listen is called you need to create descriptor first, set all needed flags, bind, listen and forward your descriptor to QTcpServer for future usage with it.
Here is an example which will listen on port 9999 on any interface
mytcpserver.h:
#ifndef MYTCPSERVER_H
#define MYTCPSERVER_H
#include <QObject>
#include <QTcpSocket>
#include <QTcpServer>
class MyTcpServer : public QObject
{
Q_OBJECT
public:
explicit MyTcpServer(QObject *parent = 0);
public slots:
void newConnection();
private:
QTcpServer *server;
};
#endif // MYTCPSERVER_H
mytcpserver.cpp:
#include "mytcpserver.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
MyTcpServer::MyTcpServer(QObject *parent):QObject(parent)
{
this->server = new QTcpServer(this);
connect(server, &QTcpServer::newConnection, this, &MyTcpServer::newConnection);
// open server and listen on given port
int sockfd = 0;
struct sockaddr_in serv_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
qDebug() << "ERROR: IoT Omega Daemon can't open socket";
exit(EXIT_FAILURE);
}
int flag = 1;
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int)) == -1)
{
qDebug() << "ERROR: Can't set SO_REUSEADDR";
exit(EXIT_FAILURE);
}
//set Address,IFace, Port...
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(9999);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(sockaddr_in)) < 0)
{
qDebug() << "ERROR: can't bind socket";
exit(EXIT_FAILURE);
}
if(listen(sockfd,SOMAXCONN) < 0)
{
qDebug() << "ERROR: can't listen on port";
exit(EXIT_FAILURE);
}
//forward our descriptor with SO_REUSEPORT to QTcpServer member
server->setSocketDescriptor(sockfd);
}
void MyTcpServer::newConnection()
{
qDebug() << "NEW CONNECTION " << __LINE__;
QTcpSocket * socket = server->nextPendingConnection();
socket->write("Hello client\r\n");
socket->flush();
socket->waitForBytesWritten(3000);
socket->close();
}
Maybe you would like to optimize your socket usage even more? For example setting SO_LINGER timeout of zero to avoid large numbers of connections sitting in the TIME_WAIT state? Maybe you don't ned waiting for ACKs(TCP_NODELAY)?
Then your newConnection function can look like this:
void MyTcpServer::newConnection()
{
qDebug() << "NEW CONNECTION " << __LINE__;
QTcpSocket * socket = server->nextPendingConnection();
int flag = 1;
struct linger l = {1,0};
if(setsockopt(socket->socketDescriptor(), SOL_SOCKET, SO_REUSEADDR, (const char *)&flag, sizeof(int)) < 0)
{
qDebug() << "ERROR: Can't set SO_REUSEADDR";
}
if(setsockopt(socket->socketDescriptor(), IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) < 0)
{
qDebug() << "ERROR: can't fork set TCP_NODELAY";
}
socket->write("Hello client\r\n");
socket->flush();
socket->waitForBytesWritten(3000);
if(setsockopt(socket->socketDescriptor(), SOL_SOCKET, SO_LINGER, (const char *)&l,sizeof(l)) < 0)
{
qDebug() << "ERROR: Can't set SO_LINGER";
}
socket->close();
}
This will just do the job and free any used socket resource so they can be reused just right after that. Usefull for heavyload microservers, etc..

Not able to get poco 1.6.1 or 1.7.6 to bind to ipv6 with Net::DatagramSocket

Using the following code I can bind to an ipv4 address but not to a scope global ipv6 address that is also bound to this same machine. I am compiling the code like this:
g++ -lPocoFoundation -lPocoXML -lPocoUtil -lPocoNet -lcrypto -lssl -I/usr/include/Poco -o pocoudpipv6 pocoudpipv6.cpp
When I execute ./pocoudpipv6 10.X.X.X, it holds open the socket and cycles on "Waiting..." until I hit ctrl-c, which is expected. ss reports the socket:
# ss -nelup |grep 20000
UNCONN 0 0 10.X.X.X:20000 *:* users:(("pocoudpipv6",pid=2444,fd=3)) uid:1000 ino:14526705 sk:2a <->
But when I execute with ./pocoudpipv6 2001:X:X:X::X:X, this occurs:
We're resetting the ipaddress from ::1 to 2001:X:X:X::X:X
Address family is ipv6
Failure launching. Error was Net Exception: Address family not supported
This problem occurs with 1.7.6 on Slackware64 14.2 as well as with 1.6.1 on Debian 8 jessie amd64. As far as I've read, ipv6 being enabled in Poco is supposed to be the default. Is there something else I need to do in order to get this test-case to work with ipv6?
And I do have at least one daemon that is binding to an ipv6 socket on this machine:
udp UNCONN 0 0 2001:X:X:X::X:X:123 :::* users:(("ntpd",pid=2133,fd=22)) ino:5247 sk:19 v6only:1 <->
Thanks in advance!
Code as follows:
#include <iostream>
#include <Net/DatagramSocket.h>
#include <Net/SocketAddress.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
struct sigaction sigact = { 0 };
struct io_handling {
uint8_t exit_value;
};
struct io_handling io_handler = { 0 };
static void sigint_signal_handler(int sig)
{
if(sig == SIGINT) {
std::cout << std::endl << "Commencing shutdown in 5... 4... 3... 2... 1..." << std::endl;
io_handler.exit_value = 1;
}
}
static void cleanup(void)
{
sigemptyset(&sigact.sa_mask);
}
int main(int argc, char **argv)
{
Poco::Net::DatagramSocket *pSocket = new Poco::Net::DatagramSocket();
Poco::UInt16 port = 20000;
Poco::Net::IPAddress *ipAddress = new Poco::Net::IPAddress("::1");
if(argc == 2) {
delete ipAddress;
ipAddress = new Poco::Net::IPAddress(argv[1]);
std::cout << std::endl << "We're resetting the ipaddress from ::1 to " << argv[1];
}
std::cout << std::endl << "Address family is ";
if(ipAddress->family() == static_cast<Poco::Net::IPAddress::Family>(Poco::Net::Impl::IPAddressImpl::IPv6)) {
std::cout << "ipv6 ";
} else if(ipAddress->family() == static_cast<Poco::Net::IPAddress::Family>(Poco::Net::Impl::IPAddressImpl::IPv4)) {
std::cout << "ipv4 ";
} else {
std::cout << "something else, something very wrong.";
}
try {
sigact.sa_handler = sigint_signal_handler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, (struct sigaction *)NULL);
pSocket->bind(Poco::Net::SocketAddress(*ipAddress, port));
while(!io_handler.exit_value) {
sleep(1);
std::cout << std::endl << "Waiting...";
}
} catch(Poco::Exception& ex) {
std::cout << std::endl << "Failure launching. Error was " << ex.displayText() << std::endl;
}
delete pSocket;
delete ipAddress;
return 0;
}
Self-resolved. The lib I was working with was only calling up Poco::Net::DatagramSocket's default ctor, which then gives an ipv4-only socket. Calls to ::bind() will not reset that based upon the input ip address.
The solution is to not new pSocket until handling ipAddress in the example above, and then passing that and the port casted as Poco::Net::SocketAddress to the ctor, and it will create the proper socket type and automatically bind it.

Qt Threading code different behavior in MAC,Linux and Windows

I have written code for a server which accepts connections from different clients. Each client is serviced in different threads. Each thread accesses a database to get data and then updates this data to all the clients connected to server.
1) For the first time when UI asks data from server, it responds properly, but after that server does not read the socket i.e. Server's readyread() doesn't get invoked. Funnily enough, this works fine in mac and linux, this issue is seen only on windows
2) I was able to verify that when the DB module emits a signal which is caught by the threads, the hang occurs, Because everything worked fine when I removed the emit.
Here, I am attaching all the needed .h and .cpp codes
Defn.h
#ifndef DEFN_H
#define DEFN_H
struct PresetData{
QString ID;
QString name;
QString value;
QString source;
};
#endif // DEFN_H
main.cpp
#include <QCoreApplication>
#include "myserver.h"
#include "mydb.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyDB db;
MyServer server(&db);
server.startServer();
return a.exec();
}
mydb.h
#ifndef MYDB_H
#define MYDB_H
#include <QObject>
#include <QtSql>
#include "Defn.h"
class MyDB : public QObject
{
Q_OBJECT
public:
explicit MyDB(QObject *parent = 0);
signals:
void dataAvailable(QString ID, QString name, QString value, QString source);
public slots:
void onUpdateData(QString ID, QString name, QString value, QString source);
void onGetData(QString ID, QString name, QString value, QString source);
private:
QSqlDatabase m_db;
};
#endif // MYDB_H
mydb.cpp
#include "mydb.h"
MyDB::MyDB(QObject *parent) :
QObject(parent)
{
m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setConnectOptions();
m_db.setDatabaseName("D:/MySimulator/New Folder/TCPServer1/DB.db");
if (m_db.open()){
qDebug() << "DB opened succesfully" ;
}else{
qDebug() << "DB Opening failed" ;
}
QStringList tables = m_db.tables();
if (tables.contains("Presets", Qt::CaseInsensitive)){
qDebug() << "DB Contains Data" ;
return;
}
}
void MyDB::onGetData(QString ID, QString name, QString value, QString source)
{
qDebug() << "onGetData" ;
QString queryString = "SELECT Value from 'Presets' where ID = \'" + ID + "\'";
QSqlQuery q;
bool result = q.exec(queryString);
if (result){
if (q.next()){
value = q.value(q.record().indexOf("Value")).toString();
qDebug() << " Retrieved Value = " << value ;
emit dataAvailable(ID, name, value, source);
}else{
qDebug("Empty Result");
}
}else{
qDebug("NO Result");
}
}
void MyDB::onUpdateData(QString ID, QString name, QString value, QString source)
{
qDebug() << "onUpdateData" ;
QString queryString = "UPDATE 'Presets' SET Value = \'" + value + "'\ WHERE ID = \'" + ID + "\'";
QSqlQuery q;
QSqlDatabase::database().transaction();
bool result = q.exec(queryString);
if (result){
QSqlDatabase::database().commit();
onGetData(ID, name, "", "000");
}else{
qDebug("NO Result");
}
}
mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QDebug>
#include "Defn.h"
#include "mydb.h"
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(int ID, MyDB* db, QObject * parent = 0);
void run();
void parseInput(QString string);
signals:
void error(QTcpSocket::SocketError socketError);
void updateData(QString ID, QString name, QString value, QString source);
void getData(QString ID, QString name, QString value, QString source);
public slots:
void readyRead();
void disconnected();
void onDataAvailable(QString ID, QString name, QString value, QString source);
private:
QTcpSocket* socket;
int socketDescriptor;
MyDB* db;
};
#endif // MYTHREAD_H
mythread.cpp
#include "mythread.h"
#include "qtcpserver.h"
#include "qabstractsocket.h"
MyThread::MyThread(int ID, MyDB* db, QObject * parent ):
QThread(parent)
{
this->socketDescriptor = ID ;
this->db = db;
}
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);
connect(this, SIGNAL(getData(QString, QString , QString , QString )), this->db, SLOT(onGetData(QString , QString , QString , QString )));
connect(this, SIGNAL(updateData(QString , QString , QString , QString )), this->db, SLOT(onUpdateData(QString , QString , QString , QString )));
connect(this->db, SIGNAL(dataAvailable(QString , QString , QString , QString )), this, SLOT(onDataAvailable(QString , QString , QString , QString )));
qDebug() << socketDescriptor << "Client Connected" ;
exec();
}
void MyThread::readyRead()
{
QByteArray data = socket->readAll();
qDebug() << socketDescriptor << "Data in: " << data;
parseInput(data);
}
void MyThread::disconnected()
{
qDebug() << socketDescriptor << "Disconnected" ;
socket->deleteLater();
exit(0);
}
void MyThread::parseInput(QString dataFromTCP)
{
qDebug() << socketDescriptor << ":" <<"parseInput " << dataFromTCP;
if (dataFromTCP.isEmpty())
return;
QStringList list1 = dataFromTCP.split("\n", QString::SkipEmptyParts);
qDebug() << socketDescriptor << ":" << "list1 BEGIN";
for (int i = 0 ; i < list1.count(); i++)
{
qDebug() << i<< ":" << list1.at(i);
}
qDebug() << socketDescriptor << ":" << "list1 END";
if (list1.count() < 1){
return;
}
QString strMessage = "";
for (int i = 0 ; i < list1.count() ; i++)
{
strMessage = list1[i];
QStringList list2 = strMessage.split(" ", QString::SkipEmptyParts);
qDebug() << socketDescriptor << ":" << "list2 BEGIN";
for (int i = 0 ; i < list2.count(); i++)
{
qDebug() << i<< ":" << list2.at(i);
}
qDebug() << socketDescriptor << ":" << "list2 END";
if (list2.count() < 1){
break;
}
QString ID = list2[1];
QString source = QString::number(socketDescriptor) ;
if (list2[0] == "GET"){
emit getData(ID, "", "", source);
}
else if (list2[0] == "UPD"){
QString value = list2[2];
emit updateData(ID, "", value, source);
}
}
}
void MyThread::onDataAvailable(QString ID, QString name, QString value, QString source)
{
if( (QString::number(socketDescriptor) == source) || ("000" == source ) ) {
qDebug() << socketDescriptor << " : On Data Available " << ID << name << value ;
QString data = "DATA " + ID + " " + value + " " + "\n" ;
QByteArray ba;
ba.append(data);
socket->write(ba);
}
}
myserver.h
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QDebug>
#include <QObject>
#include <QTCPServer>
#include <QTCPSocket>
#include "mythread.h"
#include "mydb.h"
class MyServer: public QTcpServer
{
Q_OBJECT
public:
explicit MyServer(MyDB* pdb, QObject* parent = 0);
void startServer();
signals:
public slots:
protected:
void incomingConnection(qintptr socketDescriptor);
private:
MyDB* pdb ;
};
#endif // MYSERVER_H
myserver.cpp
#include "myserver.h"
MyServer::MyServer(MyDB* pdb, QObject* parent ):
QTcpServer(parent)
{
this->pdb = pdb;
}
void MyServer::startServer()
{
if (!this->listen(QHostAddress::Any, 1234)){
qDebug() << "Could not Start Server " << this->errorString();
}
else{
qDebug() << " Server Running... ";
}
}
void MyServer::incomingConnection(qintptr socketDescriptor)
{
qDebug() << socketDescriptor << " Connecting... ";
MyThread *thread = new MyThread(socketDescriptor, pdb, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
Here the signal about which I mentioned above is dataAvailable from "mydb.cpp". If I comment out that line then server responds to client messages. But if that signal is emitted then after the initial response, the server seems to hang and no longer reacts to incoming messages from the client.
The same code is working perfectly fine in mac and linux. But it is having this problem in Windows only.
Could someone let me know what is it that I am doing wrong that it is failing only in Windows?
Thanks in advance for helping me out.
EDIT:
The objective of this code is that whenever a thread causes an update call to the database, EVERY thread including the one that called the update gets informed about the change. So it is EXPECTED that other thread that runs at that time also receives a signal.
This is what is expected of the server:
Be able to allow TCP connections from multiple clients simultaneously.
If any client requests info, it gets the required data over the TCP connection.
If any client updates info, all clients including the updating client, gets a notifications over the TCP connection.
Well, for starters, your code is completely not thread-safe. You create a single instance of MyDB in your main() function, then call it from threads without protecting its data member. Also, signals get emitted, updating data without any protection. What if two threads happen to be running at the same time?
Secondly, and this is more important: whenever you emit dataAvailable() you call functions in other thread objects in your own thread. This is the code path when data arrives:
MyThread::parseInput() emits
MyThread::getData(), which is connected to
MyDB::onGetData(), which emits
MyDb::dataAvailable, which is connected to (drumroll....)
MyThread::onDataAvailable, which eventually calls
socket->write()
So if data arrives in thread #1, you're going to send data from MyThread object #2, #3, #4, etc from .... thread #1. Depending on the OS, this is bad news. I don't know enough about Windows threads but I do know this code is terminally broken.
If all you want to do is update a database and relay the data you can dispense with the threads and use a sequential program that handles sockets using the regular Qt signals and slots just fine.

Resources