Important: Please read the Qt Code of Conduct -

[Solved] QLocalSocket creates 2 connections on 1 connectToServer

  • Hello

    I have a daemon that acts as Unix Domain Sockets Server and it is in state listening.
    When I connect to this server by
    socket->connectToServer(socket_path, QIODevice::ReadWrite);
    it connects twice. The console output from the server shows that accept is run twice and netstat shows me the listening and 2 connected sockets.

    I am new to socket communication. Is this normal behavior?

    In addition some basics questions:
    The Qt app needs to talk very frequently to the server process on this device. Is it best practice to leave the connection established for the lifetime of these processes (reboot of the device) or is it better to close and reopen for every communication?
    Use a separate thread on client(Qt) / server(c app) for the socket communication?

    Thanks for your support.

  • Some suggestions:

    1. Look for a network sniffer (wireshark, packetyzer) that can show you the network traffic to figure out what is happening on the line.

    2a. If you want to realise much throughput then avoid overhead things like opening/closing sockets.

    2b. I would prefer separate threads. Normally such applications are built using pre-threading or with threads created in time when the connection has been established. It depends on the system you are working on.

  • Thanks for 2a and 2b.
    To 1.: Unix Domain sockets are used for internal IPC only and don't go out to the network. The principles are the same though.

    I am working on a embedded device with an ARM core. The process acting as server is written in native c and controls the chip specific HW parts like video decoder and stuff.
    The Qt app is used as GUI and for handling of external (serial) operation commands. Of course, there is a lot of communication between these 2 processes.

  • I do not know much about unix, but could it be that Unix Domain Sockets indeed use two channels to communicate?

  • Found the issue:
    I did't notice that I don't have to connect the button click manually to the _clicked function if I use the Slot function from the design window to create and jump to _clicked function and crated a connect in the code as well.
    Removing the connect line in the code resolved the doubled connection.

    Still a lot to learn .. when a connect is needed and when not for instance .. still not really sure about that concept.

  • @McLion I'ts really simple: You can either use connect() explicitly or use the special naming of slots as found in the documentation (e.g. _clicked), in which case Qt will do the connection automatically). This was a request from those who migrated from Delphi and weren't used to explicit connect calls. IMHO it tends to confuse people more than its worth (because there are enough cases where you have to use connect() explicitly), but others see that differently.

  • @danimo
    Thanks a lot for the confirmation of my finding and its explanation.

Log in to reply