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. How to get the socket from a socket error signal
QtWS25 Last Chance

How to get the socket from a socket error signal

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 3.1k 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.
  • M Offline
    M Offline
    Magnus21
    wrote on last edited by
    #1

    Hi, so I wrote a program that has to deal with up to 16 TCP connections simultaneously. It runs a QTcpServer and as soon as clients connect to it, it accepts the connection and saves the QTcpSocket to an array of QTcpSockets, in case it has to send stuff back. Since I have multiple sockets, I have to connect up to 16 signals from the sockets (I handle both readyRead and error) to the very same slot (tcpReadyRead() and tcpSocketError).
    But since the signals don't have the sender as a parameter, I cannot really determine which of the possible 16 connected QTcpSockets emitted the signal (but I need either the index or a pointer to the QTcpSocket for what I need to do). So how can I get a pointer to the sender of that signal, or is there a better way to handle it than I do?
    Here is the code btw, if it helps understanding what I mean:
    Line where I connect the signal to the slot
    Line of the slot where I need to get a pointer to the sender (the QTcpSocket)

    Thanks for any help :)

    Also feel free to propose another solution. The way I see it there is no other solution than to connect multiple signals to one slot (which is the source of this problem), however I think manually creating 16 slots for the very same event (which would make it possible to determine the sender) would be way to dirty coding...

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Magnus21
      wrote on last edited by
      #2

      Oh never mind, I forgot that I am able to connect the signal to the slot of another class, so I can just create a special class in which I save the socket as well as connecting the signal to a slot in that class.
      I will mark this as solved then, I guess...

      kshegunovK 1 Reply Last reply
      1
      • M Magnus21

        Oh never mind, I forgot that I am able to connect the signal to the slot of another class, so I can just create a special class in which I save the socket as well as connecting the signal to a slot in that class.
        I will mark this as solved then, I guess...

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        What about using the one that's already there QSignalMapper?
        I mean there's nothing wrong in writing your own, but you could make use of the existing code.

        Read and abide by the Qt Code of Conduct

        M 1 Reply Last reply
        2
        • kshegunovK kshegunov

          What about using the one that's already there QSignalMapper?
          I mean there's nothing wrong in writing your own, but you could make use of the existing code.

          M Offline
          M Offline
          Magnus21
          wrote on last edited by
          #4

          @kshegunov Wow, thanks, I didn't know that this exists.
          Definetely going to try this :)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Magnus21
            wrote on last edited by Magnus21
            #5

            @kshegunov The error signal is not parameterless (It has a parameter with type QAbstractSocket::SocketError). So I cannot use QSignalMapper in this case, right?

            kshegunovK 1 Reply Last reply
            0
            • M Magnus21

              @kshegunov The error signal is not parameterless (It has a parameter with type QAbstractSocket::SocketError). So I cannot use QSignalMapper in this case, right?

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              You can use it if the signal has a string or int parameter. But I don't remember if there are other overloads, however I doubt there's one specifically for socket errors.

              Read and abide by the Qt Code of Conduct

              M 1 Reply Last reply
              0
              • kshegunovK kshegunov

                You can use it if the signal has a string or int parameter. But I don't remember if there are other overloads, however I doubt there's one specifically for socket errors.

                M Offline
                M Offline
                Magnus21
                wrote on last edited by
                #7

                @kshegunov QAbstractSocket::SocketError is an enumeration - thus trivially typecastable to an int.
                So you said, it is possible to use it if the parameter is an int. Do you mind elaborating on how I can use the signal with an int as a parameter? I don't see it in the documentation how this could be achieved.

                kshegunovK 1 Reply Last reply
                0
                • M Magnus21

                  @kshegunov QAbstractSocket::SocketError is an enumeration - thus trivially typecastable to an int.
                  So you said, it is possible to use it if the parameter is an int. Do you mind elaborating on how I can use the signal with an int as a parameter? I don't see it in the documentation how this could be achieved.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @Magnus21 said in How to get the socket from a socket error signal:

                  So you said, it is possible to use it if the parameter is an int.

                  Sorry I misled you. You can map a single thing - either int, QString, QObject *, but not both a int and QObject.

                  Read and abide by the Qt Code of Conduct

                  M 1 Reply Last reply
                  0
                  • kshegunovK kshegunov

                    @Magnus21 said in How to get the socket from a socket error signal:

                    So you said, it is possible to use it if the parameter is an int.

                    Sorry I misled you. You can map a single thing - either int, QString, QObject *, but not both a int and QObject.

                    M Offline
                    M Offline
                    Magnus21
                    wrote on last edited by
                    #9

                    @kshegunov I don't need to map two things, I only need to map the QObject *. The int is passed as a parameter in the actual signal (and can not even be used for mapping, because it is not unique to an instance). However, as I understand it, there is no such way to use a non-parameterless signal. So either I have to reimplement QSignalMapper, or I drop the parameter and regain the parameter in the actual last slot, by calling error() on the tcpsocket to get the last error.
                    Anyways, thanks for helping, I still got to use QSignalMapper for another thing, so thanks for that :)

                    kshegunovK 1 Reply Last reply
                    0
                    • M Magnus21

                      @kshegunov I don't need to map two things, I only need to map the QObject *. The int is passed as a parameter in the actual signal (and can not even be used for mapping, because it is not unique to an instance). However, as I understand it, there is no such way to use a non-parameterless signal. So either I have to reimplement QSignalMapper, or I drop the parameter and regain the parameter in the actual last slot, by calling error() on the tcpsocket to get the last error.
                      Anyways, thanks for helping, I still got to use QSignalMapper for another thing, so thanks for that :)

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by kshegunov
                      #10

                      To be completely frank, I'd just have a session object where I have the socket and whatever's needed for the communication. So your original question is a bit unusual from my perspective. For example you could take a peek here and here.

                      Read and abide by the Qt Code of Conduct

                      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