Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当其中一个客户端停止时,所有TCP客户端都会终止_C_Server_Tcp_Pthreads - Fatal编程技术网

当其中一个客户端停止时,所有TCP客户端都会终止

当其中一个客户端停止时,所有TCP客户端都会终止,c,server,tcp,pthreads,C,Server,Tcp,Pthreads,我正在尝试使用pthreads实现一个TCP服务器,它可以在C语言中一次处理多个连接 服务器代码 #include<stdio.h> #include<string.h> #include<sys/socket.h> #include<arpa/inet.h> #include<unistd.h> #include<pthread.h> #include<readline/readline.h> void* c

我正在尝试使用
pthreads
实现一个TCP服务器,它可以在C语言中一次处理多个连接


服务器代码

#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<pthread.h>
#include<readline/readline.h>
void* connection_handler(void *arg)
{
    int sock_number = *((int *)arg);
    printf("New connection with socket No. : %d\n",sock_number);
    char *message = "!!!!!!!!!!!!!!!!!! MESSAGE !!!!!!!!!!!!!!!!!!\n";
    while(1)
    {
        write(sock_number,message,strlen(message));
    }
}
int main()
{
    int sock_desc;
    sock_desc = socket(AF_INET,SOCK_STREAM,0);
    if(sock_desc<0)
    {
        perror("Socket error");
    }
    else
    {
        printf("Socket created!\n");
        struct sockaddr_in server,client;
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = INADDR_ANY;
        server.sin_port = htons(8080);
        if(bind(sock_desc,(struct sockaddr*)&server,sizeof(server))<0)
        {
            perror("Binding error");
        }
        else
        {
            printf("Socket bound to 8080 port!\n");
            printf("Listening started.........\nWaiting for connections from clients.........\n");
            listen(sock_desc,20);
            int sock_size = sizeof(struct sockaddr_in);
            int new_socket;
            pthread_t pid[50];
            int connection_count=0;
            while((new_socket = accept(sock_desc,(struct sockaddr*)&client,(socklen_t*)&sock_size)))
            {
                printf("New connection : %s:%d\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
                if(pthread_create(&pid[connection_count++],NULL,connection_handler,&new_socket)!=0)
                {   
                    perror("Thread error");
                }
            }
            for(int i=0;i<connection_count;i++)
            {
                pthread_join(pid[i],NULL);
            }
        }
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
void*连接\u处理程序(void*arg)
{
int sock_number=*((int*)arg);
printf(“具有套接字编号的新连接:%d\n”,套接字编号);
char*message=“!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n”;
而(1)
{
写(袜子号码、信息、strlen(信息));
}
}
int main()
{
国际袜子描述;
sock\u desc=套接字(AF\u INET,sock\u STREAM,0);

如果(sock_desc让我们看一下这个代码片段:

 while((new_socket = accept(sock_desc,(struct sockaddr*)&client,(socklen_t*)&sock_size)))
        {
            printf("New connection : %s:%d\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
            if(pthread_create(&pid[connection_count++],NULL,connection_handler,&new_socket)!=0)
            {   
                perror("Thread error");
            }
        }
        for(int i=0;i<connection_count;i++)
        {
            pthread_join(pid[i],NULL);
        }

让我们看一下这段代码片段:

 while((new_socket = accept(sock_desc,(struct sockaddr*)&client,(socklen_t*)&sock_size)))
        {
            printf("New connection : %s:%d\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
            if(pthread_create(&pid[connection_count++],NULL,connection_handler,&new_socket)!=0)
            {   
                perror("Thread error");
            }
        }
        for(int i=0;i<connection_count;i++)
        {
            pthread_join(pid[i],NULL);
        }

看起来像
while((新的\u套接字=接受(sock\u desc,(struct sockaddr*)和client,(socklen\u t*)和sock\u size)))
将循环直到
accept
返回零。0可能是一个可接受的套接字,accept在出错时返回负数。Duhr.不,0应该是stdin。我认为你永远不会看到0。线程不断地写入套接字。如果程序写入已关闭的套接字,你应该得到一个SIGPIPE,如果你不处理它,你应该得到一个SIGPIPE正确地说,程序将崩溃。不要忘记检查
write
中的返回代码,以便处理它抛出的错误。“,&new_socket”不,不安全。在处理程序线程可以取消引用指向它的指针之前,第二个连接可能会变异new_socket:(另一件突出的事情是,您通过引用线程例程传递
new_socket
。因此,如果多个客户端快速连续连接,则它们都有可能使用相同的套接字描述符。看起来像
while((new_socket=accept(sock_desc,(struct sockaddr*)和client,(socklen_t*)和sock_size)))
将循环直到
accept
返回零。0可能是一个可接受的套接字,accept在出错时返回负数。Duhr.不,0应该是stdin。我认为你永远不会看到0。线程不断地写入套接字。如果程序写入已关闭的套接字,你应该得到一个SIGPIPE,如果你不处理它,你应该得到一个SIGPIPE正确地说,程序将崩溃。不要忘记检查
write
中的返回代码,以便处理它抛出的错误。“,&new_socket”不,不安全。在处理程序线程可以取消引用指向它的指针之前,第二个连接可能会变异new_socket:(唯一突出的另一件事是通过引用线程例程传递
new_socket
。因此,如果多个客户端快速连续连接,则它们都有可能使用相同的套接字描述符。