【问题标题】:Why does my client kill my server?为什么我的客户端会杀死我的服务器?
【发布时间】:2011-03-05 22:15:25
【问题描述】:

为什么一个线程退出了,父进程也退出了?当我运行服务器时,一切都很好。它坐在套接字上听。当客户端连接服务器线程为其提供服务时。当他们来回交谈时,客户端退出,服务器也退出。我正在使用 pthread.h 进行线程处理。他们来了!

首先是客户:

#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>

#define CLIENT_CONNECTED 0
#define CLIENT_NOT_CONNECTED 1
#define PORT 9999
#define MAX_CLIENTS 100

using namespace std;

struct client {
    int socket;
    int state;
    pthread_t tid;
};

int 
connectToServer (char *address )
{
    struct hostent *hostinfo;
    struct sockaddr_in name;
    int s;  
    int rc = 0;

    if ( ( s = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
    {
        cerr<<"Client could not declare socket"<<"\n";
        exit( 1 );
    }

    name.sin_family = AF_INET;
    name.sin_port = htons ( ( unsigned short int ) PORT );
    name.sin_addr.s_addr = htonl( INADDR_ANY );
    hostinfo = gethostbyname ( address );

    if ( hostinfo == NULL )
    {
        cerr<<"Host unknown"<<"\n";
        exit( 1 );
    }   

    name.sin_addr = *( struct in_addr * ) hostinfo->h_addr;

    if ( connect( s, ( const sockaddr * ) &name, sizeof( name ) ) < 0 )
    {
        cerr<<"Could not connect to host"<<"\n";
        exit( 1 );
    }
    else 
    {
/*      if( fcntl( s, F_SETFL, O_NONBLOCK ) == -1 ) 
        {
            perror( "fcntl" );
            exit( 1 );
        } */

        char readbuf[1024];
        char message[ ] = "START";
        rc = send( s, message, strlen(message), 0 );
        cout<<"RC on send() was "<<rc<<"\n";
        if ( rc > 0 )
        {
            cout<<"using recv...\n";

            while( ( rc = recv( s, readbuf, 1, 0 ) ) > 0 )
            { 
                readbuf[ rc ] = '\0';
                cout<<"Server responds with: "<<readbuf<<"\n";
            }

            return true;
        }
        else 
        {
            return false;
        }
    }
}

void 
killsignal( int param )
{
   fprintf( stderr, "Disconnecting." );
   exit( 1 );
}

int 
main ( )
{
    signal( SIGKILL, killsignal );
    signal( SIGINT, killsignal );

    char address[] = "localhost";
    connectToServer( address );
}

还有服务器:

#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>

#define CLIENT_CONNECTED 0
#define CLIENT_NOT_CONNECTED 1
#define PORT 9999
#define MAX_CLIENTS 100

using namespace std;

struct client {
    int socket;
    int state;
    pthread_t tid;
};

int 
sendClient( char *message, int *socket )
{
    int rc = 0;
    cout<<message<<"\n";
    rc = send( *socket, message, strlen(message), 0 ); 
    cout<<"send RC is: "<<rc<<"\n";
}

void 
strtochrstr( char **to, string from )
{

    int len = from.size( );

    *to = ( char * )malloc( ( len + 1 ) * sizeof( char ) );

    if ( to == NULL ){
        cout<<"out of memory!\n";
        exit( 1 );
    }

    *to[ 0 ] = '\0';

    strcpy( *to, from.c_str( ) );
}

int 
createSocket ( int *s )
{
    struct sockaddr_in name;

    if ( ( *s = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
    {
        cerr<<"Server: Socket"<<"\n";
    }

    name.sin_family = AF_INET;
    name.sin_port = htons( (unsigned short int) PORT );
    name.sin_addr.s_addr = htonl( INADDR_ANY );
    if ( bind( *s, ( struct sockaddr * ) &name, sizeof( name ) ) < 0 )
    {
        cerr<<"Could not bind to socket."<<"\n";
        exit( 1 );
    }

    return *s;
}

void *
serveClient( void *clientState )
{
    int c;
    int rc = 0;
    char readbuf[ 2 ];
    char message[ ] = "Message to client";//( char * ) malloc( 300 * sizeof( char ) );
    struct client *mystate = ( struct client * ) clientState;

    /* Set socket tot NONBLOCKING */
    if( fcntl( mystate->socket , F_SETFL, O_NONBLOCK ) == -1 ) 
    {
        perror( "fcntl" );
        exit( 1 );
    } 

    while ( true )
    {
        while ( ( rc = recv( mystate->socket, readbuf, 1 , 0 ) ) > 0 )
        {
            readbuf[ 1 ] = '\0'; 
            cout<<readbuf<<"\n";
        }
        sendClient( message, &( mystate->socket ) ); 
    }
}

int 
listenUp ( int *s )
{
    int i = 0;
    int error = 0;
    pthread_t clientIds[MAX_CLIENTS];
    struct client clients[MAX_CLIENTS];
    struct sockaddr_in fsaun[MAX_CLIENTS];  
    int fromlen[MAX_CLIENTS];   

    while ( i++ < MAX_CLIENTS )
        clients[i].state = CLIENT_NOT_CONNECTED;

    if ( listen( *s, 10 ) < 0 )
    {
        cerr<<"Could not listen on socket"<<"\n";
        exit( 1 );
    }   

    while ( true )
    {
        while ( ( clients[i++].state == CLIENT_CONNECTED && i < MAX_CLIENTS ) );    

        if ( ( clients[i].socket = accept( 
            *s, 
            ( sockaddr * ) &fsaun[i],
            ( socklen_t * ) &fromlen[i] ) ) < 0 )
        {
            cerr<<"Could not accept connection "<<i<<"\n";
        }
        else 
        {
            error = pthread_create(
                &clients[i].tid,
                NULL,
                serveClient,
                (void *)&clients[i]
            );
        }
        i = 0;
    }
}

void 
killsignal( int param )
{
   fprintf( stderr, "Disconnecting.\n" );
}

void 
intsignal( int param )
{
    fprintf( stderr, "Write error.\n" );
}

int 
main ( )
{
    signal( SIGKILL, killsignal );
    signal( SIGINT, intsignal );

    int mySock = createSocket( &mySock );
    listenUp( &mySock );
}

【问题讨论】:

  • 将此添加到您的服务器:并再次检查它的行为:signal(SIGPIPE,SIG_IGN);
  • 这解决了问题...如果您将此作为答案而不是评论,我很乐意接受。

标签: c++ c sockets pthreads client


【解决方案1】:

您的服务器不断向客户端发送数据。 当您的客户端退出时,它还没有读取套接字上要读取的所有数据。

这种情况会产生一个 TCP RST,当收到 TCP RST 时,*nixes 的默认行为是将 SIGPIPE 信号传递给进程。 SIGPIPE 信号的默认行为是退出程序。

对于 TCP 服务器,通常只是忽略 SIGPIPE 信号,忽略 SIGPIPE,write()/send() 将在提到的条件(客户端退出或关闭带有待处理数据的套接字)并将 errno 设置为 EPIPE 时返回错误

在你的服务器的 main() 中添加这个:

signal(SIGPIPE,SIG_IGN);

更多信息可以在here找到

【讨论】:

    【解决方案2】:

    exit 终止进程,因此包括您在同一进程中生成的所有线程。你应该从你的线程函数中返回而不调用exit。

    【讨论】:

    • 我不太了解 pthreads,OP 是否需要加入已完成的线程来清理它们?
    • OP 有 2 个不同的程序,他不会通过退出线程来终止。
    • @nos:他的“服务器”进程产生线程。他有两个进程和许多线程。
    • @Charles Bailey,他的问题是当客户端退出时,它会杀死服务器。问题不在于服务器线程结束时,它 exit() 的服务器
    • 我犯了一个错误。杀死线程/服务器的不是 exit()。我的错。
    【解决方案3】:

    exit() 杀死整个进程,这将杀死构成它的所有线程。您可以互换使用“线程”和“进程”这两个词,这表明您认为这两者之间可能存在一些混淆。

    多个线程可以在一个进程中执行,但如果进程死了,它的所有线程都会死。

    【讨论】:

      猜你喜欢
      • 2014-01-22
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 2019-11-23
      • 2017-01-05
      • 2012-12-07
      相关资源
      最近更新 更多