How to read From Client through server example QT Client Server ?



  • Hi,

    I am very new to qt.. I am trying to run the Fortune Client Server example of Qt. But in Example only client can read the things from the server. I want to edit the code so that I can read the Data from client side also by clicking a button just like in client side.

    I have tried only to run examples and then to do same code used in client side for reading in the server side and vice versa..but i am getting error likes

    I am getting error in the sever as QTcpserver does’t have bytesavailable()

    No matchin functiomn to cal QDatastream
    abort() is not defined in tcp server so in client programs for many function

    Please help so that i can continue my work in QT


  • Moderators

    QTcpServer facilitates in establishing the information exchange. It opens a port and waits for clients to connect. When a client connects to QTcpServer is providing a pointer to a QTcpSocket.
    The communication is basically performed between the two sockets (one on the server side and one on the client side).
    Have a look at server.cpp, you will find :
    @
    void Server::sendFortune()
    {
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_0);
    out << (quint16)0;
    out << fortunes.at(qrand() % fortunes.size());
    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));

     // this provides the socket for coomunication with the client
     QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
     connect(clientConnection, SIGNAL(disconnected()),
             clientConnection, SLOT(deleteLater()));
    
     clientConnection->write(block);
     clientConnection->disconnectFromHost();
    

    }
    @

    You may keep the socket for further communication.



  • No actually I want to send the information from server to client. I am using client server fortune example given in "Your text to link here...":http://doc.qt.nokia.com/4.7-snapshot/network-fortuneserver.html.
    I want to send the data from client to server in this example by making a pushbutton inn server. Please if you can try to tell me exactly what i have to do for this. I am getting the errors when I am trying to include the same files which client uses to get data from server. Please help


  • Moderators

    Yes, but a server is only working as a server until a connection is established. Certainly the server keeps open the port and waits possibily for other clients to connect.
    However, when a server receives a connection request, it will emit a signal "newConnetion() ":http://developer.qt.nokia.com/doc/qt-4.8/qtcpserver.html#newConnection
    In the handling of the signal one would retrieve the socket with "nextPendingConnection()":http://developer.qt.nokia.com/doc/qt-4.8/qtcpserver.html#nextPendingConnection . Keep this QTcpSocket and build the same reading mimick around as you have in the client with QTcpSocket.
    The whole communication goes only between both QTcpSockets. When the connection is established it does not matter anymore who is client and who is server.

    In client.cpp you read in this routine
    @
    void Client::readFortune()
    {
    QDataStream in(tcpSocket);
    in.setVersion(QDataStream::Qt_4_0);

     if (blockSize == 0) {
         if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
             return;
    
         in >> blockSize;
     }
    
     if (tcpSocket->bytesAvailable() < blockSize)
         return;
    
     QString nextFortune;
     in >> nextFortune;
    
     if (nextFortune == currentFortune) {
         QTimer::singleShot(0, this, SLOT(requestNewFortune()));
         return;
     }
    
     currentFortune = nextFortune;
     statusLabel->setText(currentFortune);
     getFortuneButton->setEnabled(true);
    

    }
    @

    tcpSocket in there is a QTcpSocket pointer.

    If you check the send method of the server ( see previous posting) there is also a QTcpSocket pointer used. It is called clientConnection there. You need to keep it permanently. It will emit the same signals as the tcpSocket on your client side, because it is the same class.



  • the code you posted here is same as given in qt example and is working fine.. but when i am trying to send the fortunes from the client to server i have to use
    @void Client::sendFortune()
    {
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_0);
    out << (quint16)0;
    out << fortunes.at(qrand() % fortunes.size());
    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));
    @
    i have to write this function in client but its giving errors.. Can you plz plz post the code of both Client and server with changes so that both can mutually talk. or else plz guide me.


  • Moderators

    What compiler and platform are you using?
    Please post the error messages from compiling.



  • i am using Qt Creator latest. Actually the example is only a one way communition from sever to client we send Fortunes. So i copied same functions in clients of server and in sever of client so that mutual communication can take place. but may functions are not recognized

    I m getting error in the sever as QTcpserver does’t have bytesavailable()

    No matchin functiomn to cal QDatastream abort() is not defined in tcp server so in client programs for many function


  • Moderators

    QTcpServer is NOT QTcpSocket !!

    Therefore the functions cannot match. I can imagine that you are receiving buckets of compile errors.

    How much C++ experience you have?



  • I tried to bring the necessary changes but still the error remains same.. meanwhile I resolved it through other means.thanks I am entirely new in QT programming



  • here is the code through which i did this.. Any bdy following the post can see and can contact for complete code.. @networkSession = new QNetworkSession(config, this);
    connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));

       networkSession->open();
    

    } else {
    sessionOpened();
    }

    }

    void Server::sessionOpened()
    {
    // Save the used configuration
    if (networkSession) {
    QNetworkConfiguration config = networkSession->configuration();
    QString id;
    if (config.type() == QNetworkConfiguration::UserChoice)
    id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
    else
    id = config.identifier();

        QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
        settings.beginGroup(QLatin1String("QtNetwork"));
        settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id);
        settings.endGroup();
    }
    
    tcpServer = new QTcpServer(this);
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
    if (!tcpServer->listen())  {
        return;
    }
    QString ipAddress;
    QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
    
    
    // use the first non-localhost IPv4 address
    for ( int i = 0; i < ipAddressesList.size(); ++i)  {
        if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
            ipAddressesList.at(i).toIPv4Address())  {
            ipAddress = ipAddressesList.at(i).toString();
            break;
        }
    }
    // if we did not find one, use IPv4 localhost
    if (ipAddress.isEmpty())
        ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
    ipobj->setProperty("text", ipAddress);
    portobj->setProperty("text", tcpServer->serverPort());
    

    }

    void Server::acceptConnection(void){
    qDebug() << "connection accepted";
    client_socket = tcpServer->nextPendingConnection();
    connect(client_socket, SIGNAL(readyRead()),this, SLOT(startRead()));
    }

    void Server::startRead(void){
    qDebug() << "command read";
    char buffer[1024] = {0};
    int no;
    no = client_socket->read(buffer, client_socket->bytesAvailable());

    if(no == 4){
        lightobj->setProperty("color", "#00ff00");
        lighttext->setProperty("text", "Lights on");
    }
    if(no == 5){
        lightobj->setProperty("color", "#ff0000");
        lighttext->setProperty("text", "Lights off");
    }
    
    qDebug() << buffer;
    qDebug() << no;
    
    
    }
    

    Server::~Server(){
    client_socket->close();
    qDebug() << "closed server";
    }@



  • Hi.. @s_singh
    Can you please send the complete code for me i am also trying the same.. please i am very new to Qt.. please.. my id: vishuhm911@gmail.com



  • Hi..if any one of you have complete code .please send it to me.

    vishnu_reddy220@yahoo.com

    Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.