Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem on the server in my application client-server
Forum Updated to NodeBB v4.3 + New Features

Problem on the server in my application client-server

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tchoninho
    wrote on last edited by
    #1

    Hi,

    I have a class called ClientSocket which inherits of QTcpSocket.

    The first message, the server received and responded for the client;
    After, the request client a new information but the server don't receive.

    If i close the connection, after the first message its work, but i don't do this.

    It follow the source code.

    The client send request
    @
    void ClientStub::sendRequest(const Message msg)
    {
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_6);

        out << (quint16)0;
        out << msg;
    
        out.device()->seek(0);
        out << (quint16)(block.size() - sizeof(quint16));
    
        tcpSocket->write(block);
        tcpSocket->flush();
        sended++;
    

    }
    @

    The server receive and create a ClientSocket object to treat the connection
    @
    void Server::incomingConnection(int socketId)
    {
    ClientSocket *socket = new ClientSocket();
    socket->setSocketDescriptor(socketId);
    }
    @

    ClientSockt code, it is exchange data with the client
    @
    static int countClientSocket = 0;

    ClientSocket::ClientSocket(QObject *parent)
    : QTcpSocket(parent)
    {
    connect(this, SIGNAL(readyRead()), this, SLOT(receive()));
    // connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));

    countClientSocket++;
    
    //#ifdef DEBUG_MACHINE
    printf("ctor countClientSocket: %i \n", countClientSocket);
    std::cout.flush();
    //#endif
    
    nextBlockSize = 0;
    

    }

    ClientSocket::~ClientSocket()
    {
    // abort();
    countClientSocket--;

    #ifdef DEBUG_MACHINE
    printf("dtor countClientSocket: %i \n", countClientSocket);
    std::cout.flush();
    #endif

    }

    bool ClientSocket::setSocketDescriptor(int socketDescriptor)
    {
    QTcpSocket::setSocketDescriptor(socketDescriptor);
    return waitForReadyRead(5);
    }

    void ClientSocket::receive()
    {
    QDataStream in(this);
    in.setVersion(QDataStream::Qt_4_6);

    if (nextBlockSize == 0)
    {
        if (bytesAvailable() < sizeof(quint16))
            return;
    
        in >> nextBlockSize;
    }
    
    if (bytesAvailable() < nextBlockSize)
        return;
    
    Message msg;
    in >> msg;
    
    invoke(msg);
    

    }

    void ClientSocket::invoke(Message &msg)
    {
    QList<QVariant> msgContents = msg.getContents();
    WordCounter task = msgContents.first().value<WordCounter>();

    msg.setTypes(Message::Info);
    msg.setContents(QList<QVariant>() << QString("received request"));
    
    response(msg);
    printf("%i - received request\n", msg.getId());
    
    QByteArray normalizedSignature = QMetaObject::normalizedSignature("execute (QList<QVariant> &)");
    
    QVariant retVal;
    const QMetaObject *metaObject = 0;
    metaObject = task.metaObject();
    int methodIndex = -1;
    methodIndex = metaObject->indexOfMethod(normalizedSignature);
    QMetaMethod method = metaObject->method(methodIndex);
    
    printf("Processing request of: %i \n", task.getKey());
    
    QElapsedTimer timer;
    timer.start();
    method.invoke(
            &task,
            Qt::DirectConnection, // Qt::AutoConnection,
            Q_RETURN_ARG(QVariant, retVal),
            Q_ARG(QList<QVariant>, msgContents)
            );
    int t = timer.elapsed();
    
    msg.setTypes(Message::Result);
    msg.setContents(QList<QVariant>() << retVal 
                 << QString("Time elapsed: %1 miliseconds").arg(t)); //  <<  "done!");
    printf("id: %i, Result: %i \n--------&gt;>> Time elapsed: %i miliseconds\n", task.getKey(), retVal.toInt(), t);
    
    response(msg);
    
    // waitForDisconnected();
    // close();
    

    }

    void ClientSocket::response(Message msg)
    {
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_6);

    out << quint16(0);
    out << msg;
    
    out.device()->seek(0);
    out << quint16(block.size() - sizeof(quint16));
    
    write(block);
    flush();
    

    }

    @

    Thanks!!

    Computer Scientist
    Belo Horizonte - Brasil
    C++ Development
    Qt Development

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      reset nextBlockSize to 0 once you're done with the workload.

      Otherwise you will never get the next block size in line 43.

      Set some breakpoints and run the app in a debugger, you'll see that you enter the if block only once.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved