forked from kangear/NetAssistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudpclient.cpp
More file actions
162 lines (148 loc) · 4.32 KB
/
udpclient.cpp
File metadata and controls
162 lines (148 loc) · 4.32 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "udpclient.h"
#include <QMessageBox>
UDPClient::UDPClient(QObject *parent) :
QObject(parent)
{
qDebug("%s", __func__);
}
/**
* 处理网络连接错误
* @brief MyUDP::connection_error
* @param err
*/
void UDPClient::connection_error(QAbstractSocket::SocketError err)
{
qDebug("%s", __func__);
switch(err){
case 0:QMessageBox::critical(0,"connection error","The connection was refused by the peer (or timed out).",QMessageBox::Ok);
break;
case 2:QMessageBox::critical(0,"connection error","The host address was not found.",QMessageBox::Ok);
break;
case QAbstractSocket::NetworkError:QMessageBox::critical(0,"connection error","An error occurred with the network .",QMessageBox::Ok);
break;
case QAbstractSocket::RemoteHostClosedError:
QMessageBox::critical(0,"connection error","disconnect .",QMessageBox::Ok);
break;
default:QMessageBox::critical(0,"connection error","undefine error.",QMessageBox::Ok);
qDebug()<<"error is ......."<<err;
break;
}
}
/**
* @brief MyUDP::sendData
* @param string 发送内容
* @param remoteIp 目标IP地址
* @param port 目标端口号
*/
void UDPClient::sendData(const QString string, const QString remoteIp, const int port)
{
qDebug("%s %d", __func__, __LINE__);
if(udpSendSocket == nullptr)
return;
qDebug("%s %d", __func__, __LINE__);
QByteArray Data;
Data.append(string);
emit updateState(QString(), QVariant(QVariant::Int), udpSendSocket->writeDatagram(Data, QHostAddress(remoteIp), port));
}
/**
* @brief UDPClient::udpListnerStart
* @param ip 监听IP地址
* @param port 监听端口号
*/
void UDPClient::udpListnerStart(const QHostAddress ip, const int port)
{
qDebug("%s", __func__);
udpListnerSocket = new QUdpSocket(this);
if(!udpListnerSocket->bind(ip, port)) {
qWarning("NULL");
}
connect(udpListnerSocket, SIGNAL(readyRead()), this, SLOT(readyListnerRead()));
connect(udpListnerSocket, SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(connection_error(QAbstractSocket::SocketError)));
}
/**
* @brief UDPClient::udpListnerStop
* 停止监听
*/
void UDPClient::udpListnerStop()
{
qDebug("%s", __func__);
if(udpListnerSocket != nullptr) {
udpListnerSocket->close();
udpListnerSocket = nullptr;
}
}
/**
* @brief UDPClient::udpStart
* @param localIp
* @param listnerPort
* @param remoteIp
* @param remotePort
* 启动UDP服务
*/
void UDPClient::udpStart(const QHostAddress localIp, const int listnerPort, const QHostAddress remoteIp, const int remotePort)
{
qDebug("%s", __func__);
// 开启发送
if(udpSendSocket == nullptr) {
udpSendSocket = new QUdpSocket(this);
connect(udpSendSocket, SIGNAL(readyRead()), this, SLOT(readySendRead()));
connect(udpSendSocket, SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(connection_error(QAbstractSocket::SocketError)));
}
// 开启接收Socket
udpListnerStart(localIp, listnerPort);
}
/**
* @brief UDPClient::udpStop
* @param string
* @param remoteIp
* @param port
* 停止UDP服务
*/
void UDPClient::udpStop(const QString string, const QString remoteIp, const int port)
{
qDebug("%s", __func__);
// 关闭发送Socket
if(udpSendSocket != nullptr) {
udpSendSocket->close();
udpSendSocket = nullptr;
}
// 关闭Listner
udpListnerStop();
}
/**
* @brief UDPClient::readyListnerRead
* 监听读到数据
*/
void UDPClient::readyListnerRead()
{
qDebug("%s", __func__);
readyRead(udpListnerSocket);
}
/**
* @brief UDPClient::readyRead
* @param socket
* 读取数据
*/
void UDPClient::readyRead(QUdpSocket* socket)
{
qDebug("%s", __func__);
QByteArray Buffer;
Buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(Buffer.data(), Buffer.size(), &sender, &senderPort);
qDebug() << "Message from:" << sender.toString();
qDebug() << "Message port:" << senderPort;
qDebug() << "Message: " << Buffer;
emit valueChanged(Buffer);
emit updateState(QString(), Buffer.size(), QVariant(QVariant::Int));
}
/**
* @brief UDPClient::readySendRead
* 发送得到回传数据
*/
void UDPClient::readySendRead()
{
qDebug("%s", __func__);
readyRead(udpSendSocket);
}