summaryrefslogtreecommitdiffstats
path: root/examples/network/clientserver/client/client.cpp
blob: 2a8b6b3e0da74d704ee3c803e09adc2c7aac8e0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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 <tqsocket.h>
#include <ntqapplication.h>
#include <ntqvbox.h>
#include <ntqhbox.h>
#include <tqtextview.h>
#include <tqlineedit.h>
#include <tqlabel.h>
#include <tqpushbutton.h>
#include <tqtextstream.h>


class Client : public TQVBox
{
    TQ_OBJECT
public:
    Client( const TQString &host, TQ_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 *quit = new TQPushButton( tr("Quit") , this );

	connect( send, TQ_SIGNAL(clicked()), TQ_SLOT(sendToServer()) );
	connect( close, TQ_SIGNAL(clicked()), TQ_SLOT(closeConnection()) );
	connect( quit, TQ_SIGNAL(clicked()), tqApp, TQ_SLOT(quit()) );

	// create the socket and connect various of its signals
	socket = new TQSocket( this );
	connect( socket, TQ_SIGNAL(connected()),
		TQ_SLOT(socketConnected()) );
	connect( socket, TQ_SIGNAL(connectionClosed()),
		TQ_SLOT(socketConnectionClosed()) );
	connect( socket, TQ_SIGNAL(readyRead()),
		TQ_SLOT(socketReadyRead()) );
	connect( socket, TQ_SIGNAL(error(int)),
		TQ_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, TQ_SIGNAL(delayedCloseFinished()),
		    TQ_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"