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
{
Q_OBJECT
public:
client();
~client();
private slots:
void connectToServer();
void sendRequest();
void updateTableWidget();
void stopSearch();
void connectionClosedByServer();
void error();
private:
void closeConnection();
quint16 nextBlockSize;
Ui::MainWindowClient ui;
};
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;
};
To copy to clipboard, switch view to plain text mode
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()
{
ui.textBrowserStatus->setHtml("Connecting to server...");
}
void client::sendRequest()
{
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
SERVER CODE
server.h
#include <QtGui/QWidget>
#include <QTcpServer>
#include "ui_server.h"
{
Q_OBJECT
public:
server();
~server();
private:
void incomingConnection(int socketId);
private:
Ui::MainWindowServer ui;
};
#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;
};
To copy to clipboard, switch view to plain text mode
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);
}
#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);
}
To copy to clipboard, switch view to plain text mode
clientsocket.h
#include <QTcpSocket>
#include <QtGui>
{
Q_OBJECT
public:
private slots:
void readClient();
private:
quint16 nextBlockSize;
};
#include <QTcpSocket>
#include <QtGui>
class ClientSocket : public QTcpSocket
{
Q_OBJECT
public:
ClientSocket(QTextBrowser *textBrowser);
private slots:
void readClient();
private:
QTextBrowser *textBrowserPark;
quint16 nextBlockSize;
};
To copy to clipboard, switch view to plain text mode
clientsocket.cpp
#include <QtNetwork>
#include <cstdlib>
#include <QtGui>
#include "clientsocket.h"
{
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");
if (nextBlockSize == 0) {
if (bytesAvailable() < sizeof(quint16))
return;
in >> nextBlockSize;
}
if (bytesAvailable() < nextBlockSize)
return;
in >> msg;
this->textBrowserPark->setHtml(msg);
//i should reply to the client....
close();
}
#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();
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "server.h"
#include <QtGui>
#include <QApplication>
int main(int argc, char *argv[])
{
server s;
return 1;
}
server w;
w.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Any ideas?
Bookmarks