Header:

code Code:
  1. #ifndef INDG_SOCKET_H
  2. #define INDG_SOCKET_H
  3.  
  4. #include <QTcpSocket>
  5. #include <QtGlobal>
  6.  
  7. class indg_socket : public QTcpSocket
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit indg_socket(QObject *parent = 0);
  12. qint32 computeauth(qint32 random1, qint32 random2, qint32 oper);
  13.  
  14. protected:
  15. void readyRead();
  16.  
  17. private slots:
  18. void readpackets();
  19. };
  20.  
  21. #endif // INDG_SOCKET_H
To copy to clipboard, switch view to plain text mode 

CPP:

code Code:
  1. #include "indg_socket.h"
  2.  
  3. #include <QtNetwork>
  4. #include <QtDebug>
  5. #include <QByteArray>
  6. #include <QtGlobal>
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <time.h>
  12.  
  13. bool isClientAuthenticated;
  14. qint32 random1;
  15. qint32 random2;
  16. qint32 oper;
  17. qint32 result;
  18.  
  19. QByteArray auth; //65500 do you want to authenticate?
  20. QByteArray authme; //65501 authenticate me
  21. QByteArray authq; //65502 auth question for client
  22. QByteArray autha; //65503 auth answer for server
  23. QByteArray userkeys;//65504 i'm sending user keys
  24. QByteArray wmp; //65505 whole map for client
  25. QByteArray partd; //65506 partial data for client
  26.  
  27. QByteArray authok; //65508 auth OK
  28. QByteArray authfailure; //65508 auth FAILED
  29. QByteArray admstat; //65509 admin stats
  30. QByteArray admstatq; //65510 admin stats query
  31.  
  32. QByteArray sockdata;
  33. QDataStream dsauth(&auth,QIODevice::ReadWrite);
  34. QDataStream dsauthme(&authme,QIODevice::ReadWrite);
  35. QDataStream dsauthq(&authq,QIODevice::ReadWrite);
  36. QDataStream dsautha(&autha,QIODevice::ReadWrite);
  37. QDataStream dsuserkeys(&userkeys,QIODevice::ReadWrite);
  38. QDataStream dswmp(&wmp,QIODevice::ReadWrite);
  39. QDataStream dspartd(&partd,QIODevice::ReadWrite);
  40. QDataStream dsadmstat(&admstat,QIODevice::ReadWrite);
  41. QDataStream dsadmstatq(&admstatq,QIODevice::ReadWrite);
  42.  
  43. QDataStream dsauthok(&authok,QIODevice::ReadWrite);
  44. QDataStream dsauthfailure(&authfailure,QIODevice::ReadWrite);
  45.  
  46. indg_socket::indg_socket(QObject *parent) :
  47. QTcpSocket(parent)
  48. {
  49. //generate random numbers
  50. srand(time(NULL));
  51. random1=rand()%256;
  52. //sleep(3);
  53. srand(time(NULL));
  54. random2=rand()%256;
  55. srand((time(NULL)+5));
  56. oper=rand()%5;
  57.  
  58. result=computeauth(random1, random2, oper);
  59.  
  60.  
  61. dsauth << (quint16)65500;
  62. dsauthme << (quint16)65501;
  63. dsauthq << (quint16)65502;
  64. dsautha << (quint16)65503;
  65. dsuserkeys << (quint16)65504;
  66. dsauthok << (quint16)65508;
  67. dsauthfailure << (quint16)65509;
  68. dsadmstatq << (quint16)65510;
  69.  
  70. isClientAuthenticated=false;
  71. connect(this, SIGNAL(readyRead()), this, SLOT(readpackets()));
  72. connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
  73. }
  74.  
  75. void indg_socket::readpackets()
  76. {
  77. sockdata=this->readAll();
  78. sockdata.truncate(100); //truncate if packet greater than 100 bytes
  79.  
  80. if(sockdata==authme){
  81. qDebug() << "client wanna authenticate!!!";
  82. qDebug() << "sending question " << random1 << " op" << oper << " " << random2 << " = " << result;
  83. authq.clear();
  84. dsauthq << (quint16)65502 << (quint8)random1 << (quint8)random2 << (quint8)oper;
  85. qDebug() << authq.toHex();
  86. this->write(authq, 40);
  87. sockdata.clear();
  88. return;
  89. }
  90.  
  91. if(sockdata==autha){
  92. qDebug() << "getting auth answer from client... checking";
  93. qDebug() << "OK";
  94. this->write(authok);
  95. sockdata.clear();
  96. return;
  97. }
  98.  
  99. if(sockdata.startsWith(admstatq)){
  100. qDebug() << "getting auth answer from client... checking";
  101. qDebug() << "OK";
  102. this->write(authok);
  103. sockdata.clear();
  104. return;
  105. }
  106.  
  107. return;
  108. }
  109.  
  110. qint32 indg_socket::computeauth(qint32 random1, qint32 random2, qint32 oper)
  111. {
  112. qint8 res;
  113. switch(oper){
  114. case 1:
  115. res=random1+random2;
  116. return res;
  117. case 2:
  118. res=random1-random2;
  119. return res;
  120. case 3:
  121. res=random1*random2;
  122. return res;
  123. case 4:
  124. res=random1/random2;
  125. return res;
  126. }
  127. }
