PDA

View Full Version : Can't read from socket



marekdor
7th August 2012, 14:45
Hi, I was trying to port simple socket application ffrom pure C to QT4. In C i have all my wanted functionality. The application just should send one byte to Host, and then get the answer from the host.

I'm trying to do it like this :

#include <QCoreApplication>
#include <QtNetwork/QTcpSocket>
#include <QByteArray>



using namespace std;

int main(int argc, char *argv[])
{

char received[2048];

QCoreApplication a(argc, argv);
QTcpSocket sock;

char data[20]="lalalala";

sock.connectToHost("10.0.1.1",23 , QIODevice::ReadWrite);
sock.write(data);
sock.readLine(received, 5);
qDebug() << received ;

sock.disconnectFromHost();
return a.exec();
}



In wireshark i can see that my PC is sending the "lalalala" string to this IP, also in the wireshark i see that host 10.0.1.1 is answering me, but in i have no data in received buffer.

So what i'm doing wrong ?

spirit
7th August 2012, 14:55
You should use QAbstractSocket::waitForBytesWritten and QAbstractSocket::waitForReadyRead for reading/writing data. Also you should wait until a socked is connected using QAbstractSocket::waitForConnected.

Talei
7th August 2012, 17:03
Or, for non blocking implementation, use signals readyRead() - read data, use simply write and stateChanged() to know current socket state.

marekdor
9th August 2012, 08:54
Hello,
OK, so i've done it like this, main class constructor has the connect:


QObject::connect(&socket,SIGNAL(readyRead ()),this,SLOT(receiveData()));

After I am connected to host (wireshark confrm that)

there is still no call of receiveData() slot... ?

yeye_olive
9th August 2012, 10:45
Hello,
OK, so i've done it like this, main class constructor has the connect:


QObject::connect(&socket,SIGNAL(readyRead ()),this,SLOT(receiveData()));

After I am connected to host (wireshark confrm that)

there is still no call of receiveData() slot... ?
The slot will only be called once the event loop is running. You seem to have chosen the asynchronous/non-blocking approach, which should have brought major changes to the structure of you program. Please show the complete code.

I also suggest you study the Fortune client example in Qt's documentation.

marekdor
9th August 2012, 11:01
Ok, here it is :


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QDebug>
#include <QIODevice>
#include <QtNetwork/QTcpSocket>



MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QTcpSocket socket;
ui->disconnectButton->setDisabled(true);
QObject::connect(&socket,SIGNAL(readyRead ()),this,SLOT(receiveData()));

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::connectHandler()
{
int n;
char dataToSend[2]={0x1e};
char received[2048]={0};
socket.connectToHost(ui->ipAddress->displayText(),ui->portNumber->displayText().toInt(), QIODevice::ReadWrite);

if (socket.waitForConnected(1000))
{
qDebug() << "Connecteed! " ;
socket.write(dataToSend);

socket.waitForBytesWritten(3000);
qDebug() << socket.readAll();

qDebug() << received ;

ui->connectButton->setDisabled(true);
ui->disconnectButton->setEnabled(true);
}
}

void MainWindow::disconnectHandler()
{
this->socket.disconnectFromHost();
if (socket.state() == QAbstractSocket::UnconnectedState ||
socket.waitForDisconnected(1000))
{
qDebug("Disconnected!");
ui->disconnectButton->setDisabled(true);
ui->connectButton->setEnabled(true);
}
}

void MainWindow::receiveData()
{

qDebug() << "dew data!!!" ; //this never happen... :(

}

yeye_olive
9th August 2012, 12:06
Firstly, I think you should not mix blocking-style and non-blocking-style operations on the socket. It works, but it is not very elegant. For example, instead of calling waitForConnected(), you could connect the socket's connected() signal to a slot in your main window to which you would move the code writing data to the socket. Also, you do not need to call waitForBytesWritten(): if you simply return to the event loop, the socket will automatically take care of sending the data at the right moment.

In any case this should not prevent the socket from emitting its readyRead() signal. I have several questions for you:
- On line 39 you read all the data from the socket. What happens if you comment this line out?
- Are there any error messages in the application's output relating to QObject::connect()?
- Have you tried connecting to the disconnected() and error() signals of the socket? Some error might be occurring before the socket has any chance of receiving data.