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
Qt Code:
  1. class client : public QMainWindow
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. client();
  7. ~client();
  8.  
  9. private slots:
  10. void connectToServer();
  11. void sendRequest();
  12. void updateTableWidget();
  13. void stopSearch();
  14. void connectionClosedByServer();
  15. void error();
  16.  
  17. private:
  18. void closeConnection();
  19. QTcpSocket tcpSocket;
  20. quint16 nextBlockSize;
  21. Ui::MainWindowClient ui;
  22. };
To copy to clipboard, switch view to plain text mode 

client.cpp
Qt Code:
  1. #include <QtNetwork>
  2. #include "client.h"
  3.  
  4. client::client()
  5. {
  6. ui.setupUi(this);
  7. connect(ui.pushButtonConnect, SIGNAL(clicked()),this, SLOT(connectToServer()));
  8.  
  9. connect(&tcpSocket, SIGNAL(connected()), this, SLOT(sendRequest()));
  10. connect(&tcpSocket, SIGNAL(disconnected()),this, SLOT(connectionClosedByServer()));
  11. connect(&tcpSocket, SIGNAL(readyRead()),this, SLOT(updateTableWidget()));
  12. connect(&tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(error()));
  13. }
  14.  
  15. client::~client()
  16. {
  17.  
  18. }
  19.  
  20.  
  21. void client::connectToServer()
  22. {
  23. tcpSocket.connectToHost(QHostAddress::LocalHost, 6178);
  24. ui.textBrowserStatus->setHtml("Connecting to server...");
  25. }
  26.  
  27. void client::sendRequest()
  28. {
  29. QByteArray block;
  30. QDataStream out(&block, QIODevice::WriteOnly);
  31. out.setVersion(QDataStream::Qt_4_1);
  32. out << quint16(0) << QString("it's me!");
  33.  
  34. out.device()->seek(0);
  35. out << quint16(block.size() - sizeof(quint16));
  36. tcpSocket.write(block);
  37.  
  38. ui.textBrowserStatus->setHtml(tr("Sending request..."));
  39. }
  40.  
  41. void client::updateTableWidget(){}
  42.  
  43. void client::stopSearch(){}
  44.  
  45. void client::connectionClosedByServer()
  46. {
  47. closeConnection();
  48. ui.textBrowserStatus->setHtml("error connectionClosedByServer");
  49. }
  50.  
  51. void client::error()
  52. {
  53. closeConnection();
  54. ui.textBrowserStatus->setHtml(tcpSocket.errorString()); //-> I recive an "unknow error" on the GUI
  55. }
  56.  
  57. void client::closeConnection()
  58. {
  59. tcpSocket.close();
  60. }
To copy to clipboard, switch view to plain text mode 



SERVER CODE
server.h
Qt Code:
  1. #include <QtGui/QWidget>
  2. #include <QTcpServer>
  3. #include "ui_server.h"
  4.  
  5. class server : public QTcpServer, public QMainWindow
  6. {
  7. Q_OBJECT
  8.  
  9. public:
  10. server();
  11. ~server();
  12. private:
  13. void incomingConnection(int socketId);
  14. private:
  15. Ui::MainWindowServer ui;
  16. };
To copy to clipboard, switch view to plain text mode 

server.cpp
Qt Code:
  1. #include <QtGui>
  2. #include "server.h"
  3. #include "clientsocket.h"
  4.  
  5. server::server()
  6. {
  7. ui.setupUi(this);
  8. ui.textBrowserOut->setHtml("ready....");
  9. }
  10.  
  11. server::~server()
  12. {
  13.  
  14. }
  15.  
  16. void server::incomingConnection(int socketId)
  17. {
  18. ui.textBrowserOut->setHtml(QString::number(socketId, 10)); //->I can't see the socketid in the GUI
  19. ClientSocket *socket = new ClientSocket(ui.textBrowserOut);
  20. socket->setSocketDescriptor(socketId);
  21. }
To copy to clipboard, switch view to plain text mode 

clientsocket.h
Qt Code:
  1. #include <QTcpSocket>
  2. #include <QtGui>
  3.  
  4. class ClientSocket : public QTcpSocket
  5. {
  6. Q_OBJECT
  7.  
  8. public:
  9. ClientSocket(QTextBrowser *textBrowser);
  10.  
  11. private slots:
  12. void readClient();
  13.  
  14. private:
  15. QTextBrowser *textBrowserPark;
  16. quint16 nextBlockSize;
  17. };
To copy to clipboard, switch view to plain text mode 

clientsocket.cpp
Qt Code:
  1. #include <QtNetwork>
  2. #include <cstdlib>
  3. #include <QtGui>
  4.  
  5. #include "clientsocket.h"
  6.  
  7. ClientSocket::ClientSocket(QTextBrowser *textBrowser)
  8. {
  9. this->textBrowserPark = textBrowser;
  10. connect(this, SIGNAL(readyRead()), this, SLOT(readClient()));
  11. connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
  12.  
  13. nextBlockSize = 0;
  14. }
  15.  
  16. void ClientSocket::readClient()
  17. {
  18. this->textBrowserPark->setHtml("....waiting");
  19. QDataStream in(this);
  20. in.setVersion(QDataStream::Qt_4_1);
  21.  
  22. if (nextBlockSize == 0) {
  23. if (bytesAvailable() < sizeof(quint16))
  24. return;
  25. in >> nextBlockSize;
  26. }
  27.  
  28. if (bytesAvailable() < nextBlockSize)
  29. return;
  30.  
  31.  
  32. QString msg;
  33.  
  34. in >> msg;
  35. this->textBrowserPark->setHtml(msg);
  36. //i should reply to the client....
  37. close();
  38. }
To copy to clipboard, switch view to plain text mode 

main.cpp
Qt Code:
  1. #include "server.h"
  2.  
  3. #include <QtGui>
  4. #include <QApplication>
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. QApplication a(argc, argv);
  9. server s;
  10. if (!s.listen(QHostAddress::Any, 6178)) {
  11. return 1;
  12. }
  13.  
  14. server w;
  15. w.show();
  16. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
  17. return a.exec();
  18. }
To copy to clipboard, switch view to plain text mode 

Any ideas?