summaryrefslogtreecommitdiffstats
path: root/examples/network/clientserver
diff options
context:
space:
mode:
authorTimothy Pearson <[email protected]>2011-11-08 12:31:36 -0600
committerTimothy Pearson <[email protected]>2011-11-08 12:31:36 -0600
commitd796c9dd933ab96ec83b9a634feedd5d32e1ba3f (patch)
tree6e3dcca4f77e20ec8966c666aac7c35bd4704053 /examples/network/clientserver
downloadtqt3-d796c9dd933ab96ec83b9a634feedd5d32e1ba3f.tar.gz
tqt3-d796c9dd933ab96ec83b9a634feedd5d32e1ba3f.zip
Test conversion to TQt3 from Qt3 8c6fc1f8e35fd264dd01c582ca5e7549b32ab731
Diffstat (limited to 'examples/network/clientserver')
-rw-r--r--examples/network/clientserver/client/client.cpp125
-rw-r--r--examples/network/clientserver/client/client.pro9
-rw-r--r--examples/network/clientserver/clientserver.doc35
-rw-r--r--examples/network/clientserver/server/server.cpp162
-rw-r--r--examples/network/clientserver/server/server.pro9
5 files changed, 340 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
diff --git a/examples/network/clientserver/clientserver.doc b/examples/network/clientserver/clientserver.doc
new file mode 100644
index 000000000..67b72a5da
--- /dev/null
+++ b/examples/network/clientserver/clientserver.doc
@@ -0,0 +1,35 @@
+/*
+*/
+
+/*! \page clientserver-example.html
+
+ \ingroup network-examples
+
+ \title A small client-server example
+
+ This example shows how two programs can communicate using sockets.
+
+ Two simple example programs are provided, a client program and a
+ server program. Both use the QSocket class, and the server also uses
+ QServerSocket class.
+
+ The server listens on port number 4242 and accepts incoming connections.
+ It sends back every line it receives from the client, 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.
+
+ <hr>
+
+ Implementation server (server.cpp):
+
+ \include network/clientserver/server/server.cpp
+
+ <hr>
+
+ Implementation client (client.cpp):
+
+ \include network/clientserver/client/client.cpp
+*/
diff --git a/examples/network/clientserver/server/server.cpp b/examples/network/clientserver/server/server.cpp
new file mode 100644
index 000000000..f80adda84
--- /dev/null
+++ b/examples/network/clientserver/server/server.cpp
@@ -0,0 +1,162 @@
+/****************************************************************************
+**
+** 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 <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 TQSocket
+{
+ Q_OBJECT
+public:
+ ClientSocket( int sock, TQObject *parent=0, const char *name=0 ) :
+ TQSocket( parent, name )
+ {
+ line = 0;
+ connect( this, SIGNAL(readyRead()),
+ SLOT(readClient()) );
+ connect( this, SIGNAL(connectionClosed()),
+ SLOT(deleteLater()) );
+ setSocket( sock );
+ }
+
+ ~ClientSocket()
+ {
+ }
+
+signals:
+ void logText( const TQString& );
+
+private slots:
+ void readClient()
+ {
+ TQTextStream ts( this );
+ while ( canReadLine() ) {
+ TQString 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 TQServerSocket
+{
+ Q_OBJECT
+public:
+ SimpleServer( TQObject* parent=0 ) :
+ TQServerSocket( 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 TQVBox
+{
+ Q_OBJECT
+public:
+ ServerInfo()
+ {
+ SimpleServer *server = new SimpleServer( this );
+
+ TQString itext = tr(
+ "This is a small server example.\n"
+ "Connect with the client now."
+ );
+ TQLabel *lb = new TQLabel( itext, this );
+ lb->setAlignment( AlignHCenter );
+ infoText = new TQTextView( this );
+ TQPushButton *tquit = new TQPushButton( tr("Quit") , this );
+
+ connect( server, SIGNAL(newConnect(ClientSocket*)),
+ SLOT(newConnect(ClientSocket*)) );
+ connect( tquit, SIGNAL(clicked()), qApp,
+ SLOT(tquit()) );
+ }
+
+ ~ServerInfo()
+ {
+ }
+
+private slots:
+ void newConnect( ClientSocket *s )
+ {
+ infoText->append( tr("New connection\n") );
+ connect( s, SIGNAL(logText(const TQString&)),
+ infoText, SLOT(append(const TQString&)) );
+ connect( s, SIGNAL(connectionClosed()),
+ SLOT(connectionClosed()) );
+ }
+
+ void connectionClosed()
+ {
+ infoText->append( tr("Client closed connection\n") );
+ }
+
+private:
+ TQTextView *infoText;
+};
+
+
+int main( int argc, char** argv )
+{
+ TQApplication app( argc, argv );
+ ServerInfo info;
+ app.setMainWidget( &info );
+ info.show();
+ return app.exec();
+}
+
+#include "server.moc"
diff --git a/examples/network/clientserver/server/server.pro b/examples/network/clientserver/server/server.pro
new file mode 100644
index 000000000..0a845b5fe
--- /dev/null
+++ b/examples/network/clientserver/server/server.pro
@@ -0,0 +1,9 @@
+TEMPLATE = app
+TARGET = server
+
+CONFIG += qt warn_on release
+
+REQUIRES = network full-config
+
+HEADERS =
+SOURCES = server.cpp