Results 1 to 2 of 2

Thread: password problem with client and server

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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
  •  
Qt is a trademark of The Qt Company.