我对QUdpSocket
有疑问。我想创建一个简单的程序来使用UDP protokol发送和接收数据。我已经阅读了很多类似的主题,但我没有找到解决方案。通信仅适用于QHostAdress::LocalHost
,然后我提供与send相同的数据,但是如果我想将数据发送到外部设置具体地址,例如194.181.161.134
,则无效。这意味着数据已发送但我无法接收。这是我的代码:
class Okno_GL : public QMainWindow
{
Q_OBJECT
public:
explicit Okno_GL(QWidget *parent = 0);
QWidget *wg;
QPushButton *pb;
QPushButton *pl;
QGridLayout *gr;
QUdpSocket *socket;
QHostAddress host;
QHostAddress bcast;
signals:
public slots:
void SLOT_Write();
void SLOT_load();
};
class Receiver : public QObject
{
Q_OBJECT
public:
Receiver();
QUdpSocket *udpSocket;
public slots:
void SLOT_processPendingDatagrams();
void SLOT_StCh(QAbstractSocket::SocketState state);
};
Okno_GL::Okno_GL(QWidget *parent) :
QMainWindow(parent)
{
pb = new QPushButton("write" , this);
pl = new QPushButton("read" , this);
wg = new QWidget(this);
setCentralWidget(wg);
gr = new QGridLayout(wg);
gr->addWidget(pb);
gr->addWidget(pl);
socket = new QUdpSocket(this);
connect(pb , SIGNAL(clicked()) , SLOT(SLOT_Write()));
connect(pl , SIGNAL(clicked()) , SLOT(SLOT_load()));
}
void Okno_GL::SLOT_Write()
{
QByteArray datagram = "gS";
int send;
send = socket->writeDatagram(datagram.data(), QHostAddress("194.181.161.134"), 1200);
}
void Okno_GL::SLOT_load()
{
}
Receiver::Receiver()
{
udpSocket = new QUdpSocket(this);
connect(udpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)) , this , SLOT(SLOT_StCh(QAbstractSocket::SocketState)));
if(udpSocket->bind(QHostAddress::Any , 1200))
{
qd "bind";
}
else
{
qd "not bind";
}
}
void Receiver::SLOT_processPendingDatagrams()
{
qd "receiver";
QByteArray datagram;
do {
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size());
} while (udpSocket->hasPendingDatagrams());
qd "datagram" << datagram;
}
void Receiver::SLOT_StCh(QAbstractSocket::SocketState state)
{
qd "slot" << state;
QByteArray datagram = "gS";
if ( state == QAbstractSocket::BoundState ) {
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(SLOT_processPendingDatagrams()) , Qt::QueuedConnection);
}
}
答案 0 :(得分:1)
您应该使用write()
代替writeDatagram()
qint64 QUdpSocket :: writeDatagram(const char * data,qint64 size,const QHostAddress&amp; address,quint16 port)
警告:在连接的UDP套接字上调用此函数可能会导致 错误,没有发送数据包。如果您使用连接 socket,使用write()发送数据报。
答案 1 :(得分:1)
所以,我会根据你的命题重建我的代码。我在construktor类Okno_GL中添加了这一行。
socket = new QUdpSocket(this);
socket->connectToHost(QHostAddress("194.181.161.134") , 1200);
这是要发送的广告
void Okno_GL::SLOT_Write()
{
QByteArray datagram = "gS";
int send;
// send = socket->writeDatagram(datagram.data(), QHostAddress("194.181.161.134"), 1200);
send = socket->write(datagram);
qd " send" << send;
}
接收器代码没有改变,所以我仍然是白色的信号readRedy(),我发送数据后没有得到他。
PS。我想补充一点,我有其他选择connetc,非常非常简单的文件命令* bat whitch连接这台主机没有任何问题。这证明我的网络或管理员没有阻止(端口,IP);
感谢您的关注
问题解决了!这是一个非常愚蠢的错误。我应该发送3 bajt而不是2. 3 bajt它是0x0D。 以下代码是工作
socket = new QUdpSocket(this);
socket->bind(QHostAddress::Any, 1200);
并发送
QByteArray datagram = "gS";
datagram.append(0x0D);
int send;
send = socket->writeDatagram(datagram.data(), QHostAddress("194.181.161.134"), 1200);
并在信号readyRead()
之后接收qd "receiver";
QByteArray datagram;
do {
datagram.resize(socket->pendingDatagramSize());
socket->readDatagram(datagram.data(), datagram.size());
} while (socket->hasPendingDatagrams());
qd "datagram" << datagram;
非常帮助mi wireshark。谢谢大家