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
New Developer
sharath_moblie
Posts: 104
Registered: ‎09-25-2013
My Device: BB 10

Reading data from TCP socket in BB 10 cascades

Hi All,

 

I am developing an BB 10 cascades app that reads data from server in Json Format. and my Socket Connection Establishment is working good.But some times, I am only reading the part of Json String that sent from server.

please find my code here,

 

int SocketBase::waitForInput(QTcpSocket *socket){
int bytesAvail = -1;

while (socket->state() == QAbstractSocket::ConnectedState && getRunThread()
        && bytesAvail < 0)
{
    printf(" :::  Socket is in ConnectedState ::: \n" );
    if (socket->waitForReadyRead(100))
    {
        bytesAvail = socket->bytesAvailable();
        printf(" :::  Bytes Available ::: %d\n", bytesAvail);
    }

}
if(socket->state() != QAbstractSocket::ConnectedState){
    printf(" :::  Socket not Connected ::: \n" );
}
return bytesAvail;
}

 and 

QString SocketBase::readLine(QTcpSocket *socket,QString param)
{
if (socket->state() != QAbstractSocket::ConnectedState) {
    printf("Socket not connected in Read Line\n");      
}
QString line;
   QByteArray qLine = "";
            QList<QByteArray> listAll;
            int bytesAvail = waitForInput(socket);

            if (bytesAvail > 0) {
                //int symbolCount = 0;
                int cnt = 0;
                bool endOfLine = false;
                bool endOfStream = false;

                while (true) {

                    QByteArray byte=socket->readAll();
                    listAll.append(byte);
                    qDebug()<<"::: Json data  &colon;::"<<byte;

                    if(byte.contains("]")){
                        printf(" ::: Json Reading finished::: ");
                        break;
                    }
                }
            }else{
                printf(" ::: No data found in SocketBase::readLine ::: \n" );
                line="-1";
            }
}
return line;
}

 

I have tried, readAll(),qint64 readLine(char *data, qint64 maxlen) and QByteArray readLine(qint64 maxlen = 0) but did not get solved !!!

 

Please help me,

 

Thanks!!!

Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Reading data from TCP socket in BB 10 cascades

[ Edited ]

Hi,

Socket will return data in variable size chunks which have to be assembled. It's not enough to just read all the data received from socket, you'll need to process additional chunks which will arrive later.

If the data is returned over HTTP protocol, HTTP parser has to be implemented which isn't an easy task.

I suggest using higher level classes such as QNetworkAccessManager for performing HTTP requests.


Andrey Fidrya, @zmeyc on twitter
New Developer
sharath_moblie
Posts: 104
Registered: ‎09-25-2013
My Device: BB 10

Re: Reading data from TCP socket in BB 10 cascades

Thanks fro Reply,

 

But app should read the data from Socket but not using HTTP requests. Because Server only sends the data throuh Socket but not any other HTTP requests.

 

Any suggestion to overcome this using QTcpSocket,

 

Thanks!!!