Results 1 to 2 of 2

Thread: password problem with client and server

  1. #1
    Join Date
    Jul 2008
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question password problem with client and server

    hi,

    a few days before, I have asked similar.
    with the code below the client doesen't get the data with bad or good password.

    the thread.cpp for the client:
    Qt Code:
    1. #include "netthread.h"
    2.  
    3. NetThread::NetThread(const QString &hostName, quint16 port, QByteArray passw, QObject *parent) : QThread(parent), quit(false)
    4. {
    5. this->hostName = hostName;
    6. this->port = port;
    7. this->passw = passw;
    8.  
    9. QMutexLocker locker(&mutex);
    10. }
    11. void NetThread::run()
    12. {
    13. mutex.lock();
    14. QString serverName = hostName;
    15. quint16 serverPort = port;
    16. mutex.unlock();
    17. while(!quit)
    18. {
    19. const int Timeout = 5 * 1000;
    20.  
    21. qDebug() << serverName;
    22. qDebug() << serverPort;
    23.  
    24. sslsocket = new QSslSocket;
    25. sslsocket->connectToHostEncrypted(serverName, serverPort);
    26. sslsocket->ignoreSslErrors();
    27.  
    28. if(!sslsocket->waitForEncrypted())
    29. {
    30. emit error(sslsocket->error(), sslsocket->errorString());
    31. return;
    32. }
    33.  
    34. sslsocket->write(passw);
    35.  
    36. qDebug() << sslsocket->bytesToWrite();
    37. sslsocket->flush();
    38.  
    39. while (sslsocket->bytesAvailable() < (int)sizeof(quint16))
    40. {
    41. if (!sslsocket->waitForReadyRead(Timeout))
    42. {
    43. emit error(sslsocket->error(), sslsocket->errorString());
    44. return;
    45. }
    46. }
    47.  
    48. quint16 blockSize;
    49. QDataStream in(sslsocket);
    50. in.setVersion(QDataStream::Qt_4_0);
    51. in >> blockSize;
    52.  
    53. while(sslsocket->bytesAvailable() < blockSize)
    54. {
    55. if (!sslsocket->waitForReadyRead(Timeout))
    56. {
    57. emit error(sslsocket->error(), sslsocket->errorString());
    58. return;
    59. }
    60. }
    61. QMutexLocker locker(&mutex);
    62.  
    63. QByteArray logfile;
    64. in >> logfile;
    65. qDebug() << "NetThread: " << logfile.data();
    66. emit newNet(logfile);
    67.  
    68. cond.wait(&mutex);
    69. serverName = hostName;
    70. serverPort = port;
    71. }
    72. }
    To copy to clipboard, switch view to plain text mode 
    and the thread.cpp for the server:
    Qt Code:
    1. #include "thread.h"
    2.  
    3. SslThread::SslThread(int socketDescriptor, QString &log, QObject *parent) : QThread(parent), socketDescriptor(socketDescriptor)
    4. {
    5. logfile = new QFile(log);
    6. pass_control = ":passwort";
    7. security = false;
    8. }
    9. void SslThread::run()
    10. {
    11. serverSocket = new QSslSocket();
    12. connect(serverSocket, SIGNAL(readyRead()), this, SLOT(lesen()));
    13. while(true)
    14. {
    15. QByteArray pass("certificatpasswort");
    16. serverSocket->setLocalCertificate("server.cert.crt");
    17. serverSocket->setPrivateKey("privkey.pem",QSsl::Rsa,QSsl::Pem,pass);
    18.  
    19. if(serverSocket->setSocketDescriptor(socketDescriptor))
    20. {
    21. connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready()));
    22. serverSocket->startServerEncryption();
    23. }
    24. else
    25. {
    26. emit error(serverSocket->error());
    27. delete serverSocket;
    28. return;
    29. }
    30. qDebug() << serverSocket->peerAddress();
    31.  
    32. if(security)
    33. {
    34. qDebug() << "I'm inside";
    35. if(!(logfile->open(QIODevice::ReadOnly | QIODevice::Text)))
    36. {
    37. qDebug() << "Lese Fehler!";
    38. exit();
    39. }
    40. QByteArray log = logfile->readAll();
    41.  
    42. QByteArray block;
    43. QDataStream out(&block, QIODevice::WriteOnly);
    44. out.setVersion(QDataStream::Qt_4_0);
    45. out << (quint16)0;
    46. out << log.data();
    47. out.device()->seek(0);
    48. out << (quint16)(block.size() - sizeof(quint16));
    49.  
    50.  
    51. serverSocket->write(block);
    52. qDebug() << serverSocket->bytesToWrite();
    53. serverSocket->flush();
    54. }
    55.  
    56. serverSocket->waitForDisconnected();
    57. serverSocket->disconnectFromHost();
    58. }
    59. }
    60. void SslThread::ready()
    61. {
    62. qDebug() << "Connection encrypted!";
    63. }
    64. char* SslThread::toChar(QString &string)
    65. {
    66. QByteArray ba = string.toLatin1();
    67. char *c_str2 = ba.data();
    68. return c_str2;
    69. }
    70. void SslThread::lesen()
    71. {
    72. QByteArray line = serverSocket->readAll();
    73.  
    74. //qDebug() << line;
    75.  
    76. if(line.startsWith(':'))
    77. {
    78. qDebug() << "password: " << line;
    79. if(line != pass_control)
    80. {
    81. security = false;
    82. //serverSocket->abort();
    83. qDebug() << "password: " << line << "control: " << pass_control;
    84. }
    85. else
    86. {
    87. security = true;
    88. }
    89.  
    90.  
    91. }
    92. //...
    93. }
    To copy to clipboard, switch view to plain text mode 
    the server tells me
    moon:/home/lard/Server# ./Server
    QHostAddress( "127.0.0.1" )
    Connection encrypted!
    password: "passwort"
    Password true

    the server is never inside if(security) so he doesn't send the data.

    and the client:
    network operation timeout

    i have tested much more versions of this, but i can't find a solution.
    can someone please help?

    mate
    Last edited by mate; 19th July 2008 at 09:48.

  2. #2
    Join Date
    Jul 2008
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile Re: password problem with client and server

    ok, the problem was on the server-side. this works:
    Qt Code:
    1. void SslThread::run()
    2. {
    3. serverSocket = new QSslSocket();
    4. connect(serverSocket, SIGNAL(readyRead()), this, SLOT(read()));
    5. while(true)
    6. {
    7. QByteArray pass("ssl-cert-password");
    8. serverSocket->setLocalCertificate("server.cert.crt");
    9. serverSocket->setPrivateKey("privkey.pem",QSsl::Rsa,QSsl::Pem,pass);
    10.  
    11. if(serverSocket->setSocketDescriptor(socketDescriptor))
    12. {
    13. connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready()));
    14. serverSocket->startServerEncryption();
    15. }
    16. else
    17. {
    18. emit error(serverSocket->error());
    19. delete serverSocket;
    20. return;
    21. }
    22. qDebug() << serverSocket->peerAddress();
    23.  
    24. serverSocket->waitForDisconnected();
    25. serverSocket->disconnectFromHost();
    26. }
    27. }
    28. ...
    29. void SslThread::read()
    30. {
    31. QByteArray line = serverSocket->readAll();
    32.  
    33. //qDebug() << line;
    34.  
    35. if(line.startsWith(':'))
    36. {
    37. qDebug() << "password: " << line;
    38. if(line != pass_control)
    39. {
    40. serverSocket->abort();
    41. qDebug() << "password: " << line << "control: " << pass_control;
    42. }
    43. else
    44. {
    45. if(!(logfile->open(QIODevice::ReadOnly | QIODevice::Text)))
    46. {
    47. qDebug() << "Error!";
    48. exit();
    49. }
    50. QByteArray log = logfile->readAll();
    51. QByteArray block;
    52. QDataStream out(&block, QIODevice::WriteOnly);
    53. out.setVersion(QDataStream::Qt_4_0);
    54. out << (quint16)0;
    55. out << log.data();
    56. out.device()->seek(0);
    57. out << (quint16)(block.size() - sizeof(quint16));
    58.  
    59. qDebug() << serverSocket->bytesToWrite();
    60. serverSocket->write(block);
    61. serverSocket->flush();
    62. qDebug() << serverSocket->bytesToWrite();
    63. }
    64. }
    65. ...
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to mate for this useful post:

    l2show (15th October 2010)

Similar Threads

  1. Network Threaded server problem
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2008, 09:08
  2. Sending string from QT client to Java server
    By seanmu13 in forum Qt Programming
    Replies: 10
    Last Post: 3rd July 2007, 12:52
  3. Sending string from QT client to Java server
    By seanmu13 in forum Newbie
    Replies: 3
    Last Post: 3rd July 2007, 12:20
  4. The server cannot write to the client. Why?
    By probine in forum Newbie
    Replies: 12
    Last Post: 24th March 2006, 17:14
  5. synching client readings to server output
    By OnionRingOfDoom in forum Qt Programming
    Replies: 14
    Last Post: 28th January 2006, 18:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.