亲爱的,
我有一个执行子进程并尝试通过套接字连接到该子进程的主程序。此连接正常工作。但是,当检测到错误时,主程序会尝试正确关闭套接字连接,然后它会杀死子进程,重新启动一个新进程,并尝试连接新的子进程。
重新连接可以工作数百次。但是,如果我重复这数百次,在一定数量的循环之后,我的主程序就不再能够连接到子进程。
我不知道它有一个链接,但是当尝试正确关闭套接字时,我的主程序有时会收到一个 SIGPIPE。我忽略它并继续处理。但是,我注意到当程序不再接受连接时,它总是收到 10 个 SIGPIPES。我试了几次,总是10点结束。
任何想法 ?
问候, 布莱斯
编辑:
这是关闭连接的代码:`
int BAE_nb_sigpipes_received = 0;
void enmx_close( ENMX_HANDLE conn )
{
sConnectionInfo **connInfo, *temp;
SOCKET_CMD_HEAD cmd_head;
time_t secs;
pth_event_t ev_wakeup;
temp = NULL;
for( connInfo = &enmx_connections; *connInfo != NULL; connInfo = &(*connInfo)->next ) {
if( (*connInfo)->socket == conn ) {
temp = *connInfo;
*connInfo = (*connInfo)->next;
break;
}
}
if( temp != NULL ) {
cmd_head.cmd = SOCKET_CMD_EXIT;
cmd_head.address = 0xffff;
secs = time( NULL ) + TIMEOUT;
switch( temp->mode ) {
case ENMX_MODE_STANDARD:
while( write( conn, &cmd_head, sizeof( cmd_head )) == -1 )
{
if( errno == EAGAIN ) {
if( secs <= time( NULL )) {
break;
}
usleep( 10 * 1000 );
continue;
}
/* BAE : if we encounter a sigpipe, the client stupidly continues to try to write to scoket, which never ends...*/
else if (errno == EPIPE)
{
BAE_nb_sigpipes_received++;
break;
}
}
break;
case ENMX_MODE_PTH:
ev_wakeup = pth_event( PTH_EVENT_TIME, pth_time( secs, 0 ));
pth_write_ev( conn, &cmd_head, sizeof( cmd_head ), ev_wakeup );
// either the request has been sent or timeout reached
// in any case, we've finished
pth_event_free( ev_wakeup, PTH_FREE_ALL );
break;
}
if( temp->hostname ) free( temp->hostname );
if( temp->name ) free( temp->name );
free( temp );
}
close( conn );
}
`
这是打开它的代码:`
if( connect( sock_con, (struct sockaddr *)&server, sizeof( struct sockaddr_in )) != 0 ) {
printf("ENMX_E_SERVER_NOTRUNNING (%s)\n", myname);
return( ENMX_E_SERVER_NOTRUNNING );
}
`
杀死子进程的代码如下:`
wxProcess::Kill(eibnetmux_process_pid, wxSIGKILL);
`