QUdpSocket Echo Server : Client Recv Problem
-
I am writing a simple udp echo server, but have problem about how to recv data in udp client.
Below is the UDP Server main logic:@
m_oServerSocket = new QUdpSocket(this);
m_oServerSocket->connect(m_oServerSocket, SIGNAL(readyRead()), this, SLOT(__onServerRecv()));
m_oServerSocket->bind(QHostAddress::Any, sPort.toInt());
@and in "__onServerRecv()":
@
while(m_oServerSocket->hasPendingDatagrams())
{QByteArray datagram;
datagram.resize(m_oServerSocket->pendingDatagramSize());QHostAddress sender;
quint16 senderPort;
m_oServerSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);QDateTime dt = QDateTime::currentDateTime();
QString sDtString = dt.toString("yyyy-MM-dd HH:mm:ss");
QString sLog = ui->txtServerLog->toPlainText();
sLog = sDtString + " : " + QString::fromUtf8(datagram) + "\n" + sLog;
ui->txtServerLog->setPlainText(sLog);m_oServerSocket->writeDatagram(datagram, sender, senderPort);
}
@I think the Server's Code is OK because I write a simple python client and it works perfectly.But when I write my QUdpSocket Client:
@
m_oClientSocket = new QUdpSocket(this);
m_oClientSocket ->connect(m_oClientSocket , SIGNAL(readyRead()), this, SLOT(__onClientRecv()));QString sSendMsg = ui->txtClientSend->text();
m_oClientSocket->writeDatagram(sSendMsg.toUtf8(), QHostAddress::LocalHost, sPort.toInt());
@the slot "__onClientRecv" is never called, and it means that the client's signal "readyRead" is never triggerred.
How should I do ?
-
You need to call "QAbstractSocket::connectToHost":http://qt-project.org/doc/qt-4.8/qabstractsocket.html#connectToHost function at your client application and you will be fine.
-
-
[quote author="favoritas37" date="1332196902"]You need to call "QAbstractSocket::connectToHost":http://qt-project.org/doc/qt-4.8/qabstractsocket.html#connectToHost function at your client application and you will be fine.[/quote]
It's OK.
But I think the Qt's Socket Api is much different from typical C socket api. I mean there is no api corresponding to connectToHost in C socket.