代码部分:
.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QUdpSocket>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void onSocketStateChange(QAbstractSocket::SocketState socketState);
void onSocketReadyRead();//读取socket传入的数据
void on_bindBtn_clicked();
void on_stopBtn_clicked();
void on_btnSend_clicked();
void on_mulBtn_clicked();
void on_clearBtn_clicked();
void on_quitBtn_clicked();
private:
Ui::MainWindow *ui;
QLabel* LabSocketState;//socket状态显示标签
QUdpSocket* udpSocket;
QString getLocalIp();//获取本机IP地址
};
#endif // MAINWINDOW_H
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHostInfo>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
LabSocketState = new QLabel("socket 状态");
LabSocketState->setMinimumWidth(200);
ui->statusBar->addWidget(LabSocketState);
// QString localIP = getLocalIp();//获取本机IP
QString localIP = "127.0.0.1";//获取本机IP
this->setWindowTitle(this->windowTitle()+"----本机IP:"+localIP);
ui->comboTargetIp->addItem(localIP);
udpSocket = new QUdpSocket(this);
connect(udpSocket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(onSocketStateChange(QAbstractSocket::SocketState)));
onSocketStateChange(udpSocket->state());
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(onSocketReadyRead()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_bindBtn_clicked()
{//绑定端口
quint16 port = ui->spinBox->value();//本机UDP端口
if(udpSocket->bind(port))
{
ui->plainTextEdit->appendPlainText("****已绑定成功");
ui->plainTextEdit->appendPlainText("***绑定端口"+QString::number(udpSocket->localPort()));
ui->bindBtn->setEnabled(false);
ui->stopBtn->setEnabled(true);
}
}
void MainWindow::on_stopBtn_clicked()
{//解除绑定
udpSocket->abort();//解除绑定
ui->bindBtn->setEnabled(true);
ui->stopBtn->setEnabled(false);
ui->plainTextEdit->appendPlainText("***已解除绑定");
}
void MainWindow::on_btnSend_clicked()
{//发送消息
QString targetIP = ui->comboTargetIp->currentText();//目标IP
QHostAddress targetAddr(targetIP);
quint16 targetPort = ui->spinBox_3->value();//目标Port
QString msg = ui->lineEdit->text();//发送的消息内容
QByteArray str = msg.toUtf8();
udpSocket->writeDatagram(str,targetAddr,targetPort);//发出数据包
ui->plainTextEdit->appendPlainText("[out]"+msg);
ui->lineEdit->clear();
ui->lineEdit->setFocus();
}
void MainWindow::on_mulBtn_clicked()
{//广播消息按钮
quint16 targetPort = ui->spinBox_3->value();//目标端口
QString msg = ui->lineEdit->text();
QByteArray str = msg.toUtf8();
udpSocket->writeDatagram(str,QHostAddress::Broadcast,targetPort);
ui->plainTextEdit->appendPlainText("[broadcast]"+msg);
ui->lineEdit->clear();
ui->lineEdit->clear();
}
void MainWindow::onSocketReadyRead()
{//读取socket传入的数据
QByteArray datagram;
datagram.resize(udpSocket->hasPendingDatagrams());
QHostAddress peerAddr;
quint16 peerPort;
udpSocket->readDatagram(datagram.data(),datagram.size(),&peerAddr,&peerPort);
QString str =datagram.data();
QString peer = "[From "+peerAddr.toString()+":"+QString::number(peerPort)+"]";
ui->plainTextEdit->appendPlainText(peer+str);
}
void MainWindow::onSocketStateChange(QAbstractSocket::SocketState socketState)
{//socket状态变化
switch(socketState)
{
case QAbstractSocket::UnconnectedState:
{
LabSocketState->setText("socket状态:UnconnectedState");
break;
}
case QAbstractSocket::HostLookupState:
{
LabSocketState->setText("socket状态:HostLookupState");
break;
}
case QAbstractSocket::ConnectingState:
{
LabSocketState->setText("socket状态:ConnectingState");
break;
}
case QAbstractSocket::ConnectedState:
{
LabSocketState->setText("socket状态:ConnectedState");
break;
}
case QAbstractSocket::BoundState:
{
LabSocketState->setText("socket状态:BoundState");
break;
}
case QAbstractSocket::ClosingState:
{
LabSocketState->setText("socket状态:ClosingState");
break;
}
case QAbstractSocket::ListeningState:
{
LabSocketState->setText("socket状态:ListeningState");
break;
}
}
}
QString MainWindow::getLocalIp()
{//获取本机IP
QString hostName = QHostInfo::localHostName();//本地主机名
QHostInfo hostInfo = QHostInfo::fromName(hostName);
QString localIP = "";
QList<QHostAddress> addList = hostInfo.addresses();
if(!addList.isEmpty())
{
for(int i=0;i<addList.count();++i)
{
QHostAddress aHost = addList.at(i);
if(QAbstractSocket::IPv4Protocol == aHost.protocol())
{
localIP = aHost.toString();
break;
}
}
}
return localIP;
}
void MainWindow::on_clearBtn_clicked()
{
ui->plainTextEdit->clear();
}
void MainWindow::on_quitBtn_clicked()
{
this->close();
}