Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Highlighted
New Developer
Posts: 14
Registered: ‎05-14-2014
My Device: Blackberry Z10
My Carrier: Mobile Developer

How to read response from QTcpSocket

Hi everyone, i have a problem.

 

I use QTcpSocket as client, and connected to host (server). I send my QByteArray to server. And Server will response my message and send the string response to client (my device). 

 

and how can I read that response from server ? 

 

thanks all, GBU

Developer
Posts: 543
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: Telekom Slovakia

Re: How to read response from QTcpSocket

https://www.youtube.com/watch?v=u5OdR46542M

https://www.youtube.com/watch?v=j9uAfTAZrdM



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
New Developer
Posts: 10
Registered: ‎09-01-2013
My Device: Z10
My Carrier: Videocon

Re: How to read response from QTcpSocket

I am not writing whole code but you will get i pretty idea through this usually i prefer development in QT so here is the idea

 

Client.h

 


#include 
#include 

class Client : public QObject
{
    Q_OBJECT
public:
    explicit Client(QObject *parent = 0);

public slots:
    bool connectToHost(QString host);
    bool writeData(QByteArray data);

private:
    QTcpSocket *socket;
};

 Client.cpp


#include "client.h"

static inline QByteArray IntToArray(qint32 source);

Client::Client(QObject *parent) : QObject(parent)
{
    socket = new QTcpSocket(this);
}

bool Client::connectToHost(QString host)
{
    socket->connectToHost(host, 1024);
    return socket->waitForConnected();
}

bool Client::writeData(QByteArray data)
{
    if(socket->state() == QAbstractSocket::ConnectedState)
    {
        socket->write(IntToArray(data.size())); //write size of data
        socket->write(data); //write the data itself
        return socket->waitForBytesWritten();
    }
    else
        return false;
}

QByteArray IntToArray(qint32 source) //Use qint32 to ensure that the number have 4 bytes
{
    //Avoid use of cast, this is the Qt way to serialize objects
    QByteArray temp;
    QDataStream data(&temp, QIODevice::ReadWrite);
    data << source;
    return temp;
}
New Developer
Posts: 10
Registered: ‎09-01-2013
My Device: Z10
My Carrier: Videocon

Re: How to read response from QTcpSocket

Ohh and if you were asking in regarding to java then leave a reply i will write one code for that also i got this thing my head after posting previous reply