-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyudp.cpp
More file actions
92 lines (76 loc) · 2.66 KB
/
Copy pathmyudp.cpp
File metadata and controls
92 lines (76 loc) · 2.66 KB
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
/*
* myudp.cpp: UDP socket
*
* Copyright (C) 2017 Zhengyu Peng, https://zpeng.me
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "myudp.h"
#include<QEventLoop>
#include<QTimer>
MyUDP::MyUDP(QObject *parent) : QUdpSocket(parent)
{
socket = new QUdpSocket();
}
bool MyUDP::bindPort(QHostAddress addr, quint16 port)
{
socket->abort();
bool isBinded = socket->bind(addr, port);
qDebug()<< "bindPort :" << addr<<port<<isBinded ;
qDebug()<<socket->errorString();//此处为错误打印
if (isBinded)
{
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()),Qt::DirectConnection);
}
return isBinded;
}
void MyUDP::sendMessage(QHostAddress sender, quint16 senderPort, QByteArray Data)
{
//qDebug()<<"me -->senderPort"<<senderPort;
// Sends the datagram datagram
// to the host address and at port.
socket->writeDatagram(Data, sender, senderPort);
}
void MyUDP::readyRead()
{
// when data comes in
QByteArray buffer;
buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
// Receives a datagram no larger than maxSize bytes and stores it in data.
// The sender's host address and port is stored in *address and *port
// (unless the pointers are 0).
socket->readDatagram(buffer.data(), buffer.size(),
&sender, &senderPort);
// qDebug()<<" senderPort --> me"<<sender.toString()<<":"<< senderPort<<"-->"<<socket->localPort();
emit newMessage(sender.toString(), buffer);
}
void MyUDP::unbindPort()
{
disconnect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
socket->disconnectFromHost();
socket->close();
}
bool MyUDP::waitForReadyRead(int milliseconds)
{
QEventLoop eventLoop;
QTimer timer;
connect(this->socket, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
timer.setSingleShot(true);
timer.start(milliseconds);
eventLoop.exec();
return timer.isActive();
}