Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

UDP communication using static library and test program



  • I am trying to create a UDP communication using static library and test program using UDp protocol. I have taken a QScopedPointer of QUDPSocket class. I am resetting the data in my test program. Everything is working fine except I am facing some issues with the writedatagram function. I am not sure about the syntax of the writedatagram method . Below is my code snippet

    m_UdpComm.reset(new QUdpSocket);
    QString word="Hello UDP server";
    QByteArray buffer;
    buffer.resize(m_UdpComm.data()->pendingDatagramSize());
    buffer=word.toUtf8();
    m_UdpComm.data()->writeDatagram(buffer.data(),p_proConfig.p_ipAddress,p_proConfig.p_port);
    qDebug()<<p_proConfig.p_ipAddress;
    qDebug()<<p_proConfig.p_port;
    

    The declarations:

    struct ProAxeSEConfig
    {
        //list of members of ProAxeSEConfig structure
        QString p_ipAddress;
        quint16 p_port;
    };
    ProAxeSEConfig p_proConfig;
    ProAxeSE(ProAxeSEConfig p_proConfig);
    QScopedPointer<QUdpSocket> m_UdpComm;
    

    I am facing issues in the writedatagram statement



  • @supratik123 Hi @supratik123, before sending datagram with an UDP socket, you have to bind the socket to a local UDP port.

     m_UdpComm.reset(new QUdpSocket);
     m_UdpComm->bind(QHostAddress::LocalHost, 0); // use 0 to let TCP/IP stack select the first free UDP port
    

    Please have a look at the Qt documentation: QUdpSocket Class

    [Edit aha_1980: Fixed m_UdpComm.->bind]



  • @KroMignon Hi KroMignon, I try to understand this, but since this is my client side and just want one side i.e half duplex connection, so do it matter to bind my client socket. I have used bind in my server side and it is working fine. Qt is giving me no matching function call for writeDatagram() error.



  • @supratik123 What do you want to know? If it is the last error use QUdpSocket::error() to get the error code or QUdpSocket::errorString() as it is describe in the help QUdpSocket::writeDatagram()



  • @KroMignon Okay Thanks For the help.



  • @supratik123 is your issue solved? if so please mark your post as such!. Thanks.


Log in to reply