├── .gitignore ├── NetAssistant.pro ├── README.md ├── commonhelper.cpp ├── commonhelper.h ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindow.ui ├── res ├── MainWindow_019.png └── Selection_018.png ├── udpclient.cpp └── udpclient.h /.gitignore: -------------------------------------------------------------------------------- 1 | # C++ objects and libs 2 | 3 | *.slo 4 | *.lo 5 | *.o 6 | *.a 7 | *.la 8 | *.lai 9 | *.so 10 | *.dll 11 | *.dylib 12 | 13 | # Qt-es 14 | 15 | /.qmake.cache 16 | /.qmake.stash 17 | *.pro.user 18 | *.pro.user.* 19 | *.qbs.user 20 | *.qbs.user.* 21 | *.moc 22 | moc_*.cpp 23 | qrc_*.cpp 24 | ui_*.h 25 | Makefile* 26 | *-build-* 27 | 28 | # QtCreator 29 | 30 | *.autosave 31 | 32 | #QtCtreator Qml 33 | *.qmlproject.user 34 | *.qmlproject.user.* 35 | -------------------------------------------------------------------------------- /NetAssistant.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2015-06-27T22:18:28 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui network 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = NetAssistant 12 | TEMPLATE = app 13 | 14 | QMAKE_CXXFLAGS += -std=c++0x 15 | 16 | SOURCES += main.cpp\ 17 | mainwindow.cpp \ 18 | udpclient.cpp \ 19 | commonhelper.cpp 20 | 21 | HEADERS += mainwindow.h \ 22 | udpclient.h \ 23 | commonhelper.h 24 | 25 | FORMS += mainwindow.ui 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NetAssistant 2 | 3 | ### 预览效果 4 | ![GitHub Logo](/res/MainWindow_019.png) 5 | 6 | ### 说明 7 | 已完成的功能有:UDP 手动发送 断开/连接UDP 清空接收区/发送区 计数 状态 日期 8 | 9 | 未完成的功能有:TCP 实时发送 自动发送 16进制显示 日期仅为打开时日期,并未做更新 10 | 11 | ### 其它 12 | 本程序界面是按照Windows下的 TCP调试助手 设计: 13 | ![GitHub Logo](/res/Selection_018.png) 14 | -------------------------------------------------------------------------------- /commonhelper.cpp: -------------------------------------------------------------------------------- 1 | #include "commonhelper.h" 2 | #include 3 | #include 4 | 5 | CommonHelper::CommonHelper(QObject *parent) : 6 | QObject(parent) 7 | { 8 | } 9 | 10 | QHostAddress CommonHelper::getLocalHostIP() 11 | { 12 | QList AddressList = QNetworkInterface::allAddresses(); 13 | QHostAddress result; 14 | foreach(QHostAddress address, AddressList){ 15 | if(address.protocol() == QAbstractSocket::IPv4Protocol && 16 | address != QHostAddress::Null && 17 | address != QHostAddress::LocalHost){ 18 | if (address.toString().contains("127.0.")){ 19 | continue; 20 | } 21 | result = address; 22 | break; 23 | } 24 | } 25 | return result; 26 | } 27 | -------------------------------------------------------------------------------- /commonhelper.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMONHELPER_H 2 | #define COMMONHELPER_H 3 | 4 | #include 5 | #include 6 | 7 | class CommonHelper : public QObject 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit CommonHelper(QObject *parent = 0); 12 | QHostAddress getLocalHostIP(); 13 | 14 | signals: 15 | 16 | public slots: 17 | 18 | }; 19 | 20 | #endif // COMMONHELPER_H 21 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | #include "udpclient.h" 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | MainWindow w; 9 | w.show(); 10 | 11 | qDebug("%s", __func__); 12 | 13 | return a.exec(); 14 | } 15 | -------------------------------------------------------------------------------- /mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | #include 4 | #include "udpclient.h" 5 | #include 6 | #include 7 | #include "commonhelper.h" 8 | #include 9 | 10 | MainWindow::MainWindow(QWidget *parent) : 11 | QMainWindow(parent), 12 | ui(new Ui::MainWindow) 13 | { 14 | qDebug("%s", __func__); 15 | ui->setupUi(this); 16 | 17 | /* 读取配置文件 */ 18 | doSettings(false); 19 | 20 | /* 设置默认通讯模式 */ 21 | ui->tcpclient_radioButton->setChecked(true); 22 | /** 目前设置为UDP为默认方式 */ 23 | ui->udp_radioButton->setChecked(true); 24 | /** 设置远程主机IP地址 获取本机IP */ 25 | ui->remoteIP_lineEdit->setText(mRemoteIp); 26 | /* 设置远程端口号 */ 27 | /* TODO: 将其设置为不能以0开头 */ 28 | ui->remoteport_spinBox->setRange(1024,9999); 29 | ui->remoteport_spinBox->setValue(mRemotePort); 30 | /* 设置本地端口号 */ 31 | ui->localport_spinBox->setRange(1024,9999); 32 | ui->localport_spinBox->setValue(mLocalPort); 33 | 34 | isConnect = false; 35 | 36 | // 状态栏 37 | statusLabel = new QLabel; 38 | statusLabel->setMinimumSize(260, 20); // 设置标签最小大小 39 | statusLabel->setFrameShape(QFrame::WinPanel); // 设置标签形状 40 | statusLabel->setFrameShadow(QFrame::Sunken); // 设置标签阴影 41 | ui->statusBar->addWidget(statusLabel); 42 | statusLabel->setText("UDP通信停止"); 43 | statusLabel->setAlignment(Qt::AlignHCenter); 44 | 45 | // 接收数量 46 | receiveLabel = new QLabel; 47 | receiveLabel->setMinimumSize(150, 20); // 设置标签最小大小 48 | receiveLabel->setFrameShape(QFrame::WinPanel); // 设置标签形状 49 | receiveLabel->setFrameShadow(QFrame::Sunken); // 设置标签阴影 50 | ui->statusBar->addWidget(receiveLabel); 51 | receiveLabel->setAlignment(Qt::AlignHCenter); 52 | 53 | // 发送数量 54 | sendLabel = new QLabel; 55 | sendLabel->setMinimumSize(150, 20); // 设置标签最小大小 56 | sendLabel->setFrameShape(QFrame::WinPanel); // 设置标签形状 57 | sendLabel->setFrameShadow(QFrame::Sunken); // 设置标签阴影 58 | ui->statusBar->addWidget(sendLabel); 59 | sendLabel->setAlignment(Qt::AlignHCenter); 60 | updateStateBar(QString(), 0, 0); 61 | 62 | // 计数器清零 button 63 | clearCounterButton = new QPushButton(); 64 | ui->statusBar->addWidget(clearCounterButton); 65 | clearCounterButton->setText(tr("计数器清零")); 66 | connect(clearCounterButton, SIGNAL(released()), this, SLOT(on_clearCounter_pushButton_released())); 67 | 68 | // 时间 TODO:要进行更新 69 | timeLabel = new QLabel; 70 | timeLabel->setMinimumSize(90, 20); // 设置标签最小大小 71 | timeLabel->setMaximumWidth(90); 72 | timeLabel->setFrameShape(QFrame::WinPanel); // 设置标签形状 73 | timeLabel->setFrameShadow(QFrame::Sunken); // 设置标签阴影 74 | ui->statusBar->addWidget(timeLabel); 75 | timeLabel->setText(QDate::currentDate().toString("yyyy-MM-dd")); 76 | 77 | // 更新接收到的数据 78 | connect(&client, SIGNAL(valueChanged(QString)), this, SLOT(updateReceiveText(QString))); 79 | connect(&client, 80 | SIGNAL(updateState(QString, QVariant, QVariant)), 81 | this, SLOT(updateStateBar(QString, QVariant, QVariant))); 82 | 83 | init(); 84 | 85 | mReceiveNum = mSendNum = 0; 86 | } 87 | 88 | void MainWindow::connectNet() 89 | { 90 | qDebug("%s", __func__); 91 | 92 | mRemoteIp = ui->remoteIP_lineEdit->text(); 93 | mRemotePort = ui->remoteport_spinBox->text().toInt(); 94 | mLocalPort = ui->localport_spinBox->text().toInt(); 95 | updateStateBar("UDP通信 " + mRemoteIp + ":" + QString().number(mRemotePort), 96 | QVariant(QVariant::Int), QVariant(QVariant::Int)); 97 | 98 | // No.1 99 | isConnect = true; 100 | // 将状态设置为 通 101 | ui->state_label->setText("通"); 102 | QPalette pa; 103 | pa.setColor(QPalette::WindowText,Qt::blue); 104 | ui->state_label->setPalette(pa); 105 | 106 | // 将按钮设置为 断开网络 107 | ui->connect_pushButton->setText("断开网络"); 108 | 109 | // 禁用远程端口,本地端口,远程IP 110 | ui->remoteIP_lineEdit->setEnabled(false); 111 | ui->remoteport_spinBox->setEnabled(false); 112 | ui->localport_spinBox->setEnabled(false); 113 | // 使能button 114 | ui->handSend_pushButton->setEnabled(true); 115 | 116 | client.udpStart(chelper.getLocalHostIP(), mLocalPort, QHostAddress(mRemoteIp), mRemotePort); 117 | } 118 | 119 | void MainWindow::updateReceiveText(const QString string) 120 | { 121 | QString oldString = ui->receive_textBrowser->toPlainText(); 122 | ui->receive_textBrowser->setText(oldString + string + "\n"); 123 | 124 | // 将光标移动到最后位置 125 | QTextCursor tmpCursor = ui->receive_textBrowser->textCursor(); 126 | tmpCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor, 4); 127 | ui->receive_textBrowser->setTextCursor(tmpCursor); 128 | } 129 | 130 | /** 131 | * @brief MainWindow::updateStateBar 132 | * @param state 状态 133 | * @param inNum 接收数值 134 | * @param outNum 发送数值 135 | */ 136 | void MainWindow::updateStateBar(QString state, QVariant inNum, QVariant outNum) 137 | { 138 | if(!state.isNull()) 139 | statusLabel->setText(state); 140 | 141 | if(!inNum.isNull()) { 142 | //累计 143 | if(inNum.toInt() == 0) 144 | mReceiveNum = 0; 145 | else 146 | mReceiveNum += inNum.toInt(); 147 | receiveLabel->setText("接收:" + QString::number(mReceiveNum)); 148 | } 149 | 150 | if(!outNum.isNull()) { 151 | //累计 152 | if(outNum.toInt() == 0) 153 | mSendNum = 0; 154 | else 155 | mSendNum += outNum.toInt(); 156 | sendLabel->setText("发送:" + QString::number(mSendNum)); 157 | } 158 | 159 | } 160 | 161 | /** 162 | * 断开UDP时调用该函数 163 | * @brief MainWindow::init 164 | */ 165 | void MainWindow::init() 166 | { 167 | qDebug("%s", __func__); 168 | // No.1 169 | isConnect = false; 170 | // 将状态设置为 断 171 | ui->state_label->setText("断"); 172 | QPalette pa; 173 | pa.setColor(QPalette::WindowText,Qt::red); 174 | ui->state_label->setPalette(pa); 175 | 176 | // 将按钮设置为 连接网络 177 | ui->connect_pushButton->setText("连接网络"); 178 | 179 | // 使能远程端口,本地端口,远程IP 180 | ui->remoteIP_lineEdit->setEnabled(true); 181 | ui->remoteport_spinBox->setEnabled(true); 182 | ui->localport_spinBox->setEnabled(true); 183 | // 禁用button 184 | ui->handSend_pushButton->setEnabled(false); 185 | // 186 | client.udpStop(NULL, NULL, NULL); 187 | 188 | updateStateBar("本地IP: " + chelper.getLocalHostIP().toString() + " 无连接", 189 | QVariant(QVariant::Int), QVariant(QVariant::Int)); 190 | } 191 | 192 | /** 193 | * 断开UDP时调用该函数 194 | * @brief MainWindow::disConnectNet 195 | */ 196 | void MainWindow::disConnectNet() 197 | { 198 | qDebug("%s", __func__); 199 | // No.1 200 | isConnect = false; 201 | // 将状态设置为 断 202 | ui->state_label->setText("断"); 203 | QPalette pa; 204 | pa.setColor(QPalette::WindowText,Qt::red); 205 | ui->state_label->setPalette(pa); 206 | 207 | // 将按钮设置为 连接网络 208 | ui->connect_pushButton->setText("连接网络"); 209 | 210 | // 使能远程端口,本地端口,远程IP 211 | ui->remoteIP_lineEdit->setEnabled(true); 212 | ui->remoteport_spinBox->setEnabled(true); 213 | ui->localport_spinBox->setEnabled(true); 214 | // 禁用button 215 | ui->handSend_pushButton->setEnabled(false); 216 | // 217 | client.udpStop(NULL, NULL, NULL); 218 | 219 | 220 | updateStateBar(tr("UDP通信停止"), QVariant(QVariant::Int), QVariant(QVariant::Int)); 221 | } 222 | 223 | void MainWindow::doSettings(bool isWrite) 224 | { 225 | QSettings settings("Yzs_think", "Application"); 226 | const QString REMOTE_IP = "remoteip"; 227 | const QString REMOTE_PORT = "remoteport"; 228 | const QString LOCAL_PORT = "localport"; 229 | if(isWrite) { 230 | settings.setValue(REMOTE_IP, mRemoteIp); 231 | settings.setValue(REMOTE_PORT, mRemotePort); 232 | settings.setValue(LOCAL_PORT, mLocalPort); 233 | } else { 234 | mRemoteIp = settings.value(REMOTE_IP, chelper.getLocalHostIP().toString()).toString(); 235 | mRemotePort = settings.value(REMOTE_PORT, 1234).toInt(); 236 | mLocalPort = settings.value(LOCAL_PORT, 2468).toInt(); 237 | } 238 | } 239 | 240 | MainWindow::~MainWindow() 241 | { 242 | doSettings(true); 243 | delete ui; 244 | } 245 | 246 | /** 247 | * @brief MainWindow::on_clearCounter_pushButton_released 248 | * 该函数是将计数器置零 249 | */ 250 | void MainWindow::on_clearCounter_pushButton_released() 251 | { 252 | qDebug("%s", __func__); 253 | updateStateBar(QString(), 0, 0); 254 | } 255 | 256 | void MainWindow::on_clearReceive_pushButton_released() 257 | { 258 | ui->receive_textBrowser->clear(); 259 | } 260 | 261 | void MainWindow::on_clearSend_pushButton_released() 262 | { 263 | ui->send_plainTextEdit->clear(); 264 | } 265 | 266 | /** 267 | * @brief MainWindow::on_connect_pushButton_released 268 | * 该函数是在连接/断开网络时调用 269 | */ 270 | void MainWindow::on_connect_pushButton_released() 271 | { 272 | qDebug("%s", __func__); 273 | // 如果当前网络是连接状态 调用断开连接函数 274 | if(isConnect) { 275 | disConnectNet(); 276 | } else { // 否则调用连接函数 277 | connectNet(); 278 | } 279 | } 280 | 281 | /** 282 | * 当用户点击 发送 时调用该函数 283 | * @brief MainWindow::on_handSend_pushButton_released 284 | */ 285 | void MainWindow::on_handSend_pushButton_released() 286 | { 287 | // 获取 rmeote ip/ port 和内容 288 | QString string = ui->send_plainTextEdit->toPlainText(); 289 | if(string.length() != 0) { 290 | client.sendData(string, mRemoteIp, mRemotePort); 291 | } 292 | } 293 | 294 | void MainWindow::on_quit_pushButton_released() 295 | { 296 | QApplication::quit(); 297 | } 298 | -------------------------------------------------------------------------------- /mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "udpclient.h" 9 | #include "commonhelper.h" 10 | 11 | namespace Ui { 12 | class MainWindow; 13 | } 14 | 15 | class MainWindow : public QMainWindow 16 | { 17 | Q_OBJECT 18 | 19 | public: 20 | explicit MainWindow(QWidget *parent = 0); 21 | ~MainWindow(); 22 | 23 | private slots: 24 | void on_clearReceive_pushButton_released(); 25 | void on_clearSend_pushButton_released(); 26 | void on_connect_pushButton_released(); 27 | void on_handSend_pushButton_released(); 28 | void updateReceiveText(const QString string); 29 | void on_clearCounter_pushButton_released(); 30 | void on_quit_pushButton_released(); 31 | void updateStateBar(QString state, QVariant inNum, QVariant outNum); 32 | 33 | private: 34 | Ui::MainWindow *ui; 35 | QUdpSocket *udpSocket; 36 | void connectNet(); 37 | void disConnectNet(); 38 | bool isConnect; 39 | UDPClient client; 40 | /** 工具类 */ 41 | CommonHelper chelper; 42 | /** 状态标签 */ 43 | QLabel *statusLabel; 44 | /** 总接收数量显示标签 */ 45 | QLabel *receiveLabel; 46 | /** 总发送数量显示标签 */ 47 | QLabel *sendLabel; 48 | /** 状态栏 计数清零 按钮 */ 49 | QPushButton *clearCounterButton; 50 | /** 状态栏 时间标签 */ 51 | QLabel *timeLabel; 52 | /** 接收总数 */ 53 | quint64 mReceiveNum; 54 | /** 发送总数 */ 55 | quint64 mSendNum; 56 | /** 远程IP */ 57 | QString mRemoteIp; 58 | /** 远程端口 */ 59 | int mRemotePort; 60 | /** 本地端口 */ 61 | int mLocalPort; 62 | 63 | void doSettings(bool isWrite); 64 | void init(); 65 | }; 66 | 67 | #endif // MAINWINDOW_H 68 | -------------------------------------------------------------------------------- /mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 781 10 | 649 11 | 12 | 13 | 14 | 15 | 781 16 | 649 17 | 18 | 19 | 20 | 21 | 781 22 | 649 23 | 24 | 25 | 26 | 网络调试助手(V0.1) 27 | 28 | 29 | border-color: rgb(170, 170, 255); 30 | 31 | 32 | 33 | 34 | 35 | 0 36 | 0 37 | 781 38 | 601 39 | 40 | 41 | 42 | 43 | 781 44 | 601 45 | 46 | 47 | 48 | 49 | 781 50 | 601 51 | 52 | 53 | 54 | background-color: rgb(192, 192, 255); 55 | 56 | 57 | QFrame::StyledPanel 58 | 59 | 60 | QFrame::Raised 61 | 62 | 63 | 64 | 65 | 200 66 | 10 67 | 571 68 | 401 69 | 70 | 71 | 72 | 73 | 74 | 0 75 | 0 76 | 571 77 | 31 78 | 79 | 80 | 81 | background-color: rgb(248, 255, 188); 82 | 83 | 84 | 85 | 86 | 160 87 | 0 88 | 267 89 | 27 90 | 91 | 92 | 93 | 94 | 95 | 96 | 数据接收区 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 71 105 | 21 106 | 107 | 108 | 109 | 110 | 71 111 | 21 112 | 113 | 114 | 115 | Qt::Vertical 116 | 117 | 118 | 119 | 120 | 121 | 122 | 16进制显示 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 0 133 | 30 134 | 571 135 | 371 136 | 137 | 138 | 139 | background-color: rgb(255, 255, 255); 140 | 141 | 142 | 143 | 144 | 145 | 146 | 200 147 | 420 148 | 571 149 | 171 150 | 151 | 152 | 153 | 154 | 155 | 0 156 | 10 157 | 571 158 | 31 159 | 160 | 161 | 162 | background-color: rgb(248, 255, 188); 163 | 164 | 165 | 166 | 167 | 1 168 | 0 169 | 460 170 | 27 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 121 179 | 16777215 180 | 181 | 182 | 183 | 自动发送(ms) 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 61 192 | 21 193 | 194 | 195 | 196 | 197 | 61 198 | 16777215 199 | 200 | 201 | 202 | 1000 203 | 204 | 205 | 206 | 207 | 208 | 209 | 数据接收区 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 71 218 | 21 219 | 220 | 221 | 222 | 223 | 71 224 | 21 225 | 226 | 227 | 228 | Qt::Vertical 229 | 230 | 231 | 232 | 233 | 234 | 235 | 16进制显示 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 0 246 | 40 247 | 1131 248 | 211 249 | 250 | 251 | 252 | background-color: rgb(255, 255, 255); 253 | 254 | 255 | 256 | 257 | 258 | 259 | 10 260 | 10 261 | 183 262 | 579 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 181 271 | 401 272 | 273 | 274 | 275 | 276 | 181 277 | 401 278 | 279 | 280 | 281 | background-color: rgb(255, 169, 255); 282 | border-color: rgb(58, 68, 255); 283 | 284 | 285 | 286 | 287 | 0 288 | 0 289 | 180 290 | 391 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 161 299 | 131 300 | 301 | 302 | 303 | 304 | 161 305 | 131 306 | 307 | 308 | 309 | <html><head/><body><p><br/></p></body></html> 310 | 311 | 312 | 313 | 314 | 315 | 通讯模式 316 | 317 | 318 | 319 | 320 | 10 321 | 40 322 | 106 323 | 98 324 | 325 | 326 | 327 | 328 | 329 | 330 | TCP Client 331 | 332 | 333 | true 334 | 335 | 336 | 337 | 338 | 339 | 340 | TCP Server 341 | 342 | 343 | 344 | 345 | 346 | 347 | UDP 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | Qt::Horizontal 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 161 367 | 151 368 | 369 | 370 | 371 | 372 | 161 373 | 151 374 | 375 | 376 | 377 | <html><head/><body><p><br/></p></body></html> 378 | 379 | 380 | 设置 381 | 382 | 383 | 384 | 385 | 10 386 | 30 387 | 148 388 | 125 389 | 390 | 391 | 392 | 393 | 394 | 395 | 远程主机 396 | 397 | 398 | 399 | 400 | 401 | 402 | background-color: rgb(255, 255, 255); 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 远程端口 412 | 413 | 414 | 415 | 416 | 417 | 418 | background-color: rgb(255, 255, 255); 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 本地端口 430 | 431 | 432 | 433 | 434 | 435 | 436 | background-color: rgb(255, 255, 255); 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | Qt::Horizontal 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | Qt::Horizontal 459 | 460 | 461 | 462 | 18 463 | 20 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | Qt::Horizontal 479 | 480 | 481 | 482 | 40 483 | 20 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | background-color: rgb(234, 235, 214); 492 | 493 | 494 | 连接网络 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | Qt::Horizontal 504 | 505 | 506 | 507 | 508 | 509 | 510 | background-color: rgb(234, 235, 214); 511 | 512 | 513 | 清空接收区 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | Qt::Horizontal 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 181 533 | 161 534 | 535 | 536 | 537 | background-color: rgb(255, 169, 255); 538 | border-color: rgb(58, 68, 255); 539 | 540 | 541 | 542 | 543 | 30 544 | 100 545 | 118 546 | 3 547 | 548 | 549 | 550 | Qt::Horizontal 551 | 552 | 553 | 554 | 555 | 556 | 0 557 | 0 558 | 181 559 | 161 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 实时 569 | 发送 570 | 571 | 572 | 573 | 574 | 575 | 576 | background-color: rgb(234, 235, 214); 577 | 578 | 579 | 手动发送 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | Qt::Horizontal 589 | 590 | 591 | 592 | 593 | 594 | 595 | background-color: rgb(234, 235, 214); 596 | 597 | 598 | 清空发送区 599 | 600 | 601 | 602 | 603 | 604 | 605 | Qt::Horizontal 606 | 607 | 608 | 609 | 610 | 611 | 612 | background-color: rgb(234, 235, 214); 613 | 614 | 615 | 退出 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 0 631 | 0 632 | 781 633 | 28 634 | 635 | 636 | 637 | 638 | 文件(F) 639 | 640 | 641 | 642 | 643 | 工具(T) 644 | 645 | 646 | 647 | 648 | 编码方式 649 | 650 | 651 | 652 | 653 | 关于(A) 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | -------------------------------------------------------------------------------- /res/MainWindow_019.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kangear/NetAssistant/b3dfb4e2727a66a8452f4ebe13581a7952284cfc/res/MainWindow_019.png -------------------------------------------------------------------------------- /res/Selection_018.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kangear/NetAssistant/b3dfb4e2727a66a8452f4ebe13581a7952284cfc/res/Selection_018.png -------------------------------------------------------------------------------- /udpclient.cpp: -------------------------------------------------------------------------------- 1 | #include "udpclient.h" 2 | #include 3 | 4 | UDPClient::UDPClient(QObject *parent) : 5 | QObject(parent) 6 | { 7 | qDebug("%s", __func__); 8 | } 9 | 10 | /** 11 | * 处理网络连接错误 12 | * @brief MyUDP::connection_error 13 | * @param err 14 | */ 15 | void UDPClient::connection_error(QAbstractSocket::SocketError err) 16 | { 17 | qDebug("%s", __func__); 18 | switch(err){ 19 | case 0:QMessageBox::critical(0,"connection error","The connection was refused by the peer (or timed out).",QMessageBox::Ok); 20 | break; 21 | case 2:QMessageBox::critical(0,"connection error","The host address was not found.",QMessageBox::Ok); 22 | break; 23 | case QAbstractSocket::NetworkError:QMessageBox::critical(0,"connection error","An error occurred with the network .",QMessageBox::Ok); 24 | break; 25 | 26 | case QAbstractSocket::RemoteHostClosedError: 27 | QMessageBox::critical(0,"connection error","disconnect .",QMessageBox::Ok); 28 | break; 29 | default:QMessageBox::critical(0,"connection error","undefine error.",QMessageBox::Ok); 30 | qDebug()<<"error is ......."<writeDatagram(Data, QHostAddress(remoteIp), port)); 49 | } 50 | 51 | /** 52 | * @brief UDPClient::udpListnerStart 53 | * @param ip 监听IP地址 54 | * @param port 监听端口号 55 | */ 56 | void UDPClient::udpListnerStart(const QHostAddress ip, const int port) 57 | { 58 | qDebug("%s", __func__); 59 | udpListnerSocket = new QUdpSocket(this); 60 | if(!udpListnerSocket->bind(ip, port)) { 61 | qWarning("NULL"); 62 | } 63 | connect(udpListnerSocket, SIGNAL(readyRead()), this, SLOT(readyListnerRead())); 64 | connect(udpListnerSocket, SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(connection_error(QAbstractSocket::SocketError))); 65 | } 66 | 67 | /** 68 | * @brief UDPClient::udpListnerStop 69 | * 停止监听 70 | */ 71 | void UDPClient::udpListnerStop() 72 | { 73 | qDebug("%s", __func__); 74 | if(udpListnerSocket != nullptr) { 75 | udpListnerSocket->close(); 76 | udpListnerSocket = nullptr; 77 | } 78 | } 79 | 80 | /** 81 | * @brief UDPClient::udpStart 82 | * @param localIp 83 | * @param listnerPort 84 | * @param remoteIp 85 | * @param remotePort 86 | * 启动UDP服务 87 | */ 88 | void UDPClient::udpStart(const QHostAddress localIp, const int listnerPort, const QHostAddress remoteIp, const int remotePort) 89 | { 90 | qDebug("%s", __func__); 91 | // 开启发送 92 | if(udpSendSocket == nullptr) { 93 | udpSendSocket = new QUdpSocket(this); 94 | connect(udpSendSocket, SIGNAL(readyRead()), this, SLOT(readySendRead())); 95 | connect(udpSendSocket, SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(connection_error(QAbstractSocket::SocketError))); 96 | } 97 | 98 | // 开启接收Socket 99 | udpListnerStart(localIp, listnerPort); 100 | } 101 | 102 | /** 103 | * @brief UDPClient::udpStop 104 | * @param string 105 | * @param remoteIp 106 | * @param port 107 | * 停止UDP服务 108 | */ 109 | void UDPClient::udpStop(const QString string, const QString remoteIp, const int port) 110 | { 111 | qDebug("%s", __func__); 112 | // 关闭发送Socket 113 | if(udpSendSocket != nullptr) { 114 | udpSendSocket->close(); 115 | udpSendSocket = nullptr; 116 | } 117 | 118 | // 关闭Listner 119 | udpListnerStop(); 120 | } 121 | 122 | /** 123 | * @brief UDPClient::readyListnerRead 124 | * 监听读到数据 125 | */ 126 | void UDPClient::readyListnerRead() 127 | { 128 | qDebug("%s", __func__); 129 | readyRead(udpListnerSocket); 130 | } 131 | 132 | /** 133 | * @brief UDPClient::readyRead 134 | * @param socket 135 | * 读取数据 136 | */ 137 | void UDPClient::readyRead(QUdpSocket* socket) 138 | { 139 | qDebug("%s", __func__); 140 | QByteArray Buffer; 141 | Buffer.resize(socket->pendingDatagramSize()); 142 | 143 | QHostAddress sender; 144 | quint16 senderPort; 145 | socket->readDatagram(Buffer.data(), Buffer.size(), &sender, &senderPort); 146 | 147 | qDebug() << "Message from:" << sender.toString(); 148 | qDebug() << "Message port:" << senderPort; 149 | qDebug() << "Message: " << Buffer; 150 | emit valueChanged(Buffer); 151 | emit updateState(QString(), Buffer.size(), QVariant(QVariant::Int)); 152 | } 153 | 154 | /** 155 | * @brief UDPClient::readySendRead 156 | * 发送得到回传数据 157 | */ 158 | void UDPClient::readySendRead() 159 | { 160 | qDebug("%s", __func__); 161 | readyRead(udpSendSocket); 162 | } 163 | -------------------------------------------------------------------------------- /udpclient.h: -------------------------------------------------------------------------------- 1 | #ifndef UDPCLIENT_H 2 | #define UDPCLIENT_H 3 | 4 | #include 5 | #include 6 | 7 | class UDPClient : public QObject 8 | { 9 | Q_OBJECT 10 | public: 11 | explicit UDPClient(QObject *parent = 0); 12 | void sendData(const QString string, const QString remoteIp, const int port); 13 | void udpStart(const QHostAddress localIp, const int listnerPort, const QHostAddress remoteIp, const int remotePort); 14 | void udpStop(const QString string, const QString remoteIp, const int port); 15 | void readyRead(QUdpSocket* socket); 16 | 17 | signals: 18 | void valueChanged(QString newValue); 19 | void updateState(QString state, QVariant inNum, QVariant outNum); 20 | public slots: 21 | void readyListnerRead(); 22 | void readySendRead(); 23 | void connection_error(QAbstractSocket::SocketError err); 24 | private: 25 | QUdpSocket *udpSendSocket = nullptr; 26 | QUdpSocket *udpListnerSocket = nullptr; 27 | void udpListnerStart(const QHostAddress ip, const int port); 28 | void udpListnerStop(); 29 | }; 30 | 31 | #endif // UDPCLIENT_H 32 | 33 | --------------------------------------------------------------------------------