QUdpSocket not send but No Error
-
@w-tkm said in QUdpSocket not send but No Error:
but no Send
You did not post any code where you send anything...
-
@KroMignon said in QUdpSocket not send but No Error:
@w-tkm said in QUdpSocket not send but No Error:
m_pUdpSendSock->connectToHost( udpSendIP, udpSendPort);
UDP is not an unicast protocol, there is no "connection".
connectToHost()
is for TCP sockets.Did you mean connected rather than unicast?
UDP sockets can be used with connect() in many frameworks to prespecify the remote address. For example:
The Connect method establishes a default remote host using the value specified in the endPoint parameter. Once established, you do not have to specify a remote host in each call to the Send method.
-
The part to send is as follows
void debugUdp::fnUdpSendtoWifiOrq2(stringstream &sendStream) { quint32 iSendSize; iSendSize = static_cast<quint32>(sendStream.tellp()); memcpy(pucSendBuffer, sendStream.str().data(), static_cast<quint32>(iSendSize)); m_pUdpSendSocket->write(reinterpret_cast<char*>(pucSendBuffer), static_cast<qint64>(iSendSize)); }
I don't seem to have a problem here.
Because once I disconnect and reconnect, it will start transmitting normally. -
As a prerequisite, remember that I can close the socket and reopen it to send.
Also, the problem of not being able to communicate only occurs when the application starts, and can be solved by opening and closing the socket. -
@w-tkm You should add error handling to your app (https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred, https://doc.qt.io/qt-5/qabstractsocket.html#stateChanged).
-
@w-tkm said in QUdpSocket not send but No Error:
I didn't get any errors, and there was no problem with the socket status.
Did you check the return value of
m_pUdpSendSocket->write()
?
I would suggest you to try with:m_pUdpSendSocket->writeDatagram(reinterpret_cast<char*>(pucSendBuffer), static_cast<qint64>(iSendSize), QHostAddress( m_strLocalIp ) , m_numConnectPort);
EDIT: of course before doing this you should remove
'm_pUdpSendSock->connectToHost()
to avoid errors. -
@KroMignon
Using writeDatagram, I was able to send it.
On top of that, there are four things that concern me.- I forgot to remove m_pUdpSendSock->connectToHost(), but I could send.
- The return value of m_pudpSendSocket->write() was normal
- I actually have another Send socket with the same structure but different IP, port and VLAN, but that one was fine.
- When I was using m_pUdpSendSocket->write(), I solved the problem by setting the metric in the network settings.
I may be confusing you more and more, but what is the cause of all this?
-
@w-tkm said in QUdpSocket not send but No Error:
Using writeDatagram, I was able to send it.
On top of that, there are four things that concern me.- I forgot to remove m_pUdpSendSock->connectToHost(), but I could send.
=> is not mandatory, but as written in documentation, writeDatagram() may not work in combination with connectToHost(). So it is better to not use booth with same QUdpStocket.
- The return value of m_pudpSendSocket->write() was normal
- I actually have another Send socket with the same structure but different IP, port and VLAN, but that one was fine.
- When I was using m_pUdpSendSocket->write(), I solved the problem by setting the metric in the network settings.
I may be confusing you more and more, but what is the cause of all this?
With
m_pUdpSendSocket->writeDatagram()
you always specify the destination, so it will always try to resolve destination to get MAC.With
m_pUdpSendSock->connectToHost()
the name resolution is only done once, and store locally. So when result is invalid, no chance to send data to destination!It look like your network configuration is not totally clean and the TCP/IP stack can not resolve correctly this destination.