wysota, i gave all the codes in the #1 post of this thread.
But it's ok. i'll do it again with the WHOLE files "not just a snippets" :
myserver.h
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QTcpServer>
#include <QTcpSocket>
#include <QAbstractSocket>
#include "mainwindow.h"
{
Q_OBJECT
public:
explicit MyServer
(QObject *parent
= 0);
void startServer(int);
protected:
void incomingConnection(int handle);
signals:
void _connection(int handle);
public slots:
private:
MainWindow *main;
};
#endif // MYSERVER_H
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QTcpServer>
#include <QTcpSocket>
#include <QAbstractSocket>
#include "mainwindow.h"
class MyServer : public QTcpServer
{
Q_OBJECT
public:
explicit MyServer(QObject *parent = 0);
void startServer(int);
protected:
void incomingConnection(int handle);
signals:
void _connection(int handle);
public slots:
private:
MainWindow *main;
QTcpSocket *socket;
};
#endif // MYSERVER_H
To copy to clipboard, switch view to plain text mode
myserver.cpp
#include "myserver.h"
#include "myclient.h"
#include <QtGui>
MyServer
::MyServer(QObject *parent
) :{
main = new MainWindow;
}
void MyServer::startServer(int port)
{
{
QMessageBox::information(0,
"started",
"Server Started!!");
}
else
{
QMessageBox::warning(0,
"Server not started",
"Can't listen on the port "+QString::number(port
))+".<br>" "please change the port.<br>This happened because the port is busy.";
}
}
void MyServer::incomingConnection(int handle)
{
MyClient *client = new MyClient(this);
client->setsocket(handle);
emit _connection(handle);
}
#include "myserver.h"
#include "myclient.h"
#include <QtGui>
MyServer::MyServer(QObject *parent) :
QTcpServer(parent)
{
main = new MainWindow;
socket = new QTcpSocket;
}
void MyServer::startServer(int port)
{
if(listen(QHostAddress::Any,port))
{
QMessageBox::information(0,"started", "Server Started!!");
}
else
{
QMessageBox::warning(0,"Server not started", "Can't listen on the port "+QString::number(port))+".<br>"
"please change the port.<br>This happened because the port is busy.";
}
}
void MyServer::incomingConnection(int handle)
{
MyClient *client = new MyClient(this);
client->setsocket(handle);
emit _connection(handle);
}
To copy to clipboard, switch view to plain text mode
myclient.h
#ifndef MYCLIENT_H
#define MYCLIENT_H
#include <QObject>
#include <QTcpSocket>
#include <QDebug>
#include <QThreadPool>
#include <QtGui>
#include "mytask.h"
#include "myserver.h"
#include "mainwindow.h"
{
Q_OBJECT
public:
explicit MyClient
(QObject *parent
= 0);
void setsocket(int Descriptor);
void clientConnected();
signals:
void isConnected();
public slots:
void connected();
void disconncted();
void readyRead();
void TaskResult(int number);
void setNum();
private:
MyClient *client;
MainWindow *main;
MyServer *server;
};
#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"
#include "mainwindow.h"
class MyClient : public QObject
{
Q_OBJECT
public:
explicit MyClient(QObject *parent = 0);
void setsocket(int Descriptor);
void clientConnected();
QTcpSocket *socket;
signals:
void isConnected();
public slots:
void connected();
void disconncted();
void readyRead();
void TaskResult(int number);
void setNum();
private:
MyClient *client;
MainWindow *main;
MyServer *server;
};
#endif // MYCLIENT_H
To copy to clipboard, switch view to plain text mode
myclient.cpp
#include "myclient.h"
MyClient
::MyClient(QObject *parent
) :{
QThreadPool::globalInstance()->setMaxThreadCount(5);
}
void MyClient::setsocket(int Descriptor)
{
main = new MainWindow;
server = new MyServer;
clientConnected();
int cn = connect(server,SIGNAL(_connection(int)), this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconncted()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
socket->setSocketDescriptor(Descriptor);
}
void MyClient::connected()
{
}
void MyClient::disconncted()
{
qDebug() << "client disconnected!";
// test -= 1;
}
void MyClient::clientConnected()
{
if(handshake == "newClient")
{
emit isConnected();
}
}
void MyClient::readyRead()
{
// 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);
}
void MyClient::setNum()
{
emit isConnected();
}
#include "myclient.h"
MyClient::MyClient(QObject *parent) :
QObject(parent)
{
QThreadPool::globalInstance()->setMaxThreadCount(5);
}
void MyClient::setsocket(int Descriptor)
{
socket = new QTcpSocket;
main = new MainWindow;
server = new MyServer;
clientConnected();
int cn = connect(server,SIGNAL(_connection(int)), this, SLOT(connected()));
QMessageBox::information(0, "cinnect()", QString::number(cn));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconncted()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
socket->setSocketDescriptor(Descriptor);
}
void MyClient::connected()
{
QMessageBox::information(0, "connected", "bingo!");
}
void MyClient::disconncted()
{
qDebug() << "client disconnected!";
// test -= 1;
}
void MyClient::clientConnected()
{
QByteArray handshake = socket->readAll();
if(handshake == "newClient")
{
emit isConnected();
}
}
void MyClient::readyRead()
{
// 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);
}
void MyClient::setNum()
{
emit isConnected();
}
To copy to clipboard, switch view to plain text mode
Bookmarks