Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, May 2008
QProcess and QTcpSocket problem


Message 1 in thread

Hello,

I have a problem when using QProcess and QTcpSocket together. I spawn a 
process using QProcess while a client is connected via a QTcpSocket. 
Even after a closing it, the connection is still established. The socket 
is only closed when spawned process is terminated.

Is it a normal beheviour ? How could I close the connection ?

Code of main.hxx :

#include <QTcpServer>
#include <QTimer>
#include <QTcpSocket>
#include <QProcess>

class ServerListener : public QObject {
  Q_OBJECT
public:
  ServerListener(QObject * parent = 0L) : QObject(parent){}
  ~ServerListener(){}

public slots:
  void newConnection(){
    QTcpServer * server = static_cast<QTcpServer*>(sender());
    QTcpSocket * socket = server->nextPendingConnection();

    QProcess * process = new QProcess(this);
    process->start("c:/windows/notepad.exe");

    socket->close();
    delete socket;
  }
};


Code of main.cxx :

#include <main.hxx>
#include <QCoreApplication>

int main(int argc, char ** argv){
  QCoreApplication app(argc, argv);
 
  QTcpServer * tcpServer = new QTcpServer(&app);
  if(!tcpServer->listen(QHostAddress::Any, 1234)){ return 1; }

  ServerListener * serverListener = new ServerListener(&app);
  app.connect(tcpServer, SIGNAL(newConnection()), serverListener, SLOT(newConnection()));

  return app.exec();
}

Thibaut.

--
 [ signature omitted ] 

Message 2 in thread

Ok, solved.

It was a windows only problem due to the way the new process is 
launched. QProcess use handle inheritance to create the process.

And by default, QTcpSocket has the HANDLE_FLAG_INHERIT up.

If someone is interested on how to bypass this problem, here is the 
workaround :

HANDLE socketHandle = (HANDLE) socket->socketDescriptor();;
SetHandleInformation(socketHandle, HANDLE_FLAG_INHERIT, 0);

I think that we can do the same thing for QTcpServer.

Thibaut.

Thibaut Neiger a écrit :
> Hello,
>
> I have a problem when using QProcess and QTcpSocket together. I spawn 
> a process using QProcess while a client is connected via a QTcpSocket. 
> Even after a closing it, the connection is still established. The 
> socket is only closed when spawned process is terminated.
>
> Is it a normal beheviour ? How could I close the connection ?
>
> Code of main.hxx :
>
> #include <QTcpServer>
> #include <QTimer>
> #include <QTcpSocket>
> #include <QProcess>
>
> class ServerListener : public QObject {
>  Q_OBJECT
> public:
>  ServerListener(QObject * parent = 0L) : QObject(parent){}
>  ~ServerListener(){}
>
> public slots:
>  void newConnection(){
>    QTcpServer * server = static_cast<QTcpServer*>(sender());
>    QTcpSocket * socket = server->nextPendingConnection();
>
>    QProcess * process = new QProcess(this);
>    process->start("c:/windows/notepad.exe");
>
>    socket->close();
>    delete socket;
>  }
> };
>
>
> Code of main.cxx :
>
> #include <main.hxx>
> #include <QCoreApplication>
>
> int main(int argc, char ** argv){
>  QCoreApplication app(argc, argv);
>
>  QTcpServer * tcpServer = new QTcpServer(&app);
>  if(!tcpServer->listen(QHostAddress::Any, 1234)){ return 1; }
>
>  ServerListener * serverListener = new ServerListener(&app);
>  app.connect(tcpServer, SIGNAL(newConnection()), serverListener, 
> SLOT(newConnection()));
>
>  return app.exec();
> }
>
> Thibaut.
>
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ] 

Message 3 in thread

On Friday 02 May 2008 15:07:58 Thibaut Neiger wrote:
> HANDLE socketHandle = (HANDLE) socket->socketDescriptor();;
> SetHandleInformation(socketHandle, HANDLE_FLAG_INHERIT, 0);
>
> I think that we can do the same thing for QTcpServer.

Please report this to qt-bugs@xxxxxxxxxxxxx so that we can track the issue. 
The file descriptors and handles should not leak to sub-processes like that.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.