C++ 为什么我的客户端会杀死我的服务器?

C++ 为什么我的客户端会杀死我的服务器?,c++,c,sockets,pthreads,client,C++,C,Sockets,Pthreads,Client,为什么线程退出时父进程也会退出?当我运行服务器时,一切正常。它坐在插座上监听。当客户端连接服务器线程为其服务时。当他们来回交谈时,客户端退出,服务器也退出。我正在使用pthread.h进行线程处理。它们在这里 首先,客户: #include <netinet/in.h> #include <netdb.h> #include <signal.h> #include <sys/types.h> #include <sys/socket.h>

为什么线程退出时父进程也会退出?当我运行服务器时,一切正常。它坐在插座上监听。当客户端连接服务器线程为其服务时。当他们来回交谈时,客户端退出,服务器也退出。我正在使用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 );
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义客户端\u已连接0
#定义客户端\u未连接\u 1
#定义端口9999
#定义最多100个客户端
使用名称空间std;
结构客户端{
int插座;
int状态;
pthread_t tid;
};
int
connectToServer(字符*地址)
{
结构主机*主机信息;
名称为struct sockaddr_;
int-s;
int rc=0;
if((s=socket(AF\u INET,SOCK\u STREAM,0))<0)
{

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

exit()
终止整个进程,这将终止组成该进程的所有线程。您可以使用“thread”和这个过程是“可交换的”,这意味着在你的头脑中,两者之间可能存在一些混淆


多个线程可以在一个进程内执行,但如果该进程死亡,则其所有线程都将死亡。

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

此条件将生成TCP RST,收到TCP RST时*nixes上的默认行为是将SIGPIPE信号传递给进程。SIGPIPE信号的默认行为是退出程序

对于TCP服务器,通常只忽略SIGPIPE信号,而SIGPIPE ignored write()/send()将在所述条件(客户端退出或关闭具有挂起数据的套接字)并将errno设置为EPIPE时返回错误

在服务器的main()中添加以下内容:

signal(SIGPIPE,SIG_IGN);

可以找到更多信息

将此添加到您的服务器:并再次检查其行为:
signal(SIGPIPE,SIG\u IGN)
这解决了问题…如果你回答这个问题而不是发表评论,我很乐意接受。我不太了解pthreads,OP是否需要加入已完成的线程来清理它们?OP有两个不同的程序,他不是通过退出线程来终止的。@no:His“server”“进程产生线程。他有两个进程和许多线程。@Charles Bailey和他的问题是,当客户端退出时,它会杀死服务器。问题不是当服务器线程结束时,它退出()服务器。我犯了一个错误。不是退出()杀死线程/服务器。我的错误。”。
signal(SIGPIPE,SIG_IGN);