PDA

View Full Version : Client/Server doesn't work



mattia
31st October 2007, 22:41
Hello, I dont know why this simple server/client doesn't work.
A client send a string to the server and the server has just to show it into the GUI.
I created the ui_client.h and ui_server.h to implement the GUI

CLIENT CODE
client.h


class client : public QMainWindow
{
Q_OBJECT

public:
client();
~client();

private slots:
void connectToServer();
void sendRequest();
void updateTableWidget();
void stopSearch();
void connectionClosedByServer();
void error();

private:
void closeConnection();
QTcpSocket tcpSocket;
quint16 nextBlockSize;
Ui::MainWindowClient ui;
};


client.cpp


#include <QtNetwork>
#include "client.h"

client::client()
{
ui.setupUi(this);
connect(ui.pushButtonConnect, SIGNAL(clicked()),this, SLOT(connectToServer()));

connect(&tcpSocket, SIGNAL(connected()), this, SLOT(sendRequest()));
connect(&tcpSocket, SIGNAL(disconnected()),this, SLOT(connectionClosedByServer()));
connect(&tcpSocket, SIGNAL(readyRead()),this, SLOT(updateTableWidget()));
connect(&tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(error()));
}

client::~client()
{

}


void client::connectToServer()
{
tcpSocket.connectToHost(QHostAddress::LocalHost, 6178);
ui.textBrowserStatus->setHtml("Connecting to server...");
}

void client::sendRequest()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_1);
out << quint16(0) << QString("it's me!");

out.device()->seek(0);
out << quint16(block.size() - sizeof(quint16));
tcpSocket.write(block);

ui.textBrowserStatus->setHtml(tr("Sending request..."));
}

void client::updateTableWidget(){}

void client::stopSearch(){}

void client::connectionClosedByServer()
{
closeConnection();
ui.textBrowserStatus->setHtml("error connectionClosedByServer");
}

void client::error()
{
closeConnection();
ui.textBrowserStatus->setHtml(tcpSocket.errorString()); //-> I recive an "unknow error" on the GUI
}

void client::closeConnection()
{
tcpSocket.close();
}




SERVER CODE
server.h


#include <QtGui/QWidget>
#include <QTcpServer>
#include "ui_server.h"

class server : public QTcpServer, public QMainWindow
{
Q_OBJECT

public:
server();
~server();
private:
void incomingConnection(int socketId);
private:
Ui::MainWindowServer ui;
};


server.cpp


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

server::server()
{
ui.setupUi(this);
ui.textBrowserOut->setHtml("ready....");
}

server::~server()
{

}

void server::incomingConnection(int socketId)
{
ui.textBrowserOut->setHtml(QString::number(socketId, 10)); //->I can't see the socketid in the GUI
ClientSocket *socket = new ClientSocket(ui.textBrowserOut);
socket->setSocketDescriptor(socketId);
}


clientsocket.h


#include <QTcpSocket>
#include <QtGui>

class ClientSocket : public QTcpSocket
{
Q_OBJECT

public:
ClientSocket(QTextBrowser *textBrowser);

private slots:
void readClient();

private:
QTextBrowser *textBrowserPark;
quint16 nextBlockSize;
};


clientsocket.cpp


#include <QtNetwork>
#include <cstdlib>
#include <QtGui>

#include "clientsocket.h"

ClientSocket::ClientSocket(QTextBrowser *textBrowser)
{
this->textBrowserPark = textBrowser;
connect(this, SIGNAL(readyRead()), this, SLOT(readClient()));
connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));

nextBlockSize = 0;
}

void ClientSocket::readClient()
{
this->textBrowserPark->setHtml("....waiting");
QDataStream in(this);
in.setVersion(QDataStream::Qt_4_1);

if (nextBlockSize == 0) {
if (bytesAvailable() < sizeof(quint16))
return;
in >> nextBlockSize;
}

if (bytesAvailable() < nextBlockSize)
return;


QString msg;

in >> msg;
this->textBrowserPark->setHtml(msg);
//i should reply to the client....
close();
}


main.cpp


#include "server.h"

#include <QtGui>
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
server s;
if (!s.listen(QHostAddress::Any, 6178)) {
return 1;
}

server w;
w.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}


Any ideas?

jacek
31st October 2007, 23:11
class server : public QTcpServer, public QMainWindow
This isn't a good idea. Better use two classes --- one for the GUI and one for the communication.


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
server s;
...
server w;
...
}
Why do you create two servers and where's the client?

mattia
1st November 2007, 13:31
Why do you create two servers and where's the client?

you are right...i don't know why i created 2 server object into the main, maybe a cut/paste error, anyway now it works.
I'll implement one class for the GUI and another one for the comunication as you told me before.
Thanks so much!