#ifndef MYCLIENT_H
#define MYCLIENT_H
#include <QObject>
#include <QTcpSocket>
#include <QDebug>
#include <QThreadPool>
#include <QtGui>
#include "mytask.h"
#include "myserver.h"
{
Q_OBJECT
public:
explicit MyClient
(QObject *parent
= 0);
void setsocket(int Descriptor);
int test = 0;
QWeakPointer<QMessageBox> messageBox;
signals:
bool isConnected();
public slots:
void connected();
void disconncted();
void readyRead();
void TaskResult(int number);
void setNum();
private:
MyClient *client;
};
#endif // MYCLIENT_H
#ifndef MYCLIENT_H
#define MYCLIENT_H
#include <QObject>
#include <QTcpSocket>
#include <QDebug>
#include <QThreadPool>
#include <QtGui>
#include "mytask.h"
#include "myserver.h"
class MyClient : public QObject
{
Q_OBJECT
public:
explicit MyClient(QObject *parent = 0);
void setsocket(int Descriptor);
int test = 0;
QWeakPointer<QMessageBox> messageBox;
QTcpSocket *socket;
signals:
bool isConnected();
public slots:
void connected();
void disconncted();
void readyRead();
void TaskResult(int number);
void setNum();
private:
MyClient *client;
};
#endif // MYCLIENT_H
To copy to clipboard, switch view to plain text mode
#include "myclient.h"
MyClient
::MyClient(QObject *parent
) :{
QThreadPool::globalInstance()->setMaxThreadCount(5);
}
void MyClient::setsocket(int Descriptor)
{
connect(socket,SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconncted()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
socket->setSocketDescriptor(Descriptor);
//QMessageBox::about(this,"new client","a new client is connected "+QString::number(socket->socketDescriptor()));
//connect(socket, SIGNAL(connected()), this, SLOT(setNum()));
//QMessageBox::information(0,"test", QString::number(test));
}
void MyClient::isConnected()
{
if(socket->connected())
{
return true;
}
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
}
void MyClient::connected()
{
qDebug() << "client connected event";
messageBox.information(parent, "connected", "new");
}
void MyClient::disconncted()
{
qDebug() << "client disconnected!";
// test -= 1;
}
void MyClient::readyRead()
{
qDebug() << socket->readAll();
// time consumer
MyTask *mytask = new MyTask();
mytask->setAutoDelete(true);
connect(mytask, SIGNAL(Result(int)), this, SLOT(TaskResult(int)), Qt::QueuedConnection);
QThreadPool::globalInstance()->start(mytask);
}
void MyClient::TaskResult(int number)
{
Buffer.append("Task Result = ");
Buffer.
append(QString::number(number
));
socket->write(Buffer);
QMessageBox::information(this,
"Bytes written",
"Bytes written successfully to the client");
}
void MyClient::setNum()
{
this->test = 12;
}
#include "myclient.h"
MyClient::MyClient(QObject *parent) :
QObject(parent)
{
QThreadPool::globalInstance()->setMaxThreadCount(5);
}
void MyClient::setsocket(int Descriptor)
{
socket = new QTcpSocket;
connect(socket,SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconncted()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
socket->setSocketDescriptor(Descriptor);
//QMessageBox::about(this,"new client","a new client is connected "+QString::number(socket->socketDescriptor()));
//connect(socket, SIGNAL(connected()), this, SLOT(setNum()));
//QMessageBox::information(0,"test", QString::number(test));
}
void MyClient::isConnected()
{
if(socket->connected())
{
return true;
}
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
}
void MyClient::connected()
{
messageBox = new QMessageBox(QMessageBox::Information, "Client", "Recu", QMessageBox::Ok, this);
qDebug() << "client connected event";
messageBox.information(parent, "connected", "new");
}
void MyClient::disconncted()
{
qDebug() << "client disconnected!";
// test -= 1;
}
void MyClient::readyRead()
{
qDebug() << socket->readAll();
QMessageBox::information(0, "connected", "new");
// time consumer
MyTask *mytask = new MyTask();
mytask->setAutoDelete(true);
connect(mytask, SIGNAL(Result(int)), this, SLOT(TaskResult(int)), Qt::QueuedConnection);
QThreadPool::globalInstance()->start(mytask);
}
void MyClient::TaskResult(int number)
{
QByteArray Buffer;
Buffer.append("Task Result = ");
Buffer.append(QString::number(number));
socket->write(Buffer);
QMessageBox::information(this, "Bytes written", "Bytes written successfully to the client");
}
void MyClient::setNum()
{
this->test = 12;
}
To copy to clipboard, switch view to plain text mode
Bookmarks