Results 1 to 8 of 8

Thread: Trouble Applying the Fortune Threaded Server

  1. #1
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    1

    Default Trouble Applying the Fortune Threaded Server

    I'm trying to integrate a QTcpServer into my program. My initial attempt was to use the Fortune client to connect to my server, and my server return a simple string. I'm not sure why, but the Fortune client will not connect.

    I'm running both my program and the Fortune Client on my computer, using a connection address of Localhost and port 1000.

    Here is my code:

    Server.h
    Qt Code:
    1. #ifndef SERVER_H
    2. #define SERVER_H
    3. #include <QTcpServer>
    4. class Server : public QTcpServer
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. Server(QObject *parent = 0);
    10.  
    11. protected slots :
    12. void incomingConnection(int socketDescriptor);
    13.  
    14. };
    15.  
    16. #endif // SERVER_H
    To copy to clipboard, switch view to plain text mode 

    Server.cpp
    Qt Code:
    1. #include "Server.h"
    2. #include "ServerThread.h"
    3. #include <QDebug>
    4.  
    5.  
    6. Server::Server(QObject *parent)
    7. : QTcpServer(parent)
    8. {
    9. connect(this, SIGNAL(newConnection()), this, SLOT(incommingConnection(socketDescriptor)));
    10. }
    11.  
    12. void Server::incomingConnection(int socketDescriptor)
    13. {
    14. qDebug() << "Incomming!";
    15. QString fortune = "Test";
    16. ServerThread *thread = new simonServerThread(socketDescriptor, fortune, this);
    17. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    18. thread->start();
    19. }
    To copy to clipboard, switch view to plain text mode 

    ServerThread.h

    Qt Code:
    1. #ifndef SERVERTHREAD_H
    2. #define SERVERTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QTcpSocket>
    6.  
    7. class ServerThread : public QThread
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. ServerThread(int socketDescriptor, const QString &fortune, QObject *parent);
    13.  
    14. void run();
    15.  
    16. signals:
    17. void error(QTcpSocket::SocketError socketError);
    18.  
    19. private:
    20. int socketDescriptor;
    21. QString text;
    22. };
    23.  
    24.  
    25. #endif // SERVERTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    ServerThread.cpp

    Qt Code:
    1. #include "ServerThread.h"
    2.  
    3. ServerThread::ServerThread(int socketDescriptor, const QString &fortune, QObject *parent)
    4. : QThread(parent), socketDescriptor(socketDescriptor), text(fortune)
    5. {
    6. }
    7.  
    8. void ServerThread::run()
    9. {
    10. QTcpSocket tcpSocket;
    11. if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
    12. emit error(tcpSocket.error());
    13. return;
    14. }
    15.  
    16. QByteArray block;
    17. QDataStream out(&block, QIODevice::WriteOnly);
    18. out.setVersion(QDataStream::Qt_4_0);
    19. out << (quint16)0;
    20. out << text;
    21. out.device()->seek(0);
    22. out << (quint16)(block.size() - sizeof(quint16));
    23.  
    24. tcpSocket.write(block);
    25. tcpSocket.disconnectFromHost();
    26. tcpSocket.waitForDisconnected();
    27. }
    To copy to clipboard, switch view to plain text mode 


    I'm calling the server using the following:

    Qt Code:
    1. Server server;
    2. if (!server.listen(QHostAddress::Any, 1000)) {
    3. QMessageBox::critical(this, tr("Threaded Fortune Server"),
    4. tr("Unable to start the server: %1.")
    5. .arg(server.errorString()));
    6. server.close();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for trying to help!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Trouble Applying the Fortune Threaded Server

    Did you start the event loop in the main thread?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    1

    Default Re: Trouble Applying the Fortune Threaded Server

    Yes the GUI event loop has been started.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Trouble Applying the Fortune Threaded Server

    Try connecting to the server with a telnet client and see if you can connect at all. And also try a higher port - ones between 1 and 1024 are reserved for well-known services and (may) require superuser privileges.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    1

    Default Re: Trouble Applying the Fortune Threaded Server

    I changed the port number to 5000 with out any luck.

    I then attempted telneting using this command

    Qt Code:
    1. telnet localhost 5000
    To copy to clipboard, switch view to plain text mode 

    And it couldn't connect. The connection will go through if I omit the port number, so the telnet service is on.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Trouble Applying the Fortune Threaded Server

    What exactly message did you get when trying to connect to the port using telnet? "Connection refused" or something like "Connection closed by remote host"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    1

    Default Re: Trouble Applying the Fortune Threaded Server

    OK, I have the server working correctly now, as I can send/receive data between the server embedded in my program to a small client I created for testing purposes. Now I'm having trouble getting readyRead() to emit on the client side.

    I modeled the embedded client code after the small client I made directly.

    Here is my updated code.

    client.h
    Qt Code:
    1. #ifndef CLIENT_H
    2. #define CLIENT_H
    3. #include <QtGui/QDialog>
    4. #include <QtNetwork>
    5. #include <QVector>
    6.  
    7.  
    8. class client : public QObject
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. client();
    14. ~client();
    15. QString hostname;
    16. QVector<int> sequence;
    17. int state;
    18. int ready;
    19. void requestData();
    20. QTcpSocket* socket;
    21. void readData();
    22.  
    23. private:
    24.  
    25.  
    26. private slots:
    27.  
    28. void readyReadTest();
    29.  
    30. signals:
    31. void dataComplete();
    32. };
    33.  
    34. #endif // CLIENT_H
    To copy to clipboard, switch view to plain text mode 

    client.cpp

    Qt Code:
    1. #include "client.h"
    2. #include <QDebug>
    3.  
    4. client::client()
    5. {
    6. qDebug() << "CLIENT>> \"Client Initialized\"";
    7. ready = 0;
    8. }
    9.  
    10. client::~client()
    11. {
    12.  
    13. }
    14.  
    15.  
    16. void client::requestData(){
    17.  
    18. socket = new QTcpSocket(this);
    19. connect(socket, SIGNAL(readyRead()), this, SLOT(readyReadTest()));
    20. connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
    21. connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
    22. qDebug() << "CLIENT>> CONNECTING TO \"" << hostname << ":5000\"";
    23. QHostAddress address;
    24. address.setAddress(hostname);
    25. socket->connectToHost(address, 5000);
    26. if(socket->isValid()){
    27. qDebug() << "CLIENT>> CONNECTED TO: \""<<hostname << ":5000\"";
    28. }
    29. else{
    30. qDebug() << "CLIENT>> FAILED TO CONNECTED TO: \""<<hostname << ":5000\"";
    31. }
    32. }
    33.  
    34. void client::readData(){
    35. qDebug() << "CLIENT>> ATTEMPTING TO RECEIVE DATA";
    36. int z = sequence.size();
    37. sequence.clear();
    38. QDataStream in(socket);
    39. in.setVersion(QDataStream::Qt_4_1);
    40. QString msg;
    41. in >> msg;
    42. qDebug() << "CLIENT>> MSG " << msg;
    43. if(msg == ""){
    44. qDebug() << "CLIENT>> MSG WAS NOT RECEIVED";
    45. }
    46. else{
    47. qDebug() << "CLIENT>> MSG RECEIVED";
    48. }
    49. delete socket;
    50. emit dataComplete();
    51. }
    52.  
    53. void client::readyReadTest(){
    54. qDebug() << "CLIENT>> READYREAD WAS EMITTED";
    55. }
    To copy to clipboard, switch view to plain text mode 

    server.h

    Qt Code:
    1. #ifndef SERVER_H
    2. #define SERVER_H
    3.  
    4. #include <QtNetwork>
    5.  
    6. class server : public QTcpServer{
    7. Q_OBJECT
    8. public:
    9. server(QObject* parent = 0);
    10. QVector<int> *sequence;
    11. int checkIn;
    12. int state;
    13.  
    14. protected slots:
    15. void sendMessage();
    16. void connectCheck();
    17. };
    18.  
    19. #endif // SERVER_H
    To copy to clipboard, switch view to plain text mode 

    server.cpp


    Qt Code:
    1. #include "server.h"
    2. #include <QDebug>
    3.  
    4. server::server(QObject* parent) : QTcpServer(parent) {
    5. qDebug() << "SERVER>> \"Server Intialized\"";
    6. connect(this, SIGNAL(newConnection()), this, SLOT(sendMessage()));
    7. }
    8.  
    9. void server::sendMessage(){
    10. qDebug() << "SERVER>> PREPARING TO SEND DATA";
    11. QString message;
    12. QTcpSocket* client = this->nextPendingConnection();
    13. QByteArray block;
    14. QDataStream outgoingMessage(&block, QIODevice::WriteOnly);
    15. outgoingMessage.setVersion(QDataStream::Qt_4_1);
    16. int i = 0;
    17. qDebug() << "SERVER>> SEQUENCE SIZE " << (*sequence).size();
    18. for(i; i < (*sequence).size();i++){
    19. message = QString::number((*sequence)[i]);
    20. qDebug() << "SERVER>> SENDING\"" << message << "\"";
    21. outgoingMessage << message;
    22. }
    23. client->write(block);
    24. client->disconnectFromHost();
    25. }
    To copy to clipboard, switch view to plain text mode 

    How I'm calling the server/client

    Qt Code:
    1. if(player == 1){
    2. qDebug() << "ENGINE>> GENERATING NEXT NUMBER";
    3. generate_number();
    4. SERVER->sequence = &sequence_computer;
    5. }
    6. else{
    7. client CLIENT;
    8. CLIENT.hostname = hostname;
    9. while(sequence_computer.size() == 0){
    10. CLIENT.requestData();
    11. sequence_computer = CLIENT.sequence;
    12. qDebug() << "ENGINE>> WAITING FOR CLIENT TO RECEIVE SEQUENCE";
    13. msleep(1000);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    I do have a few unused variables in the header files (debugging and haven't removed them yet)

    Thanks for any help to understand why readyRead() isn't firing.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Trouble Applying the Fortune Threaded Server

    Because there is nothing written to the socket. You are disconnecting the server from the client before the data has a chance to be written. You need to wait for bytesWritten before calling disconnectFromHost.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Network Threaded server problem
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2008, 09:08
  2. Threaded TCP server
    By Sysace in forum Newbie
    Replies: 4
    Last Post: 21st February 2008, 12:37
  3. Threaded server.
    By nithinin2001 in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2007, 16:37

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.