1 | /****************************************************************************
|
---|
2 | ** $Id: server.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
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()),
|
---|
37 | SLOT(readClient()) );
|
---|
38 | connect( this, SIGNAL(connectionClosed()),
|
---|
39 | SLOT(deleteLater()) );
|
---|
40 | setSocket( sock );
|
---|
41 | }
|
---|
42 |
|
---|
43 | ~ClientSocket()
|
---|
44 | {
|
---|
45 | }
|
---|
46 |
|
---|
47 | signals:
|
---|
48 | void logText( const QString& );
|
---|
49 |
|
---|
50 | private slots:
|
---|
51 | void readClient()
|
---|
52 | {
|
---|
53 | QTextStream ts( this );
|
---|
54 | while ( canReadLine() ) {
|
---|
55 | QString str = ts.readLine();
|
---|
56 | emit logText( tr("Read: '%1'\n").arg(str) );
|
---|
57 |
|
---|
58 | ts << line << ": " << str << endl;
|
---|
59 | emit logText( tr("Wrote: '%1: %2'\n").arg(line).arg(str) );
|
---|
60 |
|
---|
61 | line++;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | private:
|
---|
66 | int line;
|
---|
67 | };
|
---|
68 |
|
---|
69 |
|
---|
70 | /*
|
---|
71 | The SimpleServer class handles new connections to the server. For every
|
---|
72 | client that connects, it creates a new ClientSocket -- that instance is now
|
---|
73 | responsible for the communication with that client.
|
---|
74 | */
|
---|
75 | class SimpleServer : public QServerSocket
|
---|
76 | {
|
---|
77 | Q_OBJECT
|
---|
78 | public:
|
---|
79 | SimpleServer( QObject* parent=0 ) :
|
---|
80 | QServerSocket( 4242, 1, parent )
|
---|
81 | {
|
---|
82 | if ( !ok() ) {
|
---|
83 | qWarning("Failed to bind to port 4242");
|
---|
84 | exit(1);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | ~SimpleServer()
|
---|
89 | {
|
---|
90 | }
|
---|
91 |
|
---|
92 | void newConnection( int socket )
|
---|
93 | {
|
---|
94 | ClientSocket *s = new ClientSocket( socket, this );
|
---|
95 | emit newConnect( s );
|
---|
96 | }
|
---|
97 |
|
---|
98 | signals:
|
---|
99 | void newConnect( ClientSocket* );
|
---|
100 | };
|
---|
101 |
|
---|
102 |
|
---|
103 | /*
|
---|
104 | The ServerInfo class provides a small GUI for the server. It also creates the
|
---|
105 | SimpleServer and as a result the server.
|
---|
106 | */
|
---|
107 | class ServerInfo : public QVBox
|
---|
108 | {
|
---|
109 | Q_OBJECT
|
---|
110 | public:
|
---|
111 | ServerInfo()
|
---|
112 | {
|
---|
113 | SimpleServer *server = new SimpleServer( this );
|
---|
114 |
|
---|
115 | QString itext = tr(
|
---|
116 | "This is a small server example.\n"
|
---|
117 | "Connect with the client now."
|
---|
118 | );
|
---|
119 | QLabel *lb = new QLabel( itext, this );
|
---|
120 | lb->setAlignment( AlignHCenter );
|
---|
121 | infoText = new QTextView( this );
|
---|
122 | QPushButton *quit = new QPushButton( tr("Quit") , this );
|
---|
123 |
|
---|
124 | connect( server, SIGNAL(newConnect(ClientSocket*)),
|
---|
125 | SLOT(newConnect(ClientSocket*)) );
|
---|
126 | connect( quit, SIGNAL(clicked()), qApp,
|
---|
127 | SLOT(quit()) );
|
---|
128 | }
|
---|
129 |
|
---|
130 | ~ServerInfo()
|
---|
131 | {
|
---|
132 | }
|
---|
133 |
|
---|
134 | private slots:
|
---|
135 | void newConnect( ClientSocket *s )
|
---|
136 | {
|
---|
137 | infoText->append( tr("New connection\n") );
|
---|
138 | connect( s, SIGNAL(logText(const QString&)),
|
---|
139 | infoText, SLOT(append(const QString&)) );
|
---|
140 | connect( s, SIGNAL(connectionClosed()),
|
---|
141 | SLOT(connectionClosed()) );
|
---|
142 | }
|
---|
143 |
|
---|
144 | void connectionClosed()
|
---|
145 | {
|
---|
146 | infoText->append( tr("Client closed connection\n") );
|
---|
147 | }
|
---|
148 |
|
---|
149 | private:
|
---|
150 | QTextView *infoText;
|
---|
151 | };
|
---|
152 |
|
---|
153 |
|
---|
154 | int main( int argc, char** argv )
|
---|
155 | {
|
---|
156 | QApplication app( argc, argv );
|
---|
157 | ServerInfo info;
|
---|
158 | app.setMainWidget( &info );
|
---|
159 | info.show();
|
---|
160 | return app.exec();
|
---|
161 | }
|
---|
162 |
|
---|
163 | #include "server.moc"
|
---|