Skip to content

Commit 0393c96

Browse files
committed
Can send & receive message by broadcasting to all ports
1 parent 0d506ce commit 0393c96

File tree

9 files changed

+247
-6
lines changed

9 files changed

+247
-6
lines changed

go-back-n/proj1-chumengxu.tar.gz

21 MB
Binary file not shown.

go-back-n/r.test

2.92 MB
Binary file not shown.

p2p-networking/Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#############################################################################
22
# Makefile for building: p2papp
3-
# Generated by qmake (2.01a) (Qt 4.8.7) on: Sat Oct 28 16:09:48 2017
3+
# Generated by qmake (2.01a) (Qt 4.8.7) on: Wed Nov 1 13:51:35 2017
44
# Project: p2papp.pro
55
# Template: app
66
# Command: /usr/bin/qmake-qt4 -o Makefile p2papp.pro
@@ -10,7 +10,7 @@
1010

1111
CC = gcc
1212
CXX = g++
13-
DEFINES = -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB
13+
DEFINES = -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED
1414
CFLAGS = -m64 -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)
1515
CXXFLAGS = -m64 -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)
1616
INCPATH = -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtNetwork -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I.
@@ -125,7 +125,10 @@ Makefile: p2papp.pro /usr/share/qt4/mkspecs/linux-g++-64/qmake.conf /usr/share/
125125
/usr/share/qt4/mkspecs/features/uic.prf \
126126
/usr/share/qt4/mkspecs/features/yacc.prf \
127127
/usr/share/qt4/mkspecs/features/lex.prf \
128-
/usr/share/qt4/mkspecs/features/include_source_dir.prf
128+
/usr/share/qt4/mkspecs/features/include_source_dir.prf \
129+
/usr/lib/x86_64-linux-gnu/libQtGui.prl \
130+
/usr/lib/x86_64-linux-gnu/libQtNetwork.prl \
131+
/usr/lib/x86_64-linux-gnu/libQtCore.prl
129132
$(QMAKE) -o Makefile p2papp.pro
130133
/usr/share/qt4/mkspecs/common/unix.conf:
131134
/usr/share/qt4/mkspecs/common/linux.conf:
@@ -151,6 +154,9 @@ Makefile: p2papp.pro /usr/share/qt4/mkspecs/linux-g++-64/qmake.conf /usr/share/
151154
/usr/share/qt4/mkspecs/features/yacc.prf:
152155
/usr/share/qt4/mkspecs/features/lex.prf:
153156
/usr/share/qt4/mkspecs/features/include_source_dir.prf:
157+
/usr/lib/x86_64-linux-gnu/libQtGui.prl:
158+
/usr/lib/x86_64-linux-gnu/libQtNetwork.prl:
159+
/usr/lib/x86_64-linux-gnu/libQtCore.prl:
154160
qmake: FORCE
155161
@$(QMAKE) -o Makefile p2papp.pro
156162

p2p-networking/main.cc

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,23 @@ ChatDialog::ChatDialog()
4040

4141
void ChatDialog::gotReturnPressed()
4242
{
43-
// Initially, just echo the string locally.
44-
// Insert some networking code here...
43+
QByteArray datagram;
44+
QDataStream out(&datagram,QIODevice::ReadWrite);
45+
out << textline->text();
46+
emit msgReadyToSend(datagram);
47+
4548
qDebug() << "FIX: send message to other peers: " << textline->text();
4649
textview->append(textline->text());
4750

4851
// Clear the textline to get ready for the next input message.
4952
textline->clear();
5053
}
5154

55+
void ChatDialog::messageReceived(QString msg){
56+
qDebug() << "FIX: received message from peer: " << msg;
57+
textview->append(msg);
58+
}
59+
5260
NetSocket::NetSocket()
5361
{
5462
// Pick a range of four UDP ports to try to allocate by default,
@@ -60,6 +68,7 @@ NetSocket::NetSocket()
6068
// We use the range from 32768 to 49151 for this purpose.
6169
myPortMin = 32768 + (getuid() % 4096)*4;
6270
myPortMax = myPortMin + 3;
71+
connect(this,SIGNAL(readyRead()),this,SLOT(readPendingDatagrams()));
6372
}
6473

6574
bool NetSocket::bind()
@@ -77,6 +86,27 @@ bool NetSocket::bind()
7786
return false;
7887
}
7988

89+
void NetSocket::readPendingDatagrams(){
90+
while (this->hasPendingDatagrams()){
91+
QByteArray datagram;
92+
datagram.resize(this->pendingDatagramSize());
93+
QHostAddress sender;
94+
quint16 senderPort;
95+
96+
this->readDatagram(datagram.data(),datagram.size(),&sender,&senderPort);
97+
QDataStream in(&datagram,QIODevice::ReadOnly);
98+
QString msg;
99+
in >> msg;
100+
emit datagramReceived(msg);
101+
}
102+
}
103+
104+
void NetSocket::sendDatagram(QByteArray datagram){
105+
for(int p=myPortMin;p<=myPortMax;p++){
106+
writeDatagram(datagram,QHostAddress(QHostAddress::LocalHost),p);
107+
}
108+
}
109+
80110
int main(int argc, char **argv)
81111
{
82112
// Initialize Qt toolkit
@@ -90,7 +120,8 @@ int main(int argc, char **argv)
90120
NetSocket sock;
91121
if (!sock.bind())
92122
exit(1);
93-
123+
QObject::connect(&sock,SIGNAL(datagramReceived(QString)),&dialog,SLOT(messageReceived(QString)));
124+
QObject::connect(&dialog,SIGNAL(msgReadyToSend(QByteArray)),&sock,SLOT(sendDatagram(QByteArray)));
94125
// Enter the Qt main loop; everything else is event driven
95126
return app.exec();
96127
}

p2p-networking/main.hh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ class ChatDialog : public QDialog
1313
public:
1414
ChatDialog();
1515

16+
signals:
17+
void msgReadyToSend(QByteArray datagram);
18+
1619
public slots:
1720
void gotReturnPressed();
21+
void messageReceived(QString msg);
1822

1923
private:
2024
QTextEdit *textview;
@@ -30,6 +34,12 @@ public:
3034

3135
// Bind this socket to a P2Papp-specific default port.
3236
bool bind();
37+
signals:
38+
void datagramReceived(QString msg);
39+
public slots:
40+
void readPendingDatagrams();
41+
void sendDatagram(QByteArray datagram);
42+
3343

3444
private:
3545
int myPortMin, myPortMax;

p2p-networking/main.o

19.3 KB
Binary file not shown.

p2p-networking/moc_main.cpp

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/****************************************************************************
2+
** Meta object code from reading C++ file 'main.hh'
3+
**
4+
** Created by: The Qt Meta Object Compiler version 63 (Qt 4.8.7)
5+
**
6+
** WARNING! All changes made in this file will be lost!
7+
*****************************************************************************/
8+
9+
#include "main.hh"
10+
#if !defined(Q_MOC_OUTPUT_REVISION)
11+
#error "The header file 'main.hh' doesn't include <QObject>."
12+
#elif Q_MOC_OUTPUT_REVISION != 63
13+
#error "This file was generated using the moc from 4.8.7. It"
14+
#error "cannot be used with the include files from this version of Qt."
15+
#error "(The moc has changed too much.)"
16+
#endif
17+
18+
QT_BEGIN_MOC_NAMESPACE
19+
static const uint qt_meta_data_ChatDialog[] = {
20+
21+
// content:
22+
6, // revision
23+
0, // classname
24+
0, 0, // classinfo
25+
3, 14, // methods
26+
0, 0, // properties
27+
0, 0, // enums/sets
28+
0, 0, // constructors
29+
0, // flags
30+
1, // signalCount
31+
32+
// signals: signature, parameters, type, tag, flags
33+
21, 12, 11, 11, 0x05,
34+
35+
// slots: signature, parameters, type, tag, flags
36+
48, 11, 11, 11, 0x0a,
37+
71, 67, 11, 11, 0x0a,
38+
39+
0 // eod
40+
};
41+
42+
static const char qt_meta_stringdata_ChatDialog[] = {
43+
"ChatDialog\0\0datagram\0msgReadyToSend(QByteArray)\0"
44+
"gotReturnPressed()\0msg\0messageReceived(QString)\0"
45+
};
46+
47+
void ChatDialog::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
48+
{
49+
if (_c == QMetaObject::InvokeMetaMethod) {
50+
Q_ASSERT(staticMetaObject.cast(_o));
51+
ChatDialog *_t = static_cast<ChatDialog *>(_o);
52+
switch (_id) {
53+
case 0: _t->msgReadyToSend((*reinterpret_cast< QByteArray(*)>(_a[1]))); break;
54+
case 1: _t->gotReturnPressed(); break;
55+
case 2: _t->messageReceived((*reinterpret_cast< QString(*)>(_a[1]))); break;
56+
default: ;
57+
}
58+
}
59+
}
60+
61+
const QMetaObjectExtraData ChatDialog::staticMetaObjectExtraData = {
62+
0, qt_static_metacall
63+
};
64+
65+
const QMetaObject ChatDialog::staticMetaObject = {
66+
{ &QDialog::staticMetaObject, qt_meta_stringdata_ChatDialog,
67+
qt_meta_data_ChatDialog, &staticMetaObjectExtraData }
68+
};
69+
70+
#ifdef Q_NO_DATA_RELOCATION
71+
const QMetaObject &ChatDialog::getStaticMetaObject() { return staticMetaObject; }
72+
#endif //Q_NO_DATA_RELOCATION
73+
74+
const QMetaObject *ChatDialog::metaObject() const
75+
{
76+
return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
77+
}
78+
79+
void *ChatDialog::qt_metacast(const char *_clname)
80+
{
81+
if (!_clname) return 0;
82+
if (!strcmp(_clname, qt_meta_stringdata_ChatDialog))
83+
return static_cast<void*>(const_cast< ChatDialog*>(this));
84+
return QDialog::qt_metacast(_clname);
85+
}
86+
87+
int ChatDialog::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
88+
{
89+
_id = QDialog::qt_metacall(_c, _id, _a);
90+
if (_id < 0)
91+
return _id;
92+
if (_c == QMetaObject::InvokeMetaMethod) {
93+
if (_id < 3)
94+
qt_static_metacall(this, _c, _id, _a);
95+
_id -= 3;
96+
}
97+
return _id;
98+
}
99+
100+
// SIGNAL 0
101+
void ChatDialog::msgReadyToSend(QByteArray _t1)
102+
{
103+
void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
104+
QMetaObject::activate(this, &staticMetaObject, 0, _a);
105+
}
106+
static const uint qt_meta_data_NetSocket[] = {
107+
108+
// content:
109+
6, // revision
110+
0, // classname
111+
0, 0, // classinfo
112+
3, 14, // methods
113+
0, 0, // properties
114+
0, 0, // enums/sets
115+
0, 0, // constructors
116+
0, // flags
117+
1, // signalCount
118+
119+
// signals: signature, parameters, type, tag, flags
120+
15, 11, 10, 10, 0x05,
121+
122+
// slots: signature, parameters, type, tag, flags
123+
41, 10, 10, 10, 0x0a,
124+
73, 64, 10, 10, 0x0a,
125+
126+
0 // eod
127+
};
128+
129+
static const char qt_meta_stringdata_NetSocket[] = {
130+
"NetSocket\0\0msg\0datagramReceived(QString)\0"
131+
"readPendingDatagrams()\0datagram\0"
132+
"sendDatagram(QByteArray)\0"
133+
};
134+
135+
void NetSocket::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
136+
{
137+
if (_c == QMetaObject::InvokeMetaMethod) {
138+
Q_ASSERT(staticMetaObject.cast(_o));
139+
NetSocket *_t = static_cast<NetSocket *>(_o);
140+
switch (_id) {
141+
case 0: _t->datagramReceived((*reinterpret_cast< QString(*)>(_a[1]))); break;
142+
case 1: _t->readPendingDatagrams(); break;
143+
case 2: _t->sendDatagram((*reinterpret_cast< QByteArray(*)>(_a[1]))); break;
144+
default: ;
145+
}
146+
}
147+
}
148+
149+
const QMetaObjectExtraData NetSocket::staticMetaObjectExtraData = {
150+
0, qt_static_metacall
151+
};
152+
153+
const QMetaObject NetSocket::staticMetaObject = {
154+
{ &QUdpSocket::staticMetaObject, qt_meta_stringdata_NetSocket,
155+
qt_meta_data_NetSocket, &staticMetaObjectExtraData }
156+
};
157+
158+
#ifdef Q_NO_DATA_RELOCATION
159+
const QMetaObject &NetSocket::getStaticMetaObject() { return staticMetaObject; }
160+
#endif //Q_NO_DATA_RELOCATION
161+
162+
const QMetaObject *NetSocket::metaObject() const
163+
{
164+
return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
165+
}
166+
167+
void *NetSocket::qt_metacast(const char *_clname)
168+
{
169+
if (!_clname) return 0;
170+
if (!strcmp(_clname, qt_meta_stringdata_NetSocket))
171+
return static_cast<void*>(const_cast< NetSocket*>(this));
172+
return QUdpSocket::qt_metacast(_clname);
173+
}
174+
175+
int NetSocket::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
176+
{
177+
_id = QUdpSocket::qt_metacall(_c, _id, _a);
178+
if (_id < 0)
179+
return _id;
180+
if (_c == QMetaObject::InvokeMetaMethod) {
181+
if (_id < 3)
182+
qt_static_metacall(this, _c, _id, _a);
183+
_id -= 3;
184+
}
185+
return _id;
186+
}
187+
188+
// SIGNAL 0
189+
void NetSocket::datagramReceived(QString _t1)
190+
{
191+
void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
192+
QMetaObject::activate(this, &staticMetaObject, 0, _a);
193+
}
194+
QT_END_MOC_NAMESPACE

p2p-networking/moc_main.o

19.4 KB
Binary file not shown.

p2p-networking/p2papp

43.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)