PDA

View Full Version : Problem in printing the data return /read from QTcpsocket



skumar434
19th February 2009, 16:23
Hi ,
I trying the copy the data read form QtcpSocket to a text edit. I was able to connect to the destination machine , but facing problem in reading. I have just started using QT so please forgive my silly mistakes .

Here is my code

main.cpp


#include <QApplication>
#include "telnetWindow.h"


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
telnetWindow client;
client.show();
return app.exec();
}


telnetWindow.h


#ifndef TELNETWINDOW_H
#define TELNETWINDOW_H

//#include <QProcess>
//#include <QtGui>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QAbstractSocket>

#include "ui_telnet.h"


class telnetWindow : public QWidget,private Ui::telnetWindow
{
Q_OBJECT
public :
telnetWindow(QWidget *parent = 0);
~telnetWindow();

private slots:
void read_data();
void displayError(QAbstractSocket::SocketError socketError);
void enableButton();

private:

QTcpSocket *tcpSocket;
//QTcpSocket tcpSocket2;
// QString currentFortune;
// QString mystring;
// quint16 blockSize;

//void startConsole();
//void exitConsole();

};
#endif


telnetWindow.cpp


#include <QtGui>
#include "telnetWindow.h"
//#include<iostream.h>
//use namespace std;
telnetWindow::telnetWindow(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
tcpSocket = new QTcpSocket(this);

connect(lineEdit_ipAdd, SIGNAL(textChanged(const QString &)),
this, SLOT(enableButton()));
connect(lineEdit_portNo, SIGNAL(textChanged(const QString &)),
this, SLOT(enableButton()));
connect(pushButton_telnet, SIGNAL(clicked()),
this, SLOT(read_data()));
//connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(read_data()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));

//textEdit->append("----------------Console Window-----------------------------");

lineEdit_portNo->setFocus();
}
telnetWindow::~telnetWindow()
{};

void telnetWindow::read_data()
{
QByteArray consoleop;
int ret_val;
pushButton_telnet->setEnabled(true);

tcpSocket->abort();
tcpSocket->connectToHost(lineEdit_ipAdd->text(),
lineEdit_portNo->text().toInt(),QIODevice::ReadWrite);

//tcpSocket->write(data);
if(tcpSocket->waitForConnected(-1))
{
//textEdit->append("Console conncted");
//qdebug() << "Connected";
ret_val = tcpSocket->state();
// cout << ret_val;
if(tcpSocket->waitForReadyRead())
{
tcpSocket->flush();
//cout << tcpSocket->waitForReadyRead();
//while(tcpSocket->canReadLine())
// {
//QString text;
//mystring=tcpSocket->readLine();
consoleop = tcpSocket->readAll();
QString text = textEdit->toPlainText()+QString::fromLocal8Bit(consoleop);
//QString text=tcpSocket->readAll();
//textEdit->append(text);
//cout<<text;
textEdit->append(consoleop);
label->setText(text);
textEdit->insert(consoleop);
label->setText(text);
// qdebug() << text;
// }

pushButton_telnet->setEnabled(false);
}
}
}


I simply have a button , line edit and textedit.

When I try to print the output of the

textEdit->append(consoleop); -- Its printing some garbage value..

I think I need to convert data to some type Or ?
Plz help me solving this .....

wysota
19th February 2009, 22:51
It's hard to say without knowing how the data was sent through the socket but the code you pasted is incorrect. You are waiting for some data to be read from the socket and then you read it once and continue your work. By doing that you expect all data to be available in the socket whereas you might only be able to read one octet. You have to do the reading in a loop until you are sure all data was received. I'm also not sure why you flush the socket before reading.

skumar434
20th February 2009, 14:08
Hi , i was able to resolve this issue ...I refered the Fortune server example . But now I am facing one more issue .

Steps :-

1.I am opening a connection using tcp socket .
2. Wrinting one command on the console .
3. reading the output when readReady is emited.

The probelm I facing is that " What ever data I am reading its not in expected format",
One line is getting divided and returned as 2 line.

For example " My name is Sam " is divide in to line

My
name is Sam

I need to know do I need to convert the string which i read form tcp socket .




Modified code
void telnetWindow::readConsole()
{
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);
QString test;
if( tcpSocket->isSequential()){
cout << "yes";
}
blockSize = tcpSocket->readBufferSize();
QByteArray data = tcpSocket->readAll();
textEdit->append(data);
pushButton_telnet->setEnabled(true);
}

wysota
20th February 2009, 20:36
You are aware of the fact that append() also adds a newline to the data it operates on, right? Also are you sure QDataStream does what you expect it to (you are not using it anywhere, by the way)? I ask again, what is the format of data sent by the remote side?