Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #include "client.h"
  2. #include <QHostAddress>
  3. Client::Client(QObject* parent): QObject(parent)
  4. {
  5. connect(&client, SIGNAL(connected()),
  6. this, SLOT(startTransfer()));
  7. LENGTH =8;
  8. }
  9. Client::~Client()
  10. {
  11. client.close();
  12.  
  13. }
  14.  
  15. void Client::start(QString address, quint16 port)
  16. {
  17. QHostAddress addr(address);
  18. client.connectToHost(addr, port);
  19. }
  20.  
  21. void Client:: Read(QString filename)
  22. {
  23.  
  24. QFile mfile(filename);
  25.  
  26. if(!mfile.open(QFile::ReadOnly | QFile::Text))
  27. {
  28. qDebug() << "Could Not Open File For Reading!";
  29. return;
  30. }
  31. QTextStream in(&mfile);
  32. //QByteArray ba;
  33. while (!in.atEnd()) {
  34.  
  35. line = in.readAll();
  36.  
  37. qDebug() << line;
  38.  
  39. }
  40.  
  41. mfile.close();
  42. }
  43.  
  44.  
  45. void Client::startTransfer()
  46. {
  47. QString mfilename ="/home/ddm/Desktop/ENV_1_M2000.csv";
  48. Read(mfilename);
  49. QByteArray ba = line.toLocal8Bit();
  50. c_str2 = ba.data();
  51. client.write(c_str2,2048);
  52. }
  53.  
  54. #include "myserver.h"
  55.  
  56. MyServer::MyServer(QObject *parent) :
  57. QTcpServer(parent)
  58. {
  59. }
  60.  
  61. void MyServer :: StartServer()
  62. {
  63. QHostAddress myAddr=QHostAddress("192.168.3.2");
  64. if(!this->listen(myAddr,7350))
  65. {
  66. qDebug() << "Could not start server";
  67. }
  68. else
  69. {
  70. qDebug() << "Listening...";
  71. }
  72. }
  73.  
  74. void MyServer :: incomingConnection(int socketDescriptor)
  75. {
  76. qDebug()<<socketDescriptor<<"Connecting...";
  77. MyThread *thread = new MyThread(socketDescriptor,this);
  78. connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater()));
  79. thread ->start();
  80. }<br/>
  81.  
  82. #include "mythread.h"
  83. #include<QList>
  84. #include <QFile>
  85. #include <QString>
  86. #include <QtDebug>
  87. #include <QTextStream>
  88. #include <QBitArray>
  89. #include<QStringList>
  90. #include <GL/glu.h>
  91. #include <GL/glut.h>
  92.  
  93. #define LENGTH 80
  94. MyThread::MyThread(int ID,QObject *parent) :
  95. QThread(parent)
  96. {
  97. this ->socketDescriptor = ID;
  98. ROWS=100;
  99. COLS=6;
  100. row=0;col=0;
  101. }
  102.  
  103. void MyThread::run()
  104. {
  105. //thread starts here
  106. qDebug()<< socketDescriptor <<"Starting thread";
  107. socket = new QTcpSocket;
  108. if(!socket->setSocketDescriptor(this->socketDescriptor))
  109. {
  110. emit error(socket->error());
  111. return;
  112. }
  113. connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()), Qt::DirectConnection);
  114. connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection);
  115.  
  116. qDebug()<< socketDescriptor <<"Client connected";
  117. exec();
  118. }
  119. void MyThread :: readyRead()
  120. {
  121.  
  122. float array[ROWS][COLS];
  123.  
  124.  
  125. QByteArray Data = socket ->readAll();
  126. QString str(Data);
  127.  
  128. qDebug()<< socketDescriptor <<"Data in:" <<row<<Data;
  129.  
  130. QStringList tempCN = str.split("n");
  131. qDebug()<<"tempCN="<<tempCN;
  132. for(int i=0;i<40;i++)
  133. {
  134. QStringList tempCN1 = tempCN[i].split(",");
  135. qDebug()<<"tempCN1["<<i<<"]="<<tempCN1;
  136. array[i][0] = QString(tempCN1[0]).toFloat();
  137. array[i][1] = QString(tempCN1[1]).toFloat();
  138. array[i][2] = QString(tempCN1[2]).toFloat();
  139. array[i][3] = QString(tempCN1[3]).toFloat();
  140. array[i][4] = QString(tempCN1[4]).toFloat();
  141. array[i][5] = QString(tempCN1[5]).toFloat();
  142. }
  143. for(int i=0;i<40;i++)
  144. {
  145. for(int j=0;j<6;j++)
  146. {
  147.  
  148. qDebug()<<"array["<<i<<"]"<<"["<<j<<"]"<<array[i][j];
  149. }
  150. }
  151. row++;
  152. /* col++*/;
  153.  
  154.  
  155. }
  156.  
  157. void MyThread :: disconnected()
  158. {
  159. qDebug()<< socketDescriptor <<"Disconnected";
  160. socket->deleteLater();
  161. exit(0);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement