mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_radioButton_2_clicked(bool checked);
void on_radioButton_clicked(bool checked);
void on_pushButton_3_clicked();
void on_pushButton_5_clicked();
void on_radioButton_3_clicked(bool checked);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
//UDP
void MainWindow::on_radioButton_3_clicked(bool checked)
{
ui->pushButton_3->setText("打开");
}
//TCP服务器端
void MainWindow::on_radioButton_2_clicked(bool checked)
{
if(checked){
ui->pushButton_3->setText("开始监听");
//关联信号与槽
connect(server,&QTcpServer::newConnection,this,&MainWindow::accept_connect);
}
}
//TCP客户端
void MainWindow::on_radioButton_clicked(bool checked)
{
if(checked){
ui->pushButton_3->setText("链接网络");
//关联信号与槽
connect(socket,&QTcpSocket::readyRead,this,&MainWindow::read_date);
//取消新的链接请求
disconnect(connect(server,&QTcpServer::newConnection,this,&MainWindow::accept_connect));
//取消服务器的监听
server->close();
}
}
//链接网络与开始监听按钮
void MainWindow::on_pushButton_3_clicked()
{
//TCP服务器
if(ui->pushButton_3->text() == "开始监听")
{
//获取端口
QString local_port = ui->lineEdit_port_1->text();
//开始监听
if(server->listen(QHostAddress::Any,local_port.toShort()))
{
ui->label_4->setText("正在监听");
}else
{
ui->label_4->setText("监听失败");
}
}
//TCP客户端
if(ui->pushButton_3->text() == "链接网络")
{
//获取端口和IP
QString ip = ui->lineEdit_ip_2->text();
QString port = ui->lineEdit_port_2->text();
//链接服务器
socket->connectToHost(ip,port.toInt());
}
//判断是链接网络还是监听
if(ui->pushButton_3->text() == "打开")
{
//获取端口和IP
QString ip1 = ui->lineEdit_ip_1->text();
QString port1 = ui->lineEdit_port_1->text();
//建立连接
udp->bind(QHostAddress::Any,port1.toInt());
qDebug() << "Listening on port1 " << port1;
connect(udp,&QUdpSocket::readyRead,this,&MainWindow::read_date);
}
}
//新的链接请求
void MainWindow::accept_connect()
{
int ret = QMessageBox::warning(this, tr("链接提示"),
tr("有新的客户端链接是否接收请求\n"),
QMessageBox::Yes|QMessageBox::No);
if(ret == QMessageBox::Yes) //接收链接
{
new_socket = server->nextPendingConnection();
qDebug() << new_socket->peerAddress().toString();
//关联可读信号
connect(new_socket,&QTcpSocket::readyRead,this,&MainWindow::read_date);
}
}
//接收
void MainWindow::read_date()
{
if (ui->pushButton_3->text() == "开始监听")
{
ui->plainTextEdit->appendPlainText(new_socket->readAll());
}
else if (ui->pushButton_3->text() == "链接网络")
{
ui->plainTextEdit->appendPlainText(socket->readAll());
}
// if (ui->pushButton_3->text() == "打开"){
// qDebug() << "有数据可读";
// //判断是否有网络数据
// while (udp->hasPendingDatagrams()) {
// //把所有数据读到 datagram
// QNetworkDatagram datagram = udp->receiveDatagram();
// //输出数据
// qDebug() << datagram.senderAddress().toString();
// qDebug() << datagram.senderPort();
// qDebug() << datagram.data();
// ui->plainTextEdit->appendPlainText(udp->readAll());
// }
// }
if (ui->pushButton_3->text() == "打开"){
char buf[1024]={0};
QHostAddress ip;
unsigned short port=0;
udp->readDatagram(buf,1024,&ip,&port);
ui->plainTextEdit->appendPlainText(buf);
}
}
//发送
void MainWindow::on_pushButton_5_clicked()
{
//服务器
if(ui->pushButton_3->text() == "开始监听")
{
QString msg = ui->plainTextEdit_2->toPlainText();
qDebug() << msg;
new_socket->write(msg.toUtf8());
}
//客户端
if(ui->pushButton_3->text() == "链接网络")
{
QString msg = ui->plainTextEdit_2->toPlainText();
qDebug() << msg;
socket->write(msg.toUtf8());
}
//UDP发送端
if(ui->pushButton_3->text() == "打开")
{
QString ip2 = ui->lineEdit_ip_2->text();
QString port2 = ui->lineEdit_port_2->text();
QString msg = ui->plainTextEdit_2->toPlainText();
udp->writeDatagram(msg.toUtf8(),QHostAddress(ip2),port2.toInt());
}
}
效果展示
![C++ & QT Creator 开发网络调试助手_第1张图片](http://img.e-com-net.com/image/info8/44fd0960b01e4ed8811e6d1a46519093.png)