作者chigi ( )
看板C_and_CPP
標題[問題][QT] UDP socket hang住
時間Thu Sep 24 13:53:15 2015
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
QT
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
QT network
問題(Question):
我計畫要用UDP receiver收一些data,
然後開另一個UDP的socket把資料整理之後送出去
大部分的code都從UDP receiver example改的,
主要是多加了一個UDP socket作為sender,
然後當receiver 收到資料readready後就把資料拿出來塞進UDP sender扔出去
不過隨機會掛掉,receiver就收不到資料了,
沒有readready的信號也不會繼續做下去
求解.. 可能是什麼環節錯了呢?
程式碼(Code):(請善用置底文網頁, 記得排版)
QUdpSocket *udpSocket;
QUdpSocket *udpSocketsender;
此二參數都已經initialize過。
void Receiver::processPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size());
udpSocketsender->writeDatagram(datagram,
QHostAddress("239.192.43.78"), 4378);
/*savefile = new QFile(tr("packetsaved_%1.dat").arg(index));
if (!savefile->open(QIODevice::WriteOnly)) return;
savefile->write(datagram.data(), datagram.size());
savefile->close();
delete savefile;*/
}
}
補充說明(Supplement):
如code所示,
如果我把sender註解調改換成寫到檔案中,
就不會卡住,但是如果是從網路send出去,
不定時就會停住,長短不一定
p.s.
我有去查過udpSocket的status
udpSocket.status() = 4(QAbstractSocket::BoundState)
麻煩各位...
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.135.21.58
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1443073998.A.C59.html
→ johnhjwu: 你用multicast ? 09/25 21:39
→ johnhjwu: 如果用unicast address收送會有問題嗎? 09/28 06:30
→ chigi: 沒有試過誒@@ 想說既然是multicast了就沒再測這個 09/30 16:50