0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
#include <QtNetwork>
|
|
42 |
|
|
43 |
class ClientServer : public QUdpSocket
|
|
44 |
{
|
|
45 |
Q_OBJECT
|
|
46 |
public:
|
|
47 |
enum Type {
|
|
48 |
ConnectedClient,
|
|
49 |
UnconnectedClient,
|
|
50 |
Server
|
|
51 |
};
|
|
52 |
|
|
53 |
ClientServer(Type type, const QString &host, quint16 port)
|
|
54 |
: type(type)
|
|
55 |
{
|
|
56 |
switch (type) {
|
|
57 |
case Server:
|
|
58 |
if (bind(0, ShareAddress | ReuseAddressHint)) {
|
|
59 |
printf("%d\n", localPort());
|
|
60 |
} else {
|
|
61 |
printf("XXX\n");
|
|
62 |
}
|
|
63 |
break;
|
|
64 |
case ConnectedClient:
|
|
65 |
connectToHost(host, port);
|
|
66 |
startTimer(250);
|
|
67 |
printf("ok\n");
|
|
68 |
break;
|
|
69 |
case UnconnectedClient:
|
|
70 |
peerAddress = host;
|
|
71 |
peerPort = port;
|
|
72 |
if (bind(QHostAddress::Any, port + 1, ShareAddress | ReuseAddressHint)) {
|
|
73 |
startTimer(250);
|
|
74 |
printf("ok\n");
|
|
75 |
} else {
|
|
76 |
printf("XXX\n");
|
|
77 |
}
|
|
78 |
break;
|
|
79 |
}
|
|
80 |
fflush(stdout);
|
|
81 |
|
|
82 |
connect(this, SIGNAL(readyRead()), this, SLOT(readData()));
|
|
83 |
}
|
|
84 |
|
|
85 |
protected:
|
|
86 |
void timerEvent(QTimerEvent *event)
|
|
87 |
{
|
|
88 |
static int n = 0;
|
|
89 |
switch (type) {
|
|
90 |
case ConnectedClient:
|
|
91 |
write(QByteArray::number(n++));
|
|
92 |
break;
|
|
93 |
case UnconnectedClient:
|
|
94 |
writeDatagram(QByteArray::number(n++), peerAddress, peerPort);
|
|
95 |
break;
|
|
96 |
default:
|
|
97 |
break;
|
|
98 |
}
|
|
99 |
|
|
100 |
QUdpSocket::timerEvent(event);
|
|
101 |
}
|
|
102 |
|
|
103 |
private slots:
|
|
104 |
void readData()
|
|
105 |
{
|
|
106 |
printf("readData()\n");
|
|
107 |
switch (type) {
|
|
108 |
case ConnectedClient: {
|
|
109 |
while (bytesAvailable() || hasPendingDatagrams()) {
|
|
110 |
QByteArray data = readAll();
|
|
111 |
printf("got %d\n", data.toInt());
|
|
112 |
}
|
|
113 |
break;
|
|
114 |
}
|
|
115 |
case UnconnectedClient: {
|
|
116 |
while (hasPendingDatagrams()) {
|
|
117 |
QByteArray data;
|
|
118 |
data.resize(pendingDatagramSize());
|
|
119 |
readDatagram(data.data(), data.size());
|
|
120 |
printf("got %d\n", data.toInt());
|
|
121 |
}
|
|
122 |
break;
|
|
123 |
}
|
|
124 |
case Server: {
|
|
125 |
while (hasPendingDatagrams()) {
|
|
126 |
QHostAddress sender;
|
|
127 |
quint16 senderPort;
|
|
128 |
QByteArray data;
|
|
129 |
data.resize(pendingDatagramSize());
|
|
130 |
readDatagram(data.data(), data.size(), &sender, &senderPort);
|
|
131 |
printf("got %d\n", data.toInt());
|
|
132 |
printf("sending %d\n", data.toInt() * 2);
|
|
133 |
writeDatagram(QByteArray::number(data.toInt() * 2), sender, senderPort);
|
|
134 |
}
|
|
135 |
break;
|
|
136 |
}
|
|
137 |
}
|
|
138 |
fflush(stdout);
|
|
139 |
}
|
|
140 |
|
|
141 |
private:
|
|
142 |
Type type;
|
|
143 |
QHostAddress peerAddress;
|
|
144 |
quint16 peerPort;
|
|
145 |
};
|
|
146 |
|
|
147 |
int main(int argc, char **argv)
|
|
148 |
{
|
|
149 |
QCoreApplication app(argc, argv);
|
|
150 |
ClientServer::Type type;
|
|
151 |
if (app.arguments().size() < 4) {
|
|
152 |
qDebug("usage: ./%s [ConnectedClient <server> <port>|UnconnectedClient <server> <port>|Server]", argv[0]);
|
|
153 |
return 1;
|
|
154 |
}
|
|
155 |
|
|
156 |
QString arg = app.arguments().at(1).trimmed().toLower();
|
|
157 |
if (arg == "connectedclient")
|
|
158 |
type = ClientServer::ConnectedClient;
|
|
159 |
else if (arg == "unconnectedclient")
|
|
160 |
type = ClientServer::UnconnectedClient;
|
|
161 |
else if (arg == "server")
|
|
162 |
type = ClientServer::Server;
|
|
163 |
|
|
164 |
ClientServer clientServer(type, app.arguments().at(2),
|
|
165 |
app.arguments().at(3).toInt());
|
|
166 |
|
|
167 |
return app.exec();
|
|
168 |
}
|
|
169 |
|
|
170 |
#include "main.moc"
|