[SOLVED] what signal and slot for this server?



  • 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()));
    

    }@



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



  • 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()));
    

    }
    @



  • newConnection() is emitted by 'server'.

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



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

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



  • 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.



  • 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()));@



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



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



  • 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...)



  • the code works excellent. thank you :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.