[Qt-interest] error passing socket descriptors between applications

Thomi Richards thomir at gmail.com
Mon Aug 17 11:25:05 CEST 2009


Hi,


I'm trying to create an application that listens on a TCP port, and
then passes any incoming connections to child processes. I do this
using the following steps:

1) create QTcpServer and override newConnection()
2) For each new connection, create the child process using QProcess.
3) write the socket descriptor to the processes stdin stream, after
duplicating the socket (using dup() under Linux)
4) in the client application, I read the socket descriptor from the
stdin stream, and create a TCPSocket using the setSocketDescriptor()
call.

In my test application, this works perfectly. However, when I try and
replicate this in a larger application, It fails. The failure is in:

qnativesocketengine_unix.cpp in the following code (line 674):

    if (::getsockname(socketDescriptor, &sa.a, &sockAddrSize) == 0) {
    ...

    } else if (errno == EBADF) {
        setError(QAbstractSocket::UnsupportedSocketOperationError,
InvalidSocketErrorString);
        return false;
    }

EBADF means a bad file descriptor. I've checked, and I'm sure I'm
doing everything in my large application that I'm doing in my test
app. Can anyone suggest what might be the issue here? The socket
descriptor value seems OK to me - it's the original descriptor value +
1, so the call to dup() is working.

My test app is available for download here:

https://sites.google.com/site/thomir/downloads/testSocketPassing.tgz?attredirects=0

It's 3 files, and a 4KB download - however, since this works perfectly
for me, I'm not sure how useful this will be.


I'd be very grateful if anyone could suggest anything. I'm rather
stumped at this point

cheers..



More information about the Qt-interest-old mailing list