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 : How to send a response to a client

UDP : How to send a response to a client

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 11.6k Views
  • 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
    sayem.bd
    wrote on 30 Dec 2011, 10:18 last edited by
    #1

    Hi

    I want to write a UDP server which will listen for requests coming from various UDP clients. After receiving a request it will send a response to that client. How can I do that in Qt?

    I am binding a QUdpSocket instance to a specific port to listen for client requests. As a resut when a request arrives the readyread signal is emitted. I can then read the request in the corresponding slot method. But now to send a response I need to know client's address and port. How can I get that?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on 30 Dec 2011, 10:31 last edited by
      #2

      AFAIK it would be better to make the request through TCP/IP since you know it will be received.
      UDP is more a one transmission without checking of reception on the other end ("see also":http://en.wikipedia.org/wiki/User_Datagram_Protocol#Comparison_of_UDP_and_TCP ).

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 30 Dec 2011, 10:37 last edited by
        #3

        UDP has its uses, and without knowing the use case one cannot advise to use TCP instead of UDP.

        The point of UDP is that you basically just send of a bit of information to another IP at a certain port, and that's it. There is no connection, so also no back channel. If you want to reply, your client will also need to be listening on a certain port, and your server can send the UDP response back to that port. You could choose a fixed port to listen on, or you could make the client send a port number it will be listening on in one of the messages to the server.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sayem.bd
          wrote on 30 Dec 2011, 10:37 last edited by
          #4

          [quote author="koahnig" date="1325241071"]AFAIK it would be better to make the request through TCP/IP since you know it will be received.
          UDP is more a one transmission without checking of reception on the other end ("see also":http://en.wikipedia.org/wiki/User_Datagram_Protocol#Comparison_of_UDP_and_TCP ). [/quote]

          The client doesn't know that the request will be received for sure. Similarly, the server also doesn't know that a client request will arrive. But even if a single client request arrives at server, then the server must send a response back to the client. This is why I want to know if there is any way to get the client address and port.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on 30 Dec 2011, 10:45 last edited by
            #5

            [quote author="sayem.bd" date="1325241455"]
            The client doesn't know that the request will be received for sure. Similarly, the server also doesn't know that a client request will arrive. But even if a single client request arrives at server, then the server must send a response back to the client. This is why I want to know if there is any way to get the client address and port.[/quote]

            Well, there are apparently these "access methods":http://developer.qt.nokia.com/doc/qt-4.8/qabstractsocket.html#peerAddress (also for local address and ports) available.

            @Andre I wonder if it is a good strategy to use UDP instead of TCP/IP for forward and backward communication. At the day's end you might have to establish certain checks simulating what is already part of TCP/IP.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on 30 Dec 2011, 10:48 last edited by
              #6

              True enough, it may be better to use TCP, but without more information on the use case, that is not possible to decide.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sayem.bd
                wrote on 30 Dec 2011, 10:50 last edited by
                #7

                Thanks Andre, your answer explained a lot of things.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on 30 Dec 2011, 10:55 last edited by
                  #8

                  [quote author="Andre" date="1325242094"]True enough, it may be better to use TCP, but without more information on the use case, that is not possible to decide.[/quote]

                  You are right there might be justifications for UDP in certain use cases.

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on 30 Dec 2011, 10:55 last edited by
                    #9

                    "QUdpSocket::readDatagram() ":/doc/qt-4.8/qudpsocket.html#readDatagram takes optional pointers to a [[Doc:QHostAddress]] for the address and a quint for the port.

                    bq. Receives a datagram no larger than maxSize bytes and stores it in data. The sender's host address and port is stored in *address and *port (unless the pointers are 0).

                    Use it to send your data back to the sender.

                    In Qt, using [[Doc:QUdpSocket]] you can sort of connect between the server and the client and use [[Doc:QIODevice]]'s read() and write() methods. This is not a real connection like in using TCP, but kind of a "virtual connection".

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0

                    2/9

                    30 Dec 2011, 10:31

                    7 unread
                    • Login

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