[問題] Qt UdpSocket receiver 指定IP 收不到
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
QT
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
N/A
問題(Question):
嘗試用Qt Network 作UDP指定IP與PORT的封包傳送,
若是用預設IP可以順利收送,但是指定IP後就只能送不能收
作為對照有另一隻第三方的APP可以收送指定IP與PORT的封包。
預期的正確結果(Expected Output):
正常封包收送
程式碼(Code):(請善用置底文網頁, 記得排版)
line 55, receiver.cpp
udpSocket = new QUdpSocket(this);
//udpSocket->bind(QHostAddress::Broadcast, 45454);
udpSocket->bind(QHostAddress("239.255.43.12"), 4312,
QUdpSocket::ReuseAddressHint);
line 88, sender.cpp
udpSocket->writeDatagram(datagram.data(), datagram.size(),
QHostAddress("239.192.43.12"), 4312);
補充說明(Supplement):
使用的code同 Qt Example Udp sender / receiver,
有修改的部分我寫在上面
實際執行結果是,sender送的資料第三方的receiver可以收,
但是無論sender或第三方的sender送的資料,自己的receiver都收不到。
請問有哪邊要特別調整才可以收到嗎?
p.s. 無防火牆
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.135.21.58
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1442399043.A.5EA.html
推
09/16 20:31, , 1F
09/16 20:31, 1F
→
09/16 23:46, , 2F
09/16 23:46, 2F
→
09/16 23:46, , 3F
09/16 23:46, 3F
自問自答
先上結論,用UdpSocket::joinMulticastGroup(const QHostAddress &)
設定IP,bind裡面的IP address是Device IP,也就是媒體
最後改成
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::AnyIPv4, 4312,
QUdpSocket::ReuseAddressHint);
udpSocket->joinMulticastGroup(QHostAddress("239.192.43.12"));
就可以用了。
※ 編輯: chigi (220.135.21.58), 09/17/2015 10:10:06
推
09/18 07:55, , 4F
09/18 07:55, 4F
→
09/18 12:16, , 5F
09/18 12:16, 5F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章