home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / network / clientserver / server / server.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-11  |  3.0 KB  |  148 lines

  1. /****************************************************************************
  2. ** $Id:  qt/server.cpp   3.0.0   edited Jul 10 10:59 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include <qsocket.h>
  12. #include <qserversocket.h>
  13. #include <qapplication.h>
  14. #include <qvbox.h>
  15. #include <qtextview.h>
  16. #include <qlabel.h>
  17. #include <qpushbutton.h>
  18. #include <qtextstream.h>
  19.  
  20. #include <stdlib.h>
  21.  
  22.  
  23. /*
  24.   The ClientSocket class provides a socket that is connected with a client.
  25.   For every client that connects to the server, the server creates a new
  26.   instance of this class.
  27. */
  28. class ClientSocket : public QSocket
  29. {
  30.     Q_OBJECT
  31. public:
  32.     ClientSocket( int sock, QObject *parent=0, const char *name=0 ) :
  33.     QSocket( parent, name )
  34.     {
  35.     line = 0;
  36.     connect( this, SIGNAL(readyRead()), SLOT(readClient()) );
  37.     connect( this, SIGNAL(connectionClosed()), SLOT(connectionClosed()) );
  38.     setSocket( sock );
  39.     }
  40.  
  41.     ~ClientSocket()
  42.     {
  43.     }
  44.  
  45. private slots:
  46.     void readClient()
  47.     {
  48.     while ( canReadLine() ) {
  49.         QTextStream os( this );
  50.         os << line << ": " << readLine();
  51.         line++;
  52.     }
  53.     }
  54.  
  55.     void connectionClosed()
  56.     {
  57.     delete this;
  58.     }
  59.  
  60. private:
  61.     int line;
  62. };
  63.  
  64.  
  65. /*
  66.   The SimpleServer class handles new connections to the server. For every
  67.   client that connects, it creates a new ClientSocket -- that instance is now
  68.   responsible for the communication with that client.
  69. */
  70. class SimpleServer : public QServerSocket
  71. {
  72.     Q_OBJECT
  73. public:
  74.     SimpleServer( QObject* parent=0 ) :
  75.     QServerSocket( 4242, 1, parent )
  76.     {
  77.     if ( !ok() ) {
  78.         qWarning("Failed to bind to port 4242");
  79.         exit(1);
  80.     }
  81.     }
  82.  
  83.     ~SimpleServer()
  84.     {
  85.     }
  86.  
  87.     void newConnection( int socket )
  88.     {
  89.     (void)new ClientSocket( socket, this );
  90.     emit newConnect();
  91.     }
  92.  
  93. signals:
  94.     void newConnect();
  95. };
  96.  
  97.  
  98. /*
  99.   The ServerInfo class provides a small GUI for the server. It also creates the
  100.   SimpleServer and as a result the server.
  101. */
  102. class ServerInfo : public QVBox
  103. {
  104.     Q_OBJECT
  105. public:
  106.     ServerInfo()
  107.     {
  108.     SimpleServer *server = new SimpleServer( this );
  109.  
  110.     QString itext = QString(
  111.         "This is a small server example.\n"
  112.         "Connect with the client now."
  113.         );
  114.     QLabel *lb = new QLabel( itext, this );
  115.     lb->setAlignment( AlignHCenter );
  116.     infoText = new QTextView( this );
  117.     QPushButton *quit = new QPushButton( "Quit" , this );
  118.  
  119.     connect( server, SIGNAL(newConnect()), SLOT(newConnect()) );
  120.     connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
  121.     }
  122.  
  123.     ~ServerInfo()
  124.     {
  125.     }
  126.  
  127. private slots:
  128.     void newConnect()
  129.     {
  130.     infoText->append( "New connection\n" );
  131.     }
  132.  
  133. private:
  134.     QTextView *infoText;
  135. };
  136.  
  137.  
  138. int main( int argc, char** argv )
  139. {
  140.     QApplication app( argc, argv );
  141.     ServerInfo info;
  142.     app.setMainWidget( &info );
  143.     info.show();
  144.     return app.exec();
  145. }
  146.  
  147. #include "server.moc"
  148.