PDA

View Full Version : receive data from client via QTcpServer



Fallen_
8th September 2010, 00:51
Hi, I made a simple chat server, but can't get it working to receive data from the client (incoming connection)
heres my code:
server.h:

#ifndef SERVER_H
#define SERVER_H

#include <QDialog>

class QTcpServer;
class QLabel;
class QPushButton;
class QTextEdit;

class Server : public QDialog
{
Q_OBJECT
public:
Server(QWidget* parent = 0);

private slots:
void sendWelcomeMessage();

private:
QLabel* status;
QPushButton* quit;
QTcpServer* server;
QTextEdit* read;
};
#endif

server.cpp

#include <QtGui>
#include <QtNetwork>
#include "server.h"

Server::Server(QWidget* parent)
: QDialog(parent)
{
status = new QLabel;
quit = new QPushButton(tr("&Close"));
connect(quit, SIGNAL(clicked()), this, SLOT(close()));
server = new QTcpServer(this);
read = new QTextEdit(tr("Status:\n"));
read->setReadOnly(true);
QHostAddress addr("127.0.0.1");

if(!server->listen(addr, 12345)) {
QMessageBox::information(this, tr("Error!!"), tr("The following errors has occured: %1").arg(server->errorString()));
return;
}

status->setText(tr("The server is running on %1 port.").arg(server->serverPort()));
connect(server, SIGNAL(newConnection()), this, SLOT(sendWelcomeMessage()));

QHBoxLayout* layout = new QHBoxLayout;
layout->addWidget(status);
layout->addWidget(quit);
layout->addWidget(read);
setLayout(layout);
}

void Server::sendWelcomeMessage()
{
typedef QVector<QTcpSocket*> Connections;
Connections connections;
QTcpSocket *client = server->nextPendingConnection();
connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
client->write("Welcome!");
char buffer[100];
sprintf(buffer, "Incoming connection from IP: %s", client->peerAddress().toString().toStdString().c_str());
read->append(buffer);
connections.push_back(client);
for(Connections::Iterator it = connections.begin(); it != connections.end(); ++it)
(*it)->write("Hello!!!");
}

guess no need for main.cpp.
Well, i tried with client->readLine & client->read both didnt work.
@Edit:
also tried with QTextStream and QDataStream didnt work also.

tbscope
8th September 2010, 06:43
You can only read from the socket if there's something to read.
You try to read a nanosecond after you've send the welcom message. In that time, the client didn't have time to respond.

Suggestions:

void Server::sendWelcomeMessage()
{
typedef QVector<QTcpSocket*> Connections; // <-- Define this at the class level
Connections connections; // This too

while (server->hasPendingConnections ()) {
QTcpSocket *client = server->nextPendingConnection();
connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
connect(client, SIGNAL(readyRead()), signalmapper, SLOT(map())); // Signal mapper is defined in the class definition and created in the constructor, see QSignalMapper.
signalmapper->setMapping(client, client);
client->write("Welcome!");
}
connect(signalmapper, SIGNAL(mapped(QObject *)), this, SLOT(clientReadyRead(QObject *)));
}

void Server::clientReadyRead(QObject *socket)
{
QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(socket);

clientSocket->read...()
}

kapoorsudhish
8th September 2010, 07:02
Exactly as mentioned, try to read the data on the readyRead() signal for the socket, which ensures that some data is present on the socket and then try to read.

Fallen_
8th September 2010, 16:18
You can only read from the socket if there's something to read.
You try to read a nanosecond after you've send the welcom message. In that time, the client didn't have time to respond.

Suggestions:

void Server::sendWelcomeMessage()
{
typedef QVector<QTcpSocket*> Connections; // <-- Define this at the class level
Connections connections; // This too

while (server->hasPendingConnections ()) {
QTcpSocket *client = server->nextPendingConnection();
connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
connect(client, SIGNAL(readyRead()), signalmapper, SLOT(map())); // Signal mapper is defined in the class definition and created in the constructor, see QSignalMapper.
signalmapper->setMapping(client, client);
client->write("Welcome!");
}
connect(signalmapper, SIGNAL(mapped(QObject *)), this, SLOT(clientReadyRead(QObject *)));
}

void Server::clientReadyRead(QObject *socket)
{
QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(socket);

clientSocket->read...()
}

i get this error:
signalmapper was not declared in this scope

Fallen_
8th September 2010, 17:08
nvm, i fixed it myself thx