To copy to clipboard, switch view to plain text mode 

client side


code Code:
  1. #ifndef AIT_SOCKET_H
  2. #define AIT_SOCKET_H
  3.  
  4. #include <QTcpSocket>
  5.  
  6. class ait_socket : public QTcpSocket
  7. {
  8. Q_OBJECT
  9. public:
  10. explicit ait_socket(QObject *parent = 0);
  11.  
  12. protected:
  13. void readyRead();
  14.  
  15. private slots:
  16. void readpackets();
  17. };
  18.  
  19. #endif // AIT_SOCKET_H
To copy to clipboard, switch view to plain text mode 

CPP

code Code:
  1. #include "ait_socket.h"
  2.  
  3. #include <QtNetwork>
  4. #include <QtDebug>
  5. #include <QByteArray>
  6. #include <QtGlobal>
  7. #include <stdio.h>
  8.  
  9. QByteArray auth; //65500 do you want to authenticate?
  10. QByteArray authme; //65501 authenticate me
  11. QByteArray authq; //65502 auth question for client
  12. QByteArray autha; //65503 auth answer for server
  13. QByteArray userkeys;//65504 i'm sending user keys
  14. QByteArray wmp; //65505 whole map for client
  15. QByteArray partd; //65506 partial data for client
  16.  
  17. QByteArray authok; //65508 auth OK
  18. QByteArray authfailure; //65508 auth FAILED
  19. QByteArray admstat; //65509 admin stats
  20. QByteArray admstatq; //65510 admin stats query
  21.  
  22. QByteArray sockdata;
  23. QDataStream dsauth(&auth,QIODevice::ReadWrite);
  24. QDataStream dsauthme(&authme,QIODevice::ReadWrite);
  25. QDataStream dsauthq(&authq,QIODevice::ReadWrite);
  26. QDataStream dsautha(&autha,QIODevice::ReadWrite);
  27. QDataStream dsuserkeys(&userkeys,QIODevice::ReadWrite);
  28. QDataStream dswmp(&wmp,QIODevice::ReadWrite);
  29. QDataStream dspartd(&partd,QIODevice::ReadWrite);
  30. QDataStream dsadmstat(&admstat,QIODevice::ReadWrite);
  31.  
  32. QDataStream dsauthok(&authok,QIODevice::ReadWrite);
  33. QDataStream dsauthfailure(&authfailure,QIODevice::ReadWrite);
  34.  
  35.  
  36. ait_socket::ait_socket(QObject *parent) :
  37. QTcpSocket(parent)
  38. {
  39. dsauth << (quint16)65500;
  40. dsauthme << (quint16)65501;
  41. dsauthq << (quint16)65502;
  42. dsautha << (quint16)65503;
  43. dsuserkeys << (quint16)65504;
  44. dsauthok << (quint16)65508;
  45. dsauthfailure << (quint16)65509;
  46.  
  47. connect(this, SIGNAL(readyRead()), this, SLOT(readpackets()));
  48. connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
  49. }
  50.  
  51. void ait_socket::readpackets()
  52. {
  53. sockdata=this->readAll();
  54.  
  55. if(sockdata.startsWith(auth)){
  56. qDebug() << "server asking for authentication!!!";
  57. qDebug() << "OK, writing authme";
  58. this->write(authme);
  59. sockdata.clear();
  60. return;
  61. }
  62.  
  63. if(sockdata.startsWith(authq)){
  64. qDebug() << "server sending auth question!!!";
  65. qDebug() << sockdata.toHex();
  66. qDebug() << "OK, writing autha";
  67. this->write(autha);
  68. sockdata.clear();
  69. return;
  70. }
  71.  
  72. if(sockdata.startsWith(authok)){
  73. qDebug() << "auth passed!";
  74. this->write(userkeys);
  75. sockdata.clear();
  76. return;
  77. }
  78. return;
  79. }
To copy to clipboard, switch view to plain text mode