Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# A small client-server example

This example shows how two programs can communicate using sockets. It consists out of two different programs: a server and a client.

The server listens on port number 4242 and accepts incoming connections. It sends back every line he receives from the client. The line is prepended with the line number.

The client tries to connect to the server on the host specified on the command line or to localhost if no command line arguments are specified. You can send single lines to the server.

Implementation server (server.cpp):

    /****************************************************************************
    ** $Id:  qt/server.cpp   3.0.0   edited Jul 10 10:59 $
    **
    ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
    **
    ** This file is part of an example program for Qt.  This example
    ** program may be used, distributed and modified without limitation.
    **
    *****************************************************************************/

    #include <qsocket.h>
    #include <qserversocket.h>
    #include <qapplication.h>
    #include <qvbox.h>
    #include <qtextview.h>
    #include <qlabel.h>
    #include <qpushbutton.h>
    #include <qtextstream.h>

    #include <stdlib.h>

    /*
      The ClientSocket class provides a socket that is connected with a client.
      For every client that connects to the server, the server creates a new
      instance of this class.
    */
    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(connectionClosed()) );
            setSocket( sock );
        }

        ~ClientSocket()
        {
        }

    private slots:
        void readClient()
        {
            while ( canReadLine() ) {
                QTextStream os( this );
                os << line << ": " << readLine();
                line++;
            }
        }

        void connectionClosed()
        {
            delete this;
        }

    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 )
        {
            (void)new ClientSocket( socket, this );
            emit newConnect();
        }

    signals:
        void newConnect();
    };

    /*
      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 = QString(
                    "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( "Quit" , this );

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

        ~ServerInfo()
        {
        }

    private slots:
        void newConnect()
        {
            infoText->append( "New 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();
    }

    #include "server.moc"

Implementation client (client.cpp):

    /****************************************************************************
    ** $Id:  qt/client.cpp   3.0.0   edited Jun 1 18:44 $
    **
    ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
    **
    ** This file is part of an example program for Qt.  This example
    ** program may be used, distributed and modified without limitation.
    **
    *****************************************************************************/

    #include <qsocket.h>
    #include <qapplication.h>
    #include <qvbox.h>
    #include <qhbox.h>
    #include <qtextview.h>
    #include <qlineedit.h>
    #include <qlabel.h>
    #include <qpushbutton.h>
    #include <qtextstream.h>

    class Client : public QVBox
    {
        Q_OBJECT
    public:
        Client( const QString &host, Q_UINT16 port )
        {
            // GUI layout
            infoText = new QTextView( this );
            QHBox *hb = new QHBox( this );
            inputText = new QLineEdit( hb );
            QPushButton *send = new QPushButton( tr("Send") , hb );
            QPushButton *close = new QPushButton( tr("Close connection") , this );
            QPushButton *quit = new QPushButton( tr("Quit") , this );

            connect( send, SIGNAL(clicked()), SLOT(sendToServer()) );
            connect( close, SIGNAL(clicked()), SLOT(closeConnection()) );
            connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );

            // create the socket and connect various of its signals
            socket = new QSocket( this );
            connect( socket, SIGNAL(connected()),
                    SLOT(socketConnected()) );
            connect( socket, SIGNAL(connectionClosed()),
                    SLOT(socketConnectionClosed()) );
            connect( socket, SIGNAL(readyRead()),
                    SLOT(socketReadyRead()) );
            connect( socket, SIGNAL(error(int)),
                    SLOT(socketError(int)) );

            // connect to the server
            infoText->append( tr("Try to connect to server\n") );
            socket->connectToHost( host, port );
        }

        ~Client()
        {
        }

    private slots:
        void closeConnection()
        {
            socket->close();
            if ( socket->state() == QSocket::Closing ) {
                // We have a delayed close.
                connect( socket, SIGNAL(delayedCloseFinished()),
                        SLOT(socketClosed()) );
            } else {
                // The socket is closed.
                socketClosed();
            }
        }

        void sendToServer()
        {
            // write to the server
            QTextStream os(socket);
            os << inputText->text() << "\n";
            inputText->setText( "" );
        }

        void socketReadyRead()
        {
            // read from the server
            while ( socket->canReadLine() ) {
                infoText->append( socket->readLine() );
            }
        }

        void socketConnected()
        {
            infoText->append( tr("Connected to server\n") );
        }

        void socketConnectionClosed()
        {
            infoText->append( tr("Connection closed by the server\n") );
        }

        void socketClosed()
        {
            infoText->append( tr("Connection closed\n") );
        }

        void socketError( int e )
        {
            infoText->append( tr("Error number %1 occurred\n").arg(e) );
        }

    private:
        QSocket *socket;
        QTextView *infoText;
        QLineEdit *inputText;
    };

    int main( int argc, char** argv )
    {
        QApplication app( argc, argv );
        Client client( argc<2 ? "localhost" : argv[1], 4242 );
        app.setMainWidget( &client );
        client.show();
        return app.exec();
    }

    #include "client.moc"

See also Network Examples.