PDA

View Full Version : Server is not reading from client



probine
30th November 2006, 14:21
I have this very simple server and I would like it to be able to read any data from a client.

the .h file
**********************************

#ifndef CLIENTSERVER_H
#define CLIENTSERVER_H

#include <QMainWindow>

class QTextBrowser;
class QByteArray;
class QTcpSocket;
class QTcpServer;


class ClientServer : public QMainWindow
{
Q_OBJECT
private:
QTextBrowser * textBrowser;
QByteArray * byteArray;
QTcpSocket * tcpSocket;
QTcpServer * tcpServer;

public:
ClientServer();

signals:

public slots:
void readData();

};

# endif
***************************************


the .cpp file
***************************************

#include <QTextBrowser>
#include <QByteArray>
#include <QTcpSocket>
#include <QTcpServer>
#include "clientserver.h"

ClientServer::ClientServer()
{
textBrowser = new QTextBrowser(this);
tcpServer = new QTcpServer();
tcpSocket = new QTcpSocket();
setCentralWidget(textBrowser);
setFixedSize(500,500);
show();

if(tcpServer->listen(QHostAddress("localhost"), quint16(33333)))
textBrowser->append("ClientServer is listening");

tcpSocket->setSocketDescriptor(tcpServer->socketDescriptor());
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));

}

void ClientServer::readData()
{
textBrowser->append("Ready to read");
}
************************************************** ***

I cannot even get to the readData() function.

Why?
How can I make it read from a client?

jpn
30th November 2006, 14:27
You should connect to signal QTcpServer::newConnection() and then use QTcpServer::nextPendingConnection() to get the socket whose signal QTcpSocket::readyRead() you are interested of. See the examples shipped with Qt or the Simple Chat (http://wiki.qtcentre.org/index.php?title=Simple_Chat) example in our wiki.

probine
30th November 2006, 15:00
I read now from the client (browser), but I cannot write to the browser. When I write to a telnet window, then the message is shown there, but the browser does not display the html message.

How can I solve it ?

probine
30th November 2006, 16:13
I don't want to make my code to complex...

I just want to know:

1. How can the server know that the browser has finished sending all its data ?

jpn
30th November 2006, 16:45
You don't know unless you provide some kind of "end-of-message" mark from the other side. Read this thread (http://www.qtcentre.org/forum/f-qt-programming-2/t-qt-socket-question-3456.html).