在Linux的多线程服务器 [英] Multithreaded Server in LInux

查看:106
本文介绍了在Linux的多线程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器编程在Linux中environment.At同时工作用C可能有几个客户与服务器编程的Server.It是我的第一个项目联系在一起,并在多threading.Till没有多少经验,现在我已经设定以下为多线程。

我想获得以下几点一些了解。

1.How多个客户端将在服务器处理?

2.will 的pthread_t THR; 创建多个线程,或者我需要做的是这样的pthread_t THR [X] 创建点¯x主题?

3.How我可以得到每个客户端的数据在以下code?

4.Will 在pthread_create 创建一个新副本 * connection_handler 为每一个新的客户端连接。

 无效dieWithError(字符* ERRORMSG);FILE *文件;
无效* connection_handler(无效* socket_desc)
{
    //获取套接字描述符
    INT袜子= *为(int *)socket_desc;
    INT read_size;
    字符*消息,client_message [2000];    //接收来自客户机的消息
    而((read_size =的recv(袜子,client_message,2000,0))0)
    {
        //字符串结束标志
        client_message [read_size] ='\\ 0';           的printf(%S,client_message);
           fprintf中(文件%S,client_message);
        //清除消息缓冲区
        memset的(client_message,0,2000);
    }    如果(read_size == 0)
    {
        看跌期权(客户端断开连接);
        fflush(标准输出);
    }
    否则,如果(read_size == -1)
    {
        PERROR(recv的失败);
    }    返回0;
}
INT主(INT ARGC,字符** argv的){    INT sock_desc = 0,connfd = 0,listenfd = 0;
    结构SOCKADDR_IN serv_addr;
    INT clntSock;
    结构SOCKADDR_IN echoClntAddr;
    无符号整型clntLen;
    焦炭sendBuff [1025];
    焦炭recvBuff [10025]
    INT N = 0;
    THR的pthread_t;    sock_desc =插座(AF_INET,SOCK_STREAM,0);    如果(sock_desc℃,)
       dieWithError(无法打开套接字\\ n);    memset的(安培; serv_addr,0,sizeof的(serv_addr));    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(7024);    如果(绑定(sock_desc,(结构sockaddr *)及serv_addr,sizeof的(serv_addr))小于0)
       dieWithError(绑定失败\\ n);    如果(听(sock_desc,3)℃的)
       dieWithError(倾听失败\\ n);     文件=的fopen(testServer.txt,W);
      clntSock = sizeof的(结构sockaddr);
      INT I = 0;
      而((connfd =接受(sock_desc,(结构sockaddr *)及echoClntAddr,(socklen_t的*)及clntSock)))
    {
        看跌期权(连接已接受);        如果(在pthread_create(安培;苏氨酸,NULL connection_handler,(无效*)及connfd)℃的)
        {
            PERROR(无法创建线程);
            返回1;
        }        //现在加入线程,因此我们不线程之前终止
        //在pthread_join(thread_id单,NULL);
        看跌期权(处理器分配);
    }    如果(connfd℃,)
    {
        PERROR(接受失败);
        返回1;
    }       回报(EXIT_SUCCESS);
}无效dieWithError(字符* ERRORMSG){
     的printf(%S,ERRORMSG);
}


解决方案

  1. 这取决于在服务器上,它可以多少线程处理。一般来说,你有更多的线程,更多的时间,操作系统将只花线程之间的切换意味着线程本身将有更少的时间做任何实际工作。


  2. 例如。 的pthread_t THR; 是一个变量,能够容纳一个单独的线程。您的可以的重复使用多线程,但你失去了其他线程导致资源泄漏。你可能需要一个数组或者如果你想多线程的列表。


  3. 结构。每个线程一体式结构,包含线程信息(的pthread_t )的每个结构,客户端插座上,一切由主线程和客户端处理线程都需要的。


  4. 没有。 connection_handler 功能的,只会出现在内存中每个进程的函数的一个实例,功能不会被复制。


I am Working on server Programming in C in Linux environment.At same time there might be several clients linked with the Server.It's my first project on server Programming and not having much experience in Multi-threading.Till Now i have Programmed following for Multithreading.

I want to get some idea about following Points.

1.How many clients will the server Handle?

2.will pthread_t thr;create multiple Threads or i need to do something like pthread_t thr[X] to create X Threads ?

3.How can i get the data for each client in the following code ?

4.Will pthread_create create a new copy of *connection_handler for each new client connected.

void dieWithError(char *errormsg);

FILE *file;


void *connection_handler(void *socket_desc)
{
    //Get the socket descriptor
    int sock = *(int*)socket_desc;
    int read_size;
    char *message , client_message[2000];



    //Receive a message from client
    while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
    {
        //end of string marker
        client_message[read_size] = '\0';

           printf("%s",client_message); 
           fprintf(file,"%s", client_message);


        //clear the message buffer
        memset(client_message, 0, 2000);
    }

    if(read_size == 0)
    {
        puts("Client disconnected");
        fflush(stdout);
    }
    else if(read_size == -1)
    {
        perror("recv failed");
    }

    return 0;
} 




int main(int argc, char** argv) {

    int sock_desc = 0, connfd = 0,listenfd =0;
    struct sockaddr_in serv_addr;
    int clntSock; 
    struct sockaddr_in echoClntAddr; 
    unsigned int clntLen; 
    char sendBuff[1025];
    char recvBuff[10025];
    int n = 0;
    pthread_t thr;



    sock_desc = socket(AF_INET, SOCK_STREAM, 0); 

    if(sock_desc < 0 )
       dieWithError("Unable to open Socket\n");  

    memset(&serv_addr,0,sizeof(serv_addr)); 

    serv_addr.sin_family = AF_INET ;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(7024);

    if(bind(sock_desc, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) 
       dieWithError("bind failed\n");

    if(listen(sock_desc,3) < 0)
       dieWithError("listen failed\n");  

     file = fopen("testServer.txt", "w");


      clntSock = sizeof(struct sockaddr);
      int i =0;
      while((connfd = accept(sock_desc, (struct sockaddr *)&echoClntAddr,(socklen_t*)&clntSock)))
    {
        puts("Connection accepted");



        if( pthread_create( &thr, NULL ,  connection_handler , (void*) &connfd) < 0)
        {
            perror("could not create thread");
            return 1;
        }

        //Now join the thread , so that we dont terminate before the thread
        //pthread_join( thread_id , NULL);
        puts("Handler assigned");
    }

    if (connfd < 0)
    {
        perror("accept failed");
        return 1;
    }

       return (EXIT_SUCCESS);
}

void dieWithError(char *errormsg){
     printf("%s", errormsg);


}

解决方案

  1. It depends on the server machine, and how many threads it can handle. Generally speaking, the more threads you have, the more time the operating system will spend just switching between threads meaning the threads themselves will have less time doing any real work.

  2. E.g. pthread_t thr; is a single variable, able to hold a single thread. You can reuse it for multiple threads, but then you loose the other threads leading to a resource leak. You probably need an array or a list if you want multiple threads.

  3. Structures. One structure per thread, each structure containing the thread information (pthread_t), the client socket, and everything else needed by both the main thread and the client-handling thread.

  4. No. connection_handler is a function, there will only be one instance of a function per process in memory, functions are not copied.

这篇关于在Linux的多线程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