crossplatform.ru

Здравствуйте, гость ( Вход | Регистрация )

> Qt3. Сервер с несколькими подключениями., Как разослать всем подключенным клиентам
sibmail
  опции профиля:
сообщение 26.5.2010, 10:32
Сообщение #1


Студент
*

Группа: Участник
Сообщений: 63
Регистрация: 20.3.2009
Пользователь №: 626

Спасибо сказали: 0 раз(а)




Репутация:   0  


берем пример из исходников qt3
Раскрывающийся текст

class ClientSocket : public QSocket
{
    Q_OBJECT
public:
    ClientSocket( int sock, QObject *parent=0, const char *name=0 ) :
    QSocket( parent, name )
    {
    line = 0;
    connect( this, SIGNAL(readyRead()),SLOT(readClient()) );
    connect( this, SIGNAL(connectionClosed()),SLOT(deleteLater()) );
    setSocket( sock );
    }

    ~ClientSocket()
    {
    }

signals:
    void logText( const QString& );

private slots:
    void readClient()
    {
    QTextStream ts( this );
    while ( canReadLine() ) {
        QString str = ts.readLine();
        emit logText( tr("Read: '%1'\n").arg(str) );

        ts << line << ": " << str << endl;
        emit logText( tr("Wrote: '%1: %2'\n").arg(line).arg(str) );

        line++;
    }
    }

private:
    int line;
};


/*
The SimpleServer class handles new connections to the server. For every
client that connects, it creates a new ClientSocket -- that instance is now
responsible for the communication with that client.
*/
class SimpleServer : public QServerSocket
{
    Q_OBJECT
public:
    SimpleServer( QObject* parent=0 ) :
    QServerSocket( 4242, 1, parent )
    {
    if ( !ok() ) {
        qWarning("Failed to bind to port 4242");
        exit(1);
    }
    }

    ~SimpleServer()
    {
    }

    void newConnection( int socket )
    {
    ClientSocket *s = new ClientSocket( socket, this );
    emit newConnect( s );
    }

signals:
    void newConnect( ClientSocket* );
};


/*
The ServerInfo class provides a small GUI for the server. It also creates the
SimpleServer and as a result the server.
*/
class ServerInfo : public QVBox
{
    Q_OBJECT
public:
    ServerInfo()
    {
    SimpleServer *server = new SimpleServer( this );

    QString itext = tr(
        "This is a small server example.\n"
        "Connect with the client now."
        );
    QLabel *lb = new QLabel( itext, this );
    lb->setAlignment( AlignHCenter );
    infoText = new QTextView( this );
    QPushButton *quit = new QPushButton( tr("Quit") , this );

    connect( server, SIGNAL(newConnect(ClientSocket*)),SLOT(newConnect(ClientSocket*)) );
    connect( quit, SIGNAL(clicked()), qApp,SLOT(quit()) );
    }

    ~ServerInfo()
    {
    }

private slots:
    void newConnect( ClientSocket *s )
    {
    infoText->append( tr("New connection\n") );
    connect( s, SIGNAL(logText(const QString&)),infoText, SLOT(append(const QString&)) );
    connect( s, SIGNAL(connectionClosed()),SLOT(connectionClosed()) );
    }

    void connectionClosed()
    {
    infoText->append( tr("Client closed connection\n") );
    }

private:
    QTextView *infoText;
};


int main( int argc, char** argv )
{
    QApplication app( argc, argv );
    ServerInfo info;
    app.setMainWidget( &info );
    info.show();
    return app.exec();
}



здесь получается что для каждого подключенного клиента создается новый класс, в котором и происходит своя работа.

Как в данном сюжете разослать полученно сообщение всем подключенным клиентам?? или подскажите другие способы реализации (Qt 3!!!!)

Сообщение отредактировал Litkevich Yuriy - 26.5.2010, 12:25
Перейти в начало страницы
 
Быстрая цитата+Цитировать сообщение
 
Начать новую тему
Ответов
Litkevich Yuriy
  опции профиля:
сообщение 26.5.2010, 12:26
Сообщение #2


разработчик РЭА
*******

Группа: Сомодератор
Сообщений: 9669
Регистрация: 9.1.2008
Из: Тюмень
Пользователь №: 64

Спасибо сказали: 807 раз(а)




Репутация:   94  


sibmail, по оформлению сообщений читай тему: Справка по кнопкам и тэгам форума
Перейти в начало страницы
 
Быстрая цитата+Цитировать сообщение

Сообщений в этой теме


Быстрый ответОтветить в данную темуНачать новую тему
Теги
Нет тегов для показа


3 чел. читают эту тему (гостей: 3, скрытых пользователей: 0)
Пользователей: 0




RSS Текстовая версия Сейчас: 18.10.2024, 11:36