Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. UDP communication using static library and test program
Forum Updated to NodeBB v4.3 + New Features

UDP communication using static library and test program

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 660 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    supratik123
    wrote on last edited by
    #1

    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

    KroMignonK 1 Reply Last reply
    0
    • S supratik123

      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

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by aha_1980
      #2

      @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]

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      S 1 Reply Last reply
      3
      • KroMignonK KroMignon

        @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]

        S Offline
        S Offline
        supratik123
        wrote on last edited by
        #3

        @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.

        KroMignonK 1 Reply Last reply
        0
        • S supratik123

          @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.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @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()

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          S 1 Reply Last reply
          2
          • KroMignonK KroMignon

            @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()

            S Offline
            S Offline
            supratik123
            wrote on last edited by
            #5

            @KroMignon Okay Thanks For the help.

            Pablo J. RoginaP 1 Reply Last reply
            0
            • S supratik123

              @KroMignon Okay Thanks For the help.

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

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

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved