[Qt-interest] passing socket descriptors between processes
Thomi Richards
thomir at gmail.com
Mon Aug 10 14:43:37 CEST 2009
Hi,
I have a need to pass a connected TCP socket descriptor between two Qt
applications. Essentially what I'm after is to have one process that
listens on a particular port and accepts incoming connectsions, then
spawns another copy of itself running as a different user. This new
process then handles all traffic on the original socket. After a bit
of trial and error I've found that it's not as easy as calling
QAbstractSocket::setSocketDescriptor(...). There are two methods I've
found that should work:
The first is to use an AF_UNIX domain socket to pass the socket
descriptor from the server to the client process. This method has been
discussed here:
https://www.linuxquestions.org/questions/programming-9/how-to-share-socket-between-processes-289978/
The second method is to create the new process by calling fork() - in
the child process the socket descriptor should still be available.
I was hoping that someone on this list has tried to do something
similar. As I see it, neither of the possible solutions are great.
Using UNIX sockets will obviously only for for machines that support
that particular mechanism of IPC. Using fork() seems a little risky as
well. I've tried to create a test app for this scenario, but I'm
having difficulty. I *think* there's a problem whereby forking() after
QCoreApplication::exec() has been called leads to some strange
behaviour. Can anyone suggest a better way to do this? I need to
support Macs, windows, and Linux boxes, so whatever method I choose I
need to be able to get it going on multiple operating systems without
too much hacking.
I'd be very grateful for any hints you may have.
Cheers,
More information about the Qt-interest-old
mailing list