[SOLVED] QTCPSocket receives only 0 byte long packet
-
Hi all,
I hope that anybody of you can help me with the problem which is named in the topic.
First, what I want to do:
I'm currently writing a chatsystem for my VPN (I know I could use out of the box software, but I want to do it by my self ;) ).
The Serverapplication is written in standard C++, finished and tested with a rudimentaly small commandline-client on Linux and Windows.Now I want to write a graphical Clientapplication, which I want to write in Qt.
For the first tests, I have written a small GUI-Application with the help of the Fortune-Example.
The connection to the server works. Also the disconnection.
The signalling, for readyRead() works also.Now my problem:
When I'm receiving data from the server, I want to print this data out via the qDebug() functionality (how I said, it's the first test).
But when I'm receiving data, the QString in which I am storing the data is empty. It has the length 0 and contains no data.
I have also tried to send data from another client-application to the qt-client over the server, but with the same result.
I have also tried to create the connection via a Pushbutton (because of the thread https://qt-project.org/forums/viewthread/12481).In the method void MainWindow::readFromSocket(), is the code implemented with which I'm trying to receive the data.
This method I have copied from the Fortune-Example.Maybe someone of you knows, what I'm doing wrong.
Here is the code of the Header-file:
@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QtGui>
#include <QtNetwork>#include "ui_mainwindow.h"
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void readFromSocket();
void socketConnected();
void socketDisconnected();
void connectToServer();
void displayError(QAbstractSocket::SocketError socketError);protected:
void changeEvent(QEvent *e);Ui::MainWindow *m_ui; QTcpSocket m_Socket;
};
#endif // MAINWINDOW_H
@And here is the code of the cpp-File:
@
#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
m_ui(new Ui::MainWindow)
{
m_ui->setupUi(this);
m_testClipboardCopy.setIP("10.8.0.2");
m_testClipboardCopy.setName("Jan");
connect(m_ui->ConnectButton,SIGNAL(clicked()),this,SLOT(connectToServer()));
connect(&m_Socket,SIGNAL(readyRead()), this, SLOT(readFromSocket()));
connect(&m_Socket,SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(&m_Socket,SIGNAL(connected()), this, SLOT(socketConnected()));
connect(&m_Socket,SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
}MainWindow::~MainWindow()
{
m_Socket.close();
delete m_ui;
}void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
m_ui->retranslateUi(this);
break;
default:
break;
}
}void MainWindow::readFromSocket(){
QString qsInput="";QDataStream in(&m_Socket); in.setVersion(QDataStream::Qt_4_0); in>>qsInput; qDebug()<<"Read bytes >>"<<qsInput.length()<<"<< and the message >>"<<qsInput<<"<<\n";
}
void MainWindow::connectToServer(){
m_Socket.abort();
m_Socket.connectToHost("10.8.0.1", 7000);
qDebug()<<"Connected via socket >>"<<m_Socket.socketDescriptor()<<"<<\n";
}void MainWindow::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
QMessageBox::information(this, tr("Fortune Client"),
tr("The following error occurred: %1.")
.arg(m_Socket.errorString()));
}}
void MainWindow::socketConnected(){
qDebug()<<"Connected to the Server\n";
}void MainWindow::socketDisconnected(){
qDebug()<<"Disconnected to the Server\n";
}
@The screenoutput, after the method readFromSocket() is called, is the following:
@
Read bytes >> 0 << and the message >> "" <<
@I hope someone of you can help me.
Regards
Jan[EDIT] Writing mistake in the topic :D
-
You are using QDataStream which implies that the sender is also using QDataStream with exactly the same version setting. If this is not the case then you will have difficulty even if you get past the next hurdle.
You are assuming that data sent in one write is received in one read: is almost never the case for practical purposes. Consequently you are feeding QDataStream an incomplete stream the first time the routine is called, and a stream partway through on subsequent calls. You need to buffer the received data until a "complete" message is present and then try to process it. How you determine "complete" is up to your protocol, but often involves preceding the message with a 2- or 4-byte byte count.
-
Ok thanks for your reply,
it works now.
Because of your comment I have searched for an alternative and I have changed the code of the method
to this.
And now it works also with a tcp server which is written in standard C/C++.@
void MainWindow::readFromSocket(){
qint64 iSize=4096;
QByteArray sInput;sInput=m_Socket.read(iSize); qDebug()<<"Read bytes >>"<<sInput.size()<<"<< and the message >>"<<sInput<<"<<\n";
}
@Regards
Jan