代码部分:
#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();
void on_bindBtn_clicked();
void on_stopBtn_clicked();
void on_btnSend_clicked();
void on_clearBtn_clicked();
void on_quitBtn_clicked();
private:
QLabel* LabSocketState;
QUdpSocket* udpSocket;
QString getLocalIp();
QHostAddress groupAddress;
private:
Ui::MainWindow *ui;
};
#endif
.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();
this->setWindowTitle(this->windowTitle()+"---本机IP"+localIP);
udpSocket = new QUdpSocket;
udpSocket->setSocketOption(QAbstractSocket::MulticastTtlOption,1);
connect(udpSocket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(onSocketStateChange(QAbstractSocket::SocketState)));
onSocketStateChange(udpSocket->state());
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(onSocketReadyRead()));
}
void MainWindow::on_bindBtn_clicked()
{
QString IP = ui->comboTargetIp->currentText();
groupAddress = QHostAddress(IP);
quint16 groupPort = ui->spinBox->value();
if(udpSocket->bind(QHostAddress::AnyIPv4,groupPort,QUdpSocket::ShareAddress))
{
udpSocket->joinMulticastGroup(groupAddress);
ui->plainTextEdit->appendPlainText("**加入组播成功");
ui->plainTextEdit->appendPlainText("组播地址IP: "+IP);
ui->plainTextEdit->appendPlainText("**绑定端口:"+QString::number(groupPort));
ui->bindBtn->setEnabled(false);
ui->stopBtn->setEnabled(true);
ui->comboTargetIp->setEnabled(true);
}
else
{
ui->plainTextEdit->appendPlainText("**绑定端口失败");
}
}
void MainWindow::on_stopBtn_clicked()
{
udpSocket->leaveMulticastGroup(groupAddress);
udpSocket->abort();
ui->bindBtn->setEnabled(true);
ui->stopBtn->setEnabled(false);
ui->comboTargetIp->setEnabled(true);
ui->plainTextEdit->appendPlainText("**已退出组播,解除端口绑定");
}
void MainWindow::on_btnSend_clicked()
{
quint16 groupPort = ui->spinBox->value();
QString msg = ui->lineEdit->text();
QByteArray array;
array = msg.toUtf8();
udpSocket->writeDatagram(array,groupAddress,groupPort);
ui->plainTextEdit->appendPlainText("[multicst]"+msg);
ui->lineEdit->clear();
ui->lineEdit->setFocus();
}
void MainWindow::on_clearBtn_clicked()
{
ui->plainTextEdit->clear();
}
void MainWindow::on_quitBtn_clicked()
{
this->close();
}
void MainWindow::onSocketReadyRead()
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
QHostAddress peerAddr;
quint16 peerPort;
udpSocket->readDatagram(datagram.data(),datagram.size(),&peerAddr,&peerPort);
QString str =datagram.data();
QString peerStr = "[From"+peerAddr.toString()+":"+QString::number(peerPort)+"]";
ui->plainTextEdit->appendPlainText(peerStr);
}
QString MainWindow::getLocalIp()
{
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::onSocketStateChange(QAbstractSocket::SocketState socketState)
{
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;
}
}
}
MainWindow::~MainWindow()
{
delete ui;
udpSocket = NULL;
delete udpSocket;
}
仅作为个人学习记录
|