myserver ifndef MYSERVER_H define MYSERVER_H include QTcpServer includ

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//myserver.h
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QTcpServer>
#include <fstream>
typedef std::vector<double> vect;
class MyServer : public QTcpServer
{
Q_OBJECT
public:
explicit MyServer(QObject *parent = 0);
void startServer();
void Generatemtx(std::vector<vect>* newmtx, int size);
signals:
public slots:
void JobIsDone();
protected:
void incomingConnection(qintptr socketDescriptor);
int *currentNum;
int size = 10;
std::vector<vect> fstmtx, scndmtx;
std::vector<QByteArray> finalmtx;
};
#endif // MYSERVER_H
//mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QTcpSocket>
#include <QDebug>
typedef std::vector<double> vect;
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(std::vector<vect>* fst,std::vector<vect>* scnd,std::vector<QByteArray>* final,int *_currentNum,int, qintptr ID, QObject *parent = 0);
void run();
int* currentNum;
int size;
int* globalcounter;
std::vector<vect>* fstmtx;
std::vector<vect>* scndmtx;
std::vector<QByteArray>* finalmtx;
signals:
void error(QTcpSocket::SocketError socketerror);
void JobIsDone();
public slots:
void readyRead();
void disconnected();
private:
QTcpSocket *socket;
qintptr socketDescriptor;
};
#endif // MYTHREAD_H
//main.cpp
#include <QCoreApplication>
#include "myserver.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Make a server and starts it
MyServer server;
server.startServer();
return a.exec();
}
//myserver.cpp
#include "myserver.h"
#include "mythread.h"
MyServer::MyServer(QObject *parent) :
QTcpServer(parent)
{
currentNum = new int;
*currentNum = 0;
Generatemtx(&fstmtx,size);
Generatemtx(&scndmtx,size);
for(int i=0; i<size; i++)
finalmtx.push_back(*(new QByteArray));
//Generatemtx(&finalmtx,size);
}
void MyServer::startServer()
{
int port = 1234;
if(!this->listen(QHostAddress::Any, port))
{
qDebug() << "Could not start server";
}
else
{
qDebug() << "Listening to port " << port << "...";
}
}
// This function is called by QTcpServer when a new connection is available.
void MyServer::incomingConnection(qintptr socketDescriptor)
{
// We have a new connection
qDebug() << socketDescriptor << " Connecting...";
// Every new connection will be run in a newly created thread
MyThread *thread = new MyThread(&fstmtx,&scndmtx,&finalmtx,currentNum, size, socketDescriptor, this);
// connect signal/slot
// once a thread is not needed, it will be beleted later
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
connect(thread,SIGNAL(JobIsDone()),this,SLOT(JobIsDone()));
thread->start();
}
void MyServer::Generatemtx(std::vector<vect>* newmtx, int size1)
{
double newnum = 0;
std::vector<double> newvec;
//newnum = rand()%100;
for(int i=0; i<size1; i++){
for(int j=0; j<size1; j++){
newnum = rand()%10;
newvec.push_back(newnum);
}
(*newmtx).push_back(newvec);
newvec.clear();
}
}
void MyServer::JobIsDone()
{
// for(unsigned int i; i<thread)
this->close();
//std::ofstream ofs ("test.txt",std::ios::out);
qDebug()<<"Job is done.Server stopped.";
for (unsigned int i=0;i<finalmtx.size();i++)
qDebug()<<finalmtx[i];
}
//mythread.cpp
#include "mythread.h"
MyThread::MyThread(std::vector<vect>* fst,std::vector<vect>* scnd,std::vector<QByteArray>* final,int* _currentNum, int _size, qintptr ID, QObject *parent ) :
QThread(parent)
{
this->socketDescriptor = ID;
currentNum = _currentNum;
fstmtx = fst;
scndmtx = scnd;
finalmtx = final;
size = _size;
globalcounter = new int;
*globalcounter = 0;
}
void MyThread::run()
{
// thread starts here
qDebug() << " Thread started";
socket = new QTcpSocket();
// set the ID
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
// something's wrong, we just emit a signal
emit error(socket->error());
return;
}
// connect socket and signal
// note - Qt::DirectConnection is used because it's multithreaded
// This makes the slot to be invoked immediately, when the signal is emitted.
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
// connect(this, SIGNAL(JobIsDone()), parent, SLOT(JobIsDone()), Qt::DirectConnection);
// We'll have multiple clients, we want to know which is which
qDebug() << socketDescriptor << " Client connected";
// make this thread a loop,
// thread will stay alive so that signal/slot to function properly
// not dropped out in the middle when thread dies
exec();
}
void MyThread::readyRead()
{
// get the information
//socket->waitForReadyRead(3000);
QByteArray Data = socket->readAll();
int Index = 0;
// QString Data;
if (Data.indexOf("Index:")!=-1)
{
bool ok;
Index = Data.mid(6,Data.indexOf("Vector:")-6).toInt(&ok, 10);
(*finalmtx)[Index] = Data.mid(Data.indexOf("Vector:")+7,Data.size()-Data.indexOf("Vector:")-16);
qDebug()<<(*finalmtx)[Index];
}
QString Data2;
// will write on server side window
qDebug() << socketDescriptor << " Data in: " << Data;
QTextStream os(socket);
os.setAutoDetectUnicode(true);
//os>>Data;
//os.setFieldAlignment(QTextStream::AlignRight);
if((*currentNum)<size){
os<<"Second:";
for(unsigned int i=0; i<(*scndmtx).size();i++)
{
for(unsigned int j=0; j<(*scndmtx).size();j++)
os<< ((*scndmtx)[i][j]);
os<<"_";
}
os<<"First:";
for(unsigned int i=0; i<(*fstmtx).size();i++)
os<<((*scndmtx)[*currentNum][i]);
os<<"Info: "<<(*currentNum);
socket->waitForBytesWritten(2000);
(*currentNum)++;
}
else{
os<<"Job is done!";
os.flush();
socket->waitForBytesWritten(1000);
socket->close();
//this->JobIsDone();
}
if((*globalcounter)==size-1)
this->JobIsDone();
(*globalcounter)++;
//qDebug<<"Counter "<<(*globalcounter);
//socket->write("Hello!!!");
}
void MyThread::disconnected()
{
qDebug() << socketDescriptor << " Disconnected";
socket->deleteLater();
exit(0);
}
//void MyThread::JobIsDone()
//{
//}