Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] what signal and slot for this server?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] what signal and slot for this server?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #1

    I am making a client/server program. in the server program, I need signal and slot for the following code to work correctly but i don't know what signal and slot to use. the ChatterBoxServer::incomingConnection function should be called from the chatterboxserver constructor when a connection has been made.

    @ChatterBoxServer::ChatterBoxServer(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ChatterBoxServer)
    {
    ui->setupUi(this);
    QTcpServer *server = new QTcpServer();
    bool success = server->listen(QHostAddress::Any, 4200);
    if(!success)
    {
    ui->textEdit->append("Could not listen on port 4200.");
    }

    ui->textEdit->append("Ready");
    

    }

    ChatterBoxServer::~ChatterBoxServer()
    {
    delete ui;
    }

    void ChatterBoxServer::incomingConnection(int socketfd)
    {
    QTcpSocket *client = new QTcpSocket(this);
    client->setSocketDescriptor(socketfd);
    clients.insert(client);

    ui->textEdit->append("New client from:"+ client->peerAddress().toString());
    
    connect(client, SIGNAL(readyRead()), this, SLOT(readyRead()));
    connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
    connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
    

    }@

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rahul Das
      wrote on last edited by
      #2

      "This":http://doc.qt.nokia.com/latest/qtcpserver.html#newConnection signal is emitted every time a new connection is available.


      Declaration of (Platform) independence.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalster
        wrote on last edited by
        #3

        its not working for me. what i have is...
        @connect(this, SIGNAL(newConnection()), this, SLOT(incomingConnection()));@

        and i have tried...

        @ connect(client, SIGNAL(newConnection()), this, SLOT(incomingConnection(int socketfd)));@

        both do not work. what am i doing wrong?

        @ChatterBoxServer::ChatterBoxServer(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::ChatterBoxServer)
        {
        ui->setupUi(this);
        QTcpServer *server = new QTcpServer();
        bool success = server->listen(QHostAddress::Any, 4200);
        if(!success)
        {
        ui->textEdit->append("Could not listen on port 4200.");
        }

        ui->textEdit->append("Ready");
        QTcpSocket *client = new QTcpSocket(this);
        client->setSocketDescriptor(socketfd);
        clients.insert(client);
        emit newConnection();
        connect(client, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
        

        }

        ChatterBoxServer::~ChatterBoxServer()
        {
        delete ui;
        }

        void ChatterBoxServer::incomingConnection(int socketfd)
        {
        QTcpSocket *client = new QTcpSocket(this);
        client->setSocketDescriptor(socketfd);
        clients.insert(client);

        ui->textEdit->append("New client from:"+ client->peerAddress().toString());
        
        connect(client, SIGNAL(readyRead()), this, SLOT(readyRead()));
        connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
        connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
        

        }
        @

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rahul Das
          wrote on last edited by
          #4

          newConnection() is emitted by 'server'.

          @connect(client, SIGNAL(newConnection()), this, SLOT(incomingConnection(int socketfd)));
          @ is wrong. You can not pass value like this.


          Declaration of (Platform) independence.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kalster
            wrote on last edited by
            #5

            weil i have tried the following without the value and it still does not work.

            @connect(client, SIGNAL(newConnection()), this, SLOT(incomingConnection()));@

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rahul Das
              wrote on last edited by
              #6

              server is emitting the signal. So you may connect server to the slot :)

              "This":http://thesmithfam.org/blog/2009/07/09/example-qt-chat-program/ could be useful as well.


              Declaration of (Platform) independence.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kalster
                wrote on last edited by
                #7

                i did not understand that sentence Rahul Das, the link you gave is the server program i am using. i am trying to make a ui form for the server instead of the console and i need a working signal for the function.

                this is what i have in the main constructor and it not working.

                @connect(client, SIGNAL(newConnection()), this, SLOT(incomingConnection()));@

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rahul Das
                  wrote on last edited by
                  #8

                  okay, so instead of cmd messages, you want a GUI for server. That is the change?


                  Declaration of (Platform) independence.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kalster
                    wrote on last edited by
                    #9

                    yes, but all i need is to get this signal to work and everything will be ok.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Rahul Das
                      wrote on last edited by
                      #10

                      I have made some changes in it like follows.

                      main
                      .......
                      @#include <QtGui/QApplication>
                      #include "mainwindow.h"

                      int main(int argc, char *argv[])
                      {
                      QApplication a(argc, argv);
                      MainWindow w;
                      w.show();

                      return a.exec&#40;&#41;;
                      

                      }
                      @

                      Ui contain just one label and Main window header and source.
                      .........................................................................
                      @#include <QMainWindow>
                      #include "server.h"

                      namespace Ui {
                      class MainWindow;
                      }

                      class MainWindow : public QMainWindow
                      {
                      Q_OBJECT

                      public:
                      explicit MainWindow(QWidget *parent = 0);
                      ~MainWindow();
                      server * serverd;
                      private:
                      Ui::MainWindow *ui;

                      public slots:
                      void display1(QString);
                      };@

                      @#include "mainwindow.h"
                      #include "ui_mainwindow.h"

                      MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                      {
                      ui->setupUi(this);

                      serverd = new server();
                      bool success = serverd->listen(QHostAddress::Any, 4200);
                      
                      if(!success)
                      {
                          return ;
                      }
                      
                      connect(serverd,SIGNAL(display(QString)),this,SLOT(display1(QString)));
                      

                      }

                      MainWindow::~MainWindow()
                      {
                      delete ui;
                      }

                      void MainWindow::display1(QString str)
                      {
                      ui->label->setText(str);
                      }@
                      ......................................
                      Now our server daemon is here.
                      @#include <QStringList>
                      #include <QTcpServer>
                      #include <QTcpSocket>
                      #include <QMap>
                      #include <QSet>

                      class server : public QTcpServer
                      {
                      Q_OBJECT

                      public:
                          server(QObject *parent=0);
                      
                      private slots:
                          void readyRead();
                          void disconnected();
                          void sendUserList();
                      
                      protected:
                          void incomingConnection(int socketfd);
                      
                      private:
                          QSet<QTcpSocket*> clients;
                          QMap<QTcpSocket*,QString> users;
                      
                      signals :
                          void display(QString);@
                      

                      @#include "server.h"

                      #include <QTcpSocket>
                      #include <QRegExp>

                      server::server(QObject *parent) : QTcpServer(parent)
                      {

                      }

                      void server::incomingConnection(int socketfd)
                      {
                      QTcpSocket *client = new QTcpSocket(this);
                      client->setSocketDescriptor(socketfd);
                      clients.insert(client);

                      qDebug() << "New client from:" << client->peerAddress().toString();
                      
                      connect(client, SIGNAL(readyRead()), this, SLOT(readyRead()));
                      connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
                      

                      }

                      void server::readyRead()
                      {
                      QTcpSocket client = (QTcpSocket)sender();
                      while(client->canReadLine())
                      {
                      QString line = QString::fromUtf8(client->readLine()).trimmed();
                      qDebug() << "Read line:" << line;

                          QRegExp meRegex("^/me:(.*)$");
                      
                          if(meRegex.indexIn(line) != -1)
                          {
                              QString user = meRegex.cap(1);
                              users[client] = user;
                              foreach(QTcpSocket *client, clients)
                                  client->write(QString("Server:" + user + " has joined.\n").toUtf8());
                              sendUserList();
                          }
                          else if(users.contains(client))
                          {
                              QString message = line;
                              QString user = users[client];
                              qDebug() << "User:" << user;
                              qDebug() << "Message:" << message;
                      
                              foreach(QTcpSocket *otherClient, clients)
                                  otherClient->write(QString(user + ":" + message + "\n").toUtf8());
                          }
                          else
                          {
                              qWarning() << "Got bad message from client:" << client->peerAddress().toString() << line;
                          }
                      }
                      

                      }

                      void server::disconnected()
                      {
                      QTcpSocket client = (QTcpSocket)sender();
                      qDebug() << "Client disconnected:" << client->peerAddress().toString();

                      clients.remove(client);
                      
                      QString user = users[client];
                      users.remove(client);
                      
                      sendUserList();
                      foreach(QTcpSocket *client, clients)
                          client->write(QString("Server:" + user + " has left.\n").toUtf8());
                      

                      }

                      void server::sendUserList()
                      {
                      QStringList userList;
                      foreach(QString user, users.values())
                      userList << user;

                      foreach(QTcpSocket *client, clients)
                          client->write(QString("/users:" + userList.join(",") + "\n").toUtf8());
                      emit display(QString("/users:" + userList.join(",") + "\n").toUtf8());
                      

                      }@

                      As you can see, what is done here is, whenever there is something to be displayed, signal is emitted. Mainwindow catches that and displays!

                      use @emit display("Your string")@ whenever you want to display. (change client->write to emit disp...)


                      Declaration of (Platform) independence.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kalster
                        wrote on last edited by
                        #11

                        the code works excellent. thank you :)

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved