From d796c9dd933ab96ec83b9a634feedd5d32e1ba3f Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Tue, 8 Nov 2011 12:31:36 -0600 Subject: Test conversion to TQt3 from Qt3 8c6fc1f8e35fd264dd01c582ca5e7549b32ab731 --- examples/network/clientserver/server/server.cpp | 162 ++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 examples/network/clientserver/server/server.cpp (limited to 'examples/network/clientserver/server/server.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 +#include +#include +#include +#include +#include +#include +#include + +#include + + +/* + 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" -- cgit v1.2.1