Results 1 to 3 of 3

Thread: Client/Server doesn't work

  1. #1
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Client/Server doesn't work

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Client/Server doesn't work

    Quote Originally Posted by mattia View Post
    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.

    Quote Originally Posted by mattia View Post
    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?

  3. The following user says thank you to jacek for this useful post:

    mattia (1st November 2007)

  4. #3
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Client/Server doesn't work

    Quote Originally Posted by jacek View Post
    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!

Similar Threads

  1. QActions don't work with menubar hidden
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 01:04
  2. Change work area OS
    By pakulo in forum Qt Programming
    Replies: 15
    Last Post: 15th May 2007, 07:20
  3. Replies: 3
    Last Post: 26th September 2006, 12:16
  4. QTextEdit Justify align making work
    By dec0ding in forum Qt Programming
    Replies: 2
    Last Post: 13th January 2006, 12:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.