PDA

View Full Version : QtcpSocket readyRead Problem with Java Client



Bernie
17th January 2011, 21:23
Hi!

AT the moment i'm trying to develop a server with Qt and a client in java. But thing don't work as i like. I'm even having troubles receiving a string with my Qt server.
Here is a little bit of code:

The important part of my java client:

public boolean sendString(){

try{
final PrintWriter pw = new PrintWriter(mSocket.getOutputStream(), true);
pw.println("Test String");
return true;
} catch(Exception e){
// to be coded
return false;
}
}

and here my qt part:


[...]
QTcpSocket clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readClient()));
[...]

void Server::readClient(void)
{
ui.clientLabel->setText(tr("entered readClient"));
}



Now, my problem is, that readClient() isn't even called. So I think the SIGNAL(readyRead()) is never executed.
I suspect that I can't send a String like this. But what do I do wrong?

Thanks,
Bernie

tbscope
18th January 2011, 04:08
QTcpSocket clientConnection = tcpServer->nextPendingConnection();

That's not correct.

Bernie
18th January 2011, 09:26
Thanks for your reply. But I made a copy paste mistake. QTcpSocket is a Pointer. (tcpServer as well)


QTcpSocket *clientConnection = tcpServer->nextPendingConnection();

or is nextPendingConnection(); wrong?

wysota
18th January 2011, 14:48
How do you know readClient() is never called? Can you show us the header for your Server class?

Bernie
18th January 2011, 16:31
Here is the Header File:


#include <QtGui/QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include <QNetworkInterface>
#include "ui_server.h"

QT_BEGIN_NAMESPACE
class QLabel;
class QPushButton;
class QTcpServer;
class QNetworkSession;
QT_END_NAMESPACE

class Server : public QMainWindow
{
Q_OBJECT

public:
Server(QWidget *parent = 0, Qt::WFlags flags = 0);
~Server();

private:
Ui::ServerClass ui;

QTcpServer *tcpServer;
QTcpSocket *clientConnection;

private slots:
void doMagic(void);
void sendTestString(void);
void openImage(void);
void startServer(void);
void stopServer(void);
void readClient(void);
};

and the cpp looks like this:


#include <QtGui>
#include <QtNetwork>


#include "server.h"

Server::Server(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);

ui.statusLabel->setText(tr("Server is not running"));

connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(doMagic()));
connect(ui.quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(ui.imageButton, SIGNAL(clicked()), this, SLOT(openImage()));
connect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(startServer()));


}

Server::~Server()
{

}

void Server::doMagic(void)
{
ui.textEdit->append("Blaaa");
}

void Server::startServer(void)
{

tcpServer = new QTcpServer(this);
tcpServer->listen(QHostAddress::Any,8001);
if (!tcpServer->isListening()) {
QMessageBox::critical(this, tr("Server"), tr("Unable to start server: %1.").arg(tcpServer->errorString()));
close();
return;
}

ui.startstopButton->setText(tr("Stop Server"));
ui.statusLabel->setText(tr("The server is running on port %1.\n").arg(tcpServer->serverPort()));

connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendTestString()));
disconnect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(startServer()));
connect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(stopServer()));

}

void Server::stopServer(void)
{
if(tcpServer->isListening()){

tcpServer->close();
}
ui.statusLabel->setText(tr("Server stopped!"));
disconnect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(stopServer()));
connect(ui.startstopButton, SIGNAL(clicked()), this, SLOT(startServer()));
ui.startstopButton->setText(tr("Start Server"));

}

void Server::readClient(void)
{
ui.clientLabel->setText(tr("readClient()"));
}

void Server::sendTestString(void)
{
//ui.clientLabel->setText(tr("String sent"));

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);

out << (quint16)0;
out << QString("Test").toUtf8();
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));

clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readClient()));


clientConnection->write(block);
clientConnection->disconnectFromHost();

}
void Server::openImage(void)
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty()) {
QImage image(fileName);
if (image.isNull()) {
QMessageBox::information(this, tr("Image Viewer"),
tr("Cannot load %1.").arg(fileName));
return;
}
}
}


I know that readClient(/) is never called because otherwise my UI would have shown "readClient()".

wysota
18th January 2011, 17:17
I know that readClient(/) is never called because otherwise my UI would have shown "readClient()".
No, not really. But it is true that readClient() is never called. Right after you establish the connection you call disconnectFromHost() (which in turn deletes the socket) which makes it impossible to receive any data from the client.

Bernie
12th February 2011, 11:59
Ohhh, :eek:
sorry for my late reply. But thank you.
It now works. :)