diff options
Diffstat (limited to 'examples/network/clientserver/client')
-rw-r--r-- | examples/network/clientserver/client/client.cpp | 125 | ||||
-rw-r--r-- | examples/network/clientserver/client/client.pro | 9 |
2 files changed, 134 insertions, 0 deletions
diff --git a/examples/network/clientserver/client/client.cpp b/examples/network/clientserver/client/client.cpp new file mode 100644 index 000000000..d76b59a9d --- /dev/null +++ b/examples/network/clientserver/client/client.cpp @@ -0,0 +1,125 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. 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 TQVBox +{ + Q_OBJECT +public: + Client( const TQString &host, Q_UINT16 port ) + { + // GUI layout + infoText = new TQTextView( this ); + TQHBox *hb = new TQHBox( this ); + inputText = new TQLineEdit( hb ); + TQPushButton *send = new TQPushButton( tr("Send") , hb ); + TQPushButton *close = new TQPushButton( tr("Close connection") , this ); + TQPushButton *tquit = new TQPushButton( tr("Quit") , this ); + + connect( send, SIGNAL(clicked()), SLOT(sendToServer()) ); + connect( close, SIGNAL(clicked()), SLOT(closeConnection()) ); + connect( tquit, SIGNAL(clicked()), qApp, SLOT(tquit()) ); + + // create the socket and connect various of its signals + socket = new TQSocket( 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("Trying to connect to the server\n") ); + socket->connectToHost( host, port ); + } + + ~Client() + { + } + +private slots: + void closeConnection() + { + socket->close(); + if ( socket->state() == TQSocket::Closing ) { + // We have a delayed close. + connect( socket, SIGNAL(delayedCloseFinished()), + SLOT(socketClosed()) ); + } else { + // The socket is closed. + socketClosed(); + } + } + + void sendToServer() + { + // write to the server + TQTextStream 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: + TQSocket *socket; + TQTextView *infoText; + TQLineEdit *inputText; +}; + + +int main( int argc, char** argv ) +{ + TQApplication app( argc, argv ); + Client client( argc<2 ? "localhost" : argv[1], 4242 ); + app.setMainWidget( &client ); + client.show(); + return app.exec(); +} + +#include "client.moc" diff --git a/examples/network/clientserver/client/client.pro b/examples/network/clientserver/client/client.pro new file mode 100644 index 000000000..bc3b9e9ad --- /dev/null +++ b/examples/network/clientserver/client/client.pro @@ -0,0 +1,9 @@ +TEMPLATE = app +TARGET = client + +CONFIG += qt warn_on release + +REQUIRES = network full-config + +HEADERS = +SOURCES = client.cpp |