#include <QDialog>
#include <QUdpSocket>
namespace Ui {
class Dialog;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
private:
Ui::Dialog *ui;
public slots:
void recv_uni_udp(void);
void recv_uni_qio(void);
void recv_broad_udp(void);
void recv_broad_qio(void);
void btn_uni_udp(void);
void btn_uni_qio(void);
void btn_broad_udp(void);
void btn_broad_qio(void);
};
#define UNICASTADDR "192.168.155.1"
#define BROADCASTADDR "192.168.155.255"
ui(new Ui::Dialog)
{
ui->setupUi(this);
sock_uni_udp->bind();
str.clear();
txtstream << "bound to " << sock_uni_udp->localPort();
ui->uni_udp_port->setText(str);
connect(sock_uni_udp, SIGNAL(readyRead()),this, SLOT(recv_uni_udp()));
sock_broad_udp->bind();
str.clear();
txtstream << "bound to " << sock_broad_udp->localPort();
ui->broad_udp_port->setText(str);
connect(sock_broad_udp, SIGNAL(readyRead()),this, SLOT(recv_broad_udp()));
sock_broad_qio->bind();
str.clear();
txtstream << "bound to " << sock_broad_qio->localPort();
ui->broad_qio_port->setText(str);
connect(sock_broad_qio, SIGNAL(readyRead()),this, SLOT(recv_broad_qio()));
sock_uni_qio->bind();
str.clear();
txtstream << "bound to " << sock_uni_qio->localPort();
ui->uni_qio_port->setText(str);
connect(sock_uni_qio, SIGNAL(readyRead()),this, SLOT(recv_uni_qio()));
connect(ui->uni_udp_api_btn, SIGNAL(clicked()),this,SLOT(btn_uni_udp()));
connect(ui->uni_qio_api_btn, SIGNAL(clicked()),this,SLOT(btn_uni_qio()));
connect(ui->broad_udp_api_btn, SIGNAL(clicked()),this,SLOT(btn_broad_udp()));
connect(ui->broad_qio_api_btn, SIGNAL(clicked()),this,SLOT(btn_broad_qio()));
}
Dialog::~Dialog()
{
delete sock_uni_udp;
delete sock_uni_qio;
delete sock_broad_udp;
delete sock_broad_qio;
delete ui;
}
void Dialog::btn_uni_udp(void)
{
}
void Dialog::btn_uni_qio(void)
{
sock_uni_qio->write("hallo");
}
void Dialog::btn_broad_udp(void)
{
}
void Dialog::btn_broad_qio(void)
{
sock_broad_qio->write("hallo");
}
void Dialog::recv_uni_udp(void)
{
char str[5];
sock_uni_udp->readDatagram(str, 5);
ui->uni_udp_recv->setText("arrived");
}
void Dialog::recv_uni_qio(void)
{
ba = sock_uni_udp->readAll();
ui->uni_qio_recv->setText("arrived");
}
void Dialog::recv_broad_udp(void)
{
char str[5];
sock_broad_udp->readDatagram(str, 5);
ui->broad_udp_recv->setText("arrived");
}
void Dialog::recv_broad_qio(void)
{
ba = sock_broad_qio->readAll();
ui->broad_qio_recv->setText("arrived");
}
#include <QDialog>
#include <QUdpSocket>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
QUdpSocket *sock_uni_udp;
QUdpSocket *sock_uni_qio;
QUdpSocket *sock_broad_udp;
QUdpSocket *sock_broad_qio;
public slots:
void recv_uni_udp(void);
void recv_uni_qio(void);
void recv_broad_udp(void);
void recv_broad_qio(void);
void btn_uni_udp(void);
void btn_uni_qio(void);
void btn_broad_udp(void);
void btn_broad_qio(void);
};
#define UNICASTADDR "192.168.155.1"
#define BROADCASTADDR "192.168.155.255"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
sock_uni_udp = new QUdpSocket(this);
sock_uni_qio = new QUdpSocket(this);
sock_broad_udp = new QUdpSocket(this);
sock_broad_qio = new QUdpSocket(this);
QString str;
QTextStream txtstream(&str);
sock_uni_udp->bind();
str.clear();
txtstream << "bound to " << sock_uni_udp->localPort();
ui->uni_udp_port->setText(str);
connect(sock_uni_udp, SIGNAL(readyRead()),this, SLOT(recv_uni_udp()));
sock_broad_udp->bind();
str.clear();
txtstream << "bound to " << sock_broad_udp->localPort();
ui->broad_udp_port->setText(str);
connect(sock_broad_udp, SIGNAL(readyRead()),this, SLOT(recv_broad_udp()));
sock_broad_qio->bind();
sock_broad_qio->connectToHost(QHostAddress(QString(BROADCASTADDR)),44444);
str.clear();
txtstream << "bound to " << sock_broad_qio->localPort();
ui->broad_qio_port->setText(str);
connect(sock_broad_qio, SIGNAL(readyRead()),this, SLOT(recv_broad_qio()));
sock_uni_qio->bind();
sock_uni_qio->connectToHost(QHostAddress(QString(UNICASTADDR)),44444);
str.clear();
txtstream << "bound to " << sock_uni_qio->localPort();
ui->uni_qio_port->setText(str);
connect(sock_uni_qio, SIGNAL(readyRead()),this, SLOT(recv_uni_qio()));
connect(ui->uni_udp_api_btn, SIGNAL(clicked()),this,SLOT(btn_uni_udp()));
connect(ui->uni_qio_api_btn, SIGNAL(clicked()),this,SLOT(btn_uni_qio()));
connect(ui->broad_udp_api_btn, SIGNAL(clicked()),this,SLOT(btn_broad_udp()));
connect(ui->broad_qio_api_btn, SIGNAL(clicked()),this,SLOT(btn_broad_qio()));
}
Dialog::~Dialog()
{
delete sock_uni_udp;
delete sock_uni_qio;
delete sock_broad_udp;
delete sock_broad_qio;
delete ui;
}
void Dialog::btn_uni_udp(void)
{
sock_uni_udp->writeDatagram("hallo",5, QHostAddress(QString(UNICASTADDR)),44444);
}
void Dialog::btn_uni_qio(void)
{
sock_uni_qio->write("hallo");
}
void Dialog::btn_broad_udp(void)
{
sock_broad_udp->writeDatagram("hallo",5, QHostAddress(QString(BROADCASTADDR)),44444);
}
void Dialog::btn_broad_qio(void)
{
sock_broad_qio->write("hallo");
}
void Dialog::recv_uni_udp(void)
{
char str[5];
sock_uni_udp->readDatagram(str, 5);
ui->uni_udp_recv->setText("arrived");
}
void Dialog::recv_uni_qio(void)
{
QByteArray ba;
ba = sock_uni_udp->readAll();
ui->uni_qio_recv->setText("arrived");
}
void Dialog::recv_broad_udp(void)
{
char str[5];
sock_broad_udp->readDatagram(str, 5);
ui->broad_udp_recv->setText("arrived");
}
void Dialog::recv_broad_qio(void)
{
QByteArray ba;
ba = sock_broad_qio->readAll();
ui->broad_qio_recv->setText("arrived");
}
To copy to clipboard, switch view to plain text mode
max@kinets-dev:~$ netstat -nupa
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0 0 192.168.155.100:53527 192.168.155.1:44444 ESTABLISHED 4405/test-broadcast
udp 0 0 192.168.155.100:50986 192.168.155.255:44444 ESTABLISHED 4405/test-broadcast
udp6 0 0 :::50762 :::* 4405/test-broadcast
udp6 0 0 :::42704 :::* 4405/test-broadcast
max@kinets-dev:~$ netstat -nupa
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0 0 192.168.155.100:53527 192.168.155.1:44444 ESTABLISHED 4405/test-broadcast
udp 0 0 192.168.155.100:50986 192.168.155.255:44444 ESTABLISHED 4405/test-broadcast
udp6 0 0 :::50762 :::* 4405/test-broadcast
udp6 0 0 :::42704 :::* 4405/test-broadcast
To copy to clipboard, switch view to plain text mode
In the first two lines I used connectToHost() api and the connection state is established. The only one that cannot receive udp datagrams is the second. My thought is that since the connection is established it can receive packet only from 192.168.155.255, but when the peer reply it use its own unicast address so the packet is discarded.
Bookmarks