source: trunk/examples/network/clientserver/client/client.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1/****************************************************************************
2** $Id: client.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 <qapplication.h>
13#include <qvbox.h>
14#include <qhbox.h>
15#include <qtextview.h>
16#include <qlineedit.h>
17#include <qlabel.h>
18#include <qpushbutton.h>
19#include <qtextstream.h>
20
21
22class Client : public QVBox
23{
24 Q_OBJECT
25public:
26 Client( const QString &host, Q_UINT16 port )
27 {
28 // GUI layout
29 infoText = new QTextView( this );
30 QHBox *hb = new QHBox( this );
31 inputText = new QLineEdit( hb );
32 QPushButton *send = new QPushButton( tr("Send") , hb );
33 QPushButton *close = new QPushButton( tr("Close connection") , this );
34 QPushButton *quit = new QPushButton( tr("Quit") , this );
35
36 connect( send, SIGNAL(clicked()), SLOT(sendToServer()) );
37 connect( close, SIGNAL(clicked()), SLOT(closeConnection()) );
38 connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
39
40 // create the socket and connect various of its signals
41 socket = new QSocket( this );
42 connect( socket, SIGNAL(connected()),
43 SLOT(socketConnected()) );
44 connect( socket, SIGNAL(connectionClosed()),
45 SLOT(socketConnectionClosed()) );
46 connect( socket, SIGNAL(readyRead()),
47 SLOT(socketReadyRead()) );
48 connect( socket, SIGNAL(error(int)),
49 SLOT(socketError(int)) );
50
51 // connect to the server
52 infoText->append( tr("Trying to connect to the server\n") );
53 socket->connectToHost( host, port );
54 }
55
56 ~Client()
57 {
58 }
59
60private slots:
61 void closeConnection()
62 {
63 socket->close();
64 if ( socket->state() == QSocket::Closing ) {
65 // We have a delayed close.
66 connect( socket, SIGNAL(delayedCloseFinished()),
67 SLOT(socketClosed()) );
68 } else {
69 // The socket is closed.
70 socketClosed();
71 }
72 }
73
74 void sendToServer()
75 {
76 // write to the server
77 QTextStream os(socket);
78 os << inputText->text() << "\n";
79 inputText->setText( "" );
80 }
81
82 void socketReadyRead()
83 {
84 // read from the server
85 while ( socket->canReadLine() ) {
86 infoText->append( socket->readLine() );
87 }
88 }
89
90 void socketConnected()
91 {
92 infoText->append( tr("Connected to server\n") );
93 }
94
95 void socketConnectionClosed()
96 {
97 infoText->append( tr("Connection closed by the server\n") );
98 }
99
100 void socketClosed()
101 {
102 infoText->append( tr("Connection closed\n") );
103 }
104
105 void socketError( int e )
106 {
107 infoText->append( tr("Error number %1 occurred\n").arg(e) );
108 }
109
110private:
111 QSocket *socket;
112 QTextView *infoText;
113 QLineEdit *inputText;
114};
115
116
117int main( int argc, char** argv )
118{
119 QApplication app( argc, argv );
120 Client client( argc<2 ? "localhost" : argv[1], 4242 );
121 app.setMainWidget( &client );
122 client.show();
123 return app.exec();
124}
125
126#include "client.moc"
Note: See TracBrowser for help on using the repository browser.