[Qt-interest] QTcpSocket receives a string
Stefanos Antaris
santaris at csd.auth.gr
Tue Apr 7 08:28:36 CEST 2009
Hello.I have tried to make an app using the fortune client example but
it doesn't work.I don't know if i am connected or not due to the fact
that the errorstring that i have made don't display in a sensorLineEdit
that i have in my ui file.Can anyone check if it is allright?I just want
to receive a string of a non qt application.Thanks in advance.
Sensor.h
[code]
#ifndef SENSORS_H
#define SENSORS_H
#include <QtGui/QDialog>
#include <QtNetwork>
#include "ui_sensors.h"
class QDialogButtonBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QTcpSocket;
class Sensors : public QDialog ,public Ui::SensorsClass
{
Q_OBJECT
public:
Sensors(QWidget *parent = 0);
~Sensors();
void requestSensors();
void readSensors();
void displayError(QAbstractSocket::SocketError socketError);
void enableGetSensorButton();
private:
QTcpSocket* tcpSocket;
Ui::SensorsClass *ui;
QString currentSensor;
quint16 blockSize;
};
#endif // SENSORS_H
[/code]
sensor.cpp
[code]
#include "sensors.h"
#include <QtGui>
#include <QtNetwork>
Sensors::Sensors(QWidget *parent)
: QDialog(parent)
{
setupUi(this);
tcpSocket=new QTcpSocket(this);
connect(connectButton,SIGNAL(clicked()),this,SLOT(requestSensors()));
connect(disconnectButton,SIGNAL(clicked()),this,SLOT(close()));
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readSensors()));
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
}
Sensors::~Sensors()
{
delete ui;
}
void Sensors::requestSensors()
{
sensorLineEdit->setText("request test");
blockSize=0;
tcpSocket->abort();
tcpSocket->connectToHost("192.168.1.150",5444);
}
void Sensors::readSensors()
{
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 nextSensor;
in>>nextSensor;
if(nextSensor==currentSensor){
QTimer::singleShot(0,this,SLOT(requestSensors()));
return;
}
currentSensor=nextSensor;
sensorLineEdit->setText(currentSensor);
}
void Sensors::displayError(QAbstractSocket::SocketError socketError)
{
switch(socketError)
{
case QAbstractSocket::RemoteHostClosedError:{
sensorLineEdit->setText("Remote Host Closed Error");
break;
}
case QAbstractSocket::HostNotFoundError:
sensorLineEdit->setText("Host not found Error");
// QMessageBox::information(this,tr("Sensor Client"),
// tr("The host was not found."));
case QAbstractSocket::ConnectionRefusedError:
sensorLineEdit->setText("Connection Refused Error");
//QMessageBox::information(this,tr("Fortune Client"),tr("The
connection was refused by the peer.Make sure the Sensor Server is
runnung"));
default:
sensorLineEdit->setText("unknown error");\
//QMessageBox::information(this,tr("Sensor Client"),
// tr("The following error
occured:%1.").arg(tcpSocket->errorString()));
}
}
[/code]
Thanks in advance
More information about the Qt-interest-old
mailing list