Xsmael 0 Newbie Poster

I am developing Client-Server application in C++ using Qt framework, but the clients can be android phones and computers(Qt client app) Now i'm having troubles to handle Reception of data on the server side; the server is not receiving data properly.

First, I got things working nicely between the server(Qt app) and the client(Qt app) using these methods for sending and receiving: The size of the message is kept at the beginning of the packet to help check whether the whole message is received or not.

This is the method to send message to the clients

void Server::send(const QString &message)
{
    QByteArray paquet;
    QDataStream out(&paquet, QIODevice::WriteOnly);

    out << (quint16) 0; // just put 0 at the head of the paquet to reserve place to put the size of the message
    out << message; // adding the message
    out.device()->seek(0); // coming back to the head of the paquet
    out << (quint16) (paquet.size() - sizeof(quint16)); // replace the 0 value by the real size


    clientSocket->write(paquet); //sending...

}

This slot is called every time a single paquet is received.

void Server::dataReceived()
{
    forever 
    {
        // 1 : a packet has arrived from any client

        // getting the socket of that client (recherche du QTcpSocket du client)
        QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
        if (socket == 0) 
            return; 

        QDataStream in(socket);

        if (dataSize == 0) // if we don't know the size of data we are suppose to receive...
        {
            if (socket->bytesAvailable() < (int)sizeof(quint16)) // we haven't yet receive the size of the data completly then return...
                 return;

            in >> dataSize; // now we know the amount of data we should get
        }

        if (socket->bytesAvailable() < dataSize) 
            return;

        // Here we are sure we got the whole data then we can startreadind
        QString message;
        in >> message;

        //Processing....

        dataSize = 0; // re-initialize for the coming data
    }
}

This is working well when the server is talking with the Qt app Client, because the same methods are used there, and the size of quint16 will remain the same hover it doesn't work with android client, then i tried another way in which i wanted to ignore the size of the message sent, but format the message in a way such that i can know where it starts and where it ends, then with some controls i can get it however i'm stuck here, cause the data read doesn't contain anything when printed, but his size has a value(which even vary according to the amount of text the client send)!

void Server::dataReceived() // a packet is received!
{

    QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
    if (socket == 0)
        return;


    QByteArray data= socket->readAll(); //reading all data available
    QString message(data)

    qDebug() << data;        // this prints nothing!
    qDebug() << data.size();//  But this prints a number, wich means we got somthing
    qDebug() << message;    // this also prints notghing!

} 

PS: it's not working even for the Qt app Client.

Can you help me find out what's wrong, i'm a bit confused how the tcp protocol is handling the data, and if you could and also advise me a good way for doing this.

here is the android class I made for the purpose

class QTcpSocket implements Runnable {

    private String ip="";
    private int port;
    private Socket socket;
    private PrintWriter printWriter;

    private DataOutputStream dataOutputStream;
    private DataInputStream dataInputStream;

    public QTcpSocket(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }

      public void setIp(String ip) {
          this.ip = ip;
      }

      public String getIp() {
          return this.ip;
      }

    public void setPort(int port) {
        this.port = port;
    }

    public void run() {
        try {
            socket = new Socket(this.ip, this.port);
            //printWriter = new PrintWriter(socket.getOutputStream(), true);
            //sendMessage("Heoollloo serv!!!");
            dataOutputStream = new DataOutputStream( socket.getOutputStream() );
            dataInputStream = new DataInputStream(socket.getInputStream());
            String response = dataInputStream.readUTF();
            } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void sendMessage(String message) {
        try {

            dataOutputStream.writeUTF(message);
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void disconnect() {
        try {

            printWriter.flush();
            printWriter.close();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean isClosed() {
        return socket.isClosed();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.