简体   繁体   English

在分支过程和客户端之间断开连接

[英]disconnect between forked process and client

I'm using Qt 5.3 on Linux. 我在Linux上使用Qt 5.3。 I have QCoreApplication server running on Linux, server fork itself when new connection arrives from client. 我有QCoreApplication服务器在Linux上运行,当新连接从客户端到达时,服务器会自行分叉。

void Ccbox::incomingConnection(qintptr handle) {
    qDebug()<<"Ccbox::incomingConnection";
    pid_t PID=fork();
    if(PID==0) {
       this->close();
       this->startNetwork(handle);
    } else {
        qDebug()<<"Ccbox::incomingConnection another process started";
    }
}

“this->close()” is to close child process to listen to port like parent, then pointer “handle” is passed to function that creates socket “ this-> close()”是关闭子进程以像父进程一样监听端口,然后将指针“ handle”传递给创建套接字的函数

socket=new QSslSocket(this);
if(!socket->setSocketDescriptor(socketDescriptor)) {
        qDebug() <<"setSocketError:"<<socket->error();
        return;
    }
qDebug()<<"ssl supported:"<<socket->supportsSsl();
qDebug()<<"peerAddress:"<<socket->peerAddress();
socket->addCaCertificates(Zm().sslCA+Zm().sslCACert);
socket->setPrivateKey(Zm().sslFiles+Zm().sslKey);
socket->setLocalCertificate(Zm().sslFiles+Zm().sslCert);
socket->setPeerVerifyMode(QSslSocket::VerifyPeer);
socket->startServerEncryption();

this is QSslSocket, so some cert options are set and connection is encrypted. 这是QSslSocket,因此设置了一些证书选项并加密了连接。

The problem is that my forked server always ends up in defunct state “< defunct >” after connection is closed between server and client and server makes qApp->quit(); 问题是在关闭服务器与客户端之间的连接并且服务器使qApp-> quit()断开后,我的分叉服务器总是以“ <defunct>”状态终止。

Connection closing is initiated by a client with disconnectFromHost and on both ends I can see that socket is entering state: QAbstractSocket::UnconnectedState, 5 seconds later forked server makes qApp->quit() and ends up in a defunct state. 连接关闭是由客户端使用DisconnectFromHost启动的,并且在两端都可以看到套接字正在进入状态:QAbstractSocket :: UnconnectedState,5秒后,分叉的服务器执行qApp-> quit()并最终处于无效状态。

I think that what I don't understand is this: When I check with lsof command, right after I start client and fork server it shows that both parent and child server are connected to the client - parent(21347), child(21351): 我认为我不明白的是:当我用lsof命令检查时,在启动客户端和fork服务器之后,它立即表明父级和子级服务器都已连接到客户端-parent(21347),child(21351) :

ccbox-dem 21347   7u  IPv4 2439080      0t0  TCP *:4321 (LISTEN)
ccbox-dem 21347   8u  IPv4 2442399      0t0  TCP localhost:4321->localhost:38669 (ESTABLISHED)
ccbox-big 21349     10u  IPv4 2440519      0t0  TCP localhost:38669->localhost:4321 (ESTABLISHED)
ccbox-dem 21351   8u  IPv4 2442399      0t0  TCP localhost:4321->localhost:38669 (ESTABLISHED)

and after client is closed parent server is still connected to the client waiting in CLOSE_WAIT state 客户端关闭后,父服务器仍连接到处于CLOSE_WAIT状态的客户端

ccbox-dem 21347    7u  IPv4 2439080      0t0  TCP *:4321 (LISTEN)
ccbox-dem 21347    8u  IPv4 2442399      0t0  TCP localhost:4321->localhost:38669 (CLOSE_WAIT)

and "ps ax" commands shows that child process (21351) is in defunct state. 和“ ps ax”命令显示子进程(21351)处于失效状态。

What should I do in parent server to close this connection right after child server has started ? 子级服务器启动后,我应如何在父级服务器中关闭此连接? Or is this some other problem? 还是这是其他问题? I need to get rid of these defunct processes 我需要摆脱这些无效的过程

Best Regards 最好的祝福

Marek 马雷克

To clean up the defunct processes, you must wait for them. 要清理已终止的进程,您必须wait它们。 Any of the functions here could do: http://linux.die.net/man/2/wait - the idea is that they are "defunct" because nobody has waited for them to finish, to get their return value. 这里的任何功能都可以做到: http : //linux.die.net/man/2/wait-想法是它们“已失效”,因为没有人等待它们完成以获取返回值。 Somebody needs to do that, or the children will be sad and haunt you forever. 有人需要这样做,否则孩子们会难过并永远困扰着您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM