mytcpsocket ifndef MYTCPSOCKET_H define MYTCPSOCKET_H include QObject

  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
//mytcpsocket.h
#ifndef MYTCPSOCKET_H
#define MYTCPSOCKET_H
#include <QObject>
#include <QTcpSocket>
#include <QDebug>
#include <thread>
#include <iostream>
#include <fstream>
typedef std::vector<double> vect;
class MyTcpSocket : public QObject
{
Q_OBJECT
public:
explicit MyTcpSocket(QObject *parent = 0);
void doConnect();
std::vector<vect> Convert(QByteArray mtx, int size);
std::vector<double> Convert(QByteArray mtx);
signals:
public slots:
void readyRead();
void disconnected();
private:
QTcpSocket *socket;
QTextStream os;
QString data;
//char data[];
qint64 size, toberead;
QByteArray newdata;
};
#endif // MYTCPSOCKET_H
//main
#include <QCoreApplication>
#include "mytcpsocket.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyTcpSocket s;
s.doConnect();
return a.exec();
}
//mytcpsocket.cpp
#include "mytcpsocket.h"
MyTcpSocket::MyTcpSocket(QObject *parent) :
QObject(parent)
{
// Newdata = "noinfo";
}
void MyTcpSocket::doConnect()
{
socket = new QTcpSocket(this);
socket->connectToHost("localhost", 1234);
if(socket->waitForConnected(5000))
{
qDebug()<<"Connected!";
}
else
{
qDebug()<<"Not connected!";
}
os.setDevice(socket);
//os.setAutoDetectUnicode(true);
// os.setFieldAlignment(QTextStream::AlignRight);
//socket->write("Hello server\r\n\r\n");
os<<"Hello, server!";
os.flush();
socket->waitForBytesWritten(1000);
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
}
void MyTcpSocket::readyRead()
{
double summ = 0;
//os.setAutoDetectUnicode(true);
//os<<"12345";
// socket->write("Hello server\r\n\r\n");
while(newdata!="Job is done!")
{
socket->waitForReadyRead(3000);
//toberead = socket->bytesAvailable();
qDebug()<<"Reading: "<<socket->bytesAvailable();;
// Newdata = socket->readAll();
//size = socket->readLine(data, toberead);
//for(qint64 i=0; i<toberead; i++){
//std::cout<<data[i];
//QString newtext(socket->readAll());
//qDebug()<<newtext;
//data = newtext;
newdata = socket->readAll();
//os>>newdata;
if(newdata!="Job is done!")
{
double finalIndex = newdata.mid(newdata.indexOf("Info:")+6).toDouble();
qDebug()<<"Final index: "<<finalIndex;
QByteArray mtx2 = newdata.mid(newdata.indexOf("Second:")+7,newdata.indexOf("First:")-7);
QByteArray mtx1 = newdata.mid(newdata.indexOf("First:")+6,newdata.indexOf("Info")-newdata.indexOf("First:")-6);
//qDebug()<<mtx2;
//qDebug()<<mtx1;
//qDebug()<<mtx1.size()<<" "<<newdata.indexOf("First:")<<newdata.indexOf("Info");
std::vector<vect> mtx2double = Convert(mtx2,mtx1.size());
std::vector<double> mtx1double = Convert(mtx1);
//qDebug()<<newdata.indexOf("Info: ");
//qDebug()<<newdata;
//}
std::ofstream ofs ("test.txt", std::ofstream::app);
for(unsigned int i=0; i<mtx2double.size(); i++)
{
for(unsigned int j=0; j<mtx2double.size();j++)
ofs<<mtx2double[i][j]<<" ";
ofs<<std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
ofs<<"counting:"<<std::endl;
ofs<<"Index:"<<finalIndex;
os<<"Index:"<<finalIndex;
ofs<<"Vector:";
os<<"Vector:";
for(unsigned int i=0; i<mtx1double.size(); i++)
{
for(unsigned int j=0; j<mtx1double.size(); j++)
{
summ+=mtx1double[j]*mtx2double[j][i];
}
//ofs<<summ<<"_";
os<<summ<<" ";
summ = 0;
}
ofs<<std::endl;
os<<"\nCounted!";
//socket->write("Counted!");
os.flush();
socket->waitForBytesWritten(1000);
}
else
{
os<<"Finished";
os.flush();
socket->waitForBytesWritten(1000);
}
}
qDebug()<<"Job is done!";
socket->close();
}
std::vector<vect> MyTcpSocket::Convert(QByteArray mtx, int size)
{
QByteArray newsign;
int k = 0;
std::vector<vect> newmtx;
std::vector<double> newvec;
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
newsign.append(mtx[k]);
newvec.push_back(newsign.toDouble());
k++;
newsign.clear();
}
newmtx.push_back(newvec);
newvec.clear();
k++;
}
return newmtx;
}
std::vector<double> MyTcpSocket::Convert(QByteArray mtx)
{
QByteArray newsign;
std::vector<double> newvec;
for(int i=0; i<mtx.size(); i++)
{
newsign.append(mtx[i]);
newvec.push_back(newsign.toDouble());
newsign.clear();
}
return newvec;
}
void MyTcpSocket::disconnected()
{
qDebug() << " Disconnected";
socket->deleteLater();
exit(0);
}