How to get the socket from a socket error signal

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

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

  • Moderators

    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.

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

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

  • Moderators

    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.

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

  • Moderators

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

  • @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 :)

  • Moderators

    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.

Log in to reply