亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1496 | 回復: 0
打印 上一主題 下一主題

請大家?guī)臀铱匆幌麓朔⻊掌骶程是否有問題 [復制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2009-05-09 20:23 |只看該作者 |倒序瀏覽
各位大家看一下我這個服務器線程有什么問題沒有,謝謝了!
void *thread_user_server(GMutex *mutex)
{
    int ret;
    struct user_conn_context_t *context=&auth_datas->user_conn_context;
   
    context->user_socket=socket(AF_INET,SOCK_STREAM,0);
    if(context->user_socket==-1)
    {

         /*skip*/
    }

    /* set socket reuse and keep alive option */
    int option_value = 1;
    setsockopt(context->user_socket,
           SOL_SOCKET,
           SO_REUSEADDR, &option_value, sizeof(option_value));
    setsockopt(context->user_socket,
           SOL_SOCKET,
           SO_KEEPALIVE, &option_value, sizeof(option_value));
   
    struct sockaddr_in usr_addr;
    usr_addr.sin_family=AF_INET;
    usr_addr.sin_port=htons(USER_SERVER_PORT);     
    usr_addr.sin_addr.s_addr=inet_addr(AUTHSRV_IP);
    bzero(&(usr_addr.sin_zero),8);

    ret=bind(context->user_socket,(struct sockaddr*)&usr_addr,sizeof(struct sockaddr));
    if (ret) {
        /*skip*/

    }
    printf("[+]user server bind on port %d ...\n",USER_SERVER_PORT);     

    /* Listen ! */
    int socket_fd;
    socket_fd = listen(context->user_socket,5);
    if (socket_fd == -1) {
        /*skip*/   

    }   

    int client_socket[context->max_user_num];

    FD_ZERO(&context->read_set);
    FD_SET(context->user_socket,&context->read_set);   

    context->mx=context->user_socket+1;
   
    struct timeval tv;
    fd_set wk_set;
    int i,z,n,index=0;
   
    g_message("[+]wait for client connections!");
        
    while(g_mutex_trylock(mutex))
    {
        g_mutex_unlock(mutex);
        
        /*copy read set to working set*/
        FD_ZERO(&wk_set);
        g_mutex_lock(context->wr_mutex);
        for(z=0;z<context->mx;z++)
            if(FD_ISSET(z,&context->read_set))
                FD_SET(z,&wk_set);
        g_mutex_unlock(context->wr_mutex);

        /*wait read events during 1 second
         * read events:
         * 1.context->user_socket can read
         * 2.client_socket can read*/
   
        tv.tv_sec=0;
        tv.tv_usec=250000;

        n=select(context->mx,&wk_set,NULL,NULL,&tv);
        if(n==-1)
        {
            /*signal SIGNIPE was captured,ingore it*/
            if(errno==EINTR)
            {
                g_message("broken pipe!\n");
                continue;
            }

            if(errno==EBADF)
            {
                g_message("bad file!\n");
                int i;
                /*a clinet disconnect between FD_SET and select
                 * try to find it*/

                for(i=0;i<context->mx;i++)
                {
                    if(FD_ISSET(i,&context->read_set))
                    {
                        struct stat s;
                        if(fstat(i,&s)<0)
                        {
                            g_message("%d is a bad socket",i);

                            g_mutex_lock(context->wr_mutex);
                            FD_CLR(i,&context->read_set);
                            g_mutex_unlock(context->wr_mutex);
                        }
                    }
                }
                continue;
            }
        }else if(!n)
        {
            /*time up*/
            continue;
        }

        /*check if a socket can accept
         * that means new user connection*/

        if(FD_ISSET(context->user_socket,&wk_set))
        {
            if(context->curr_user_num>context->max_user_num)
            {
                g_message("max client connection reached,deny new connections!");
                continue;
            }   

            struct sockaddr user_sock_addr;
            int len=sizeof(struct sockaddr);
            
            /*add new client fd to client_socket array*/
            client_socket[context->curr_user_num]=accept(context->user_socket,&user_sock_addr,&len);
        
            /*ckeck fd sanity*/
            if(client_socket[context->curr_user_num]<0)
            {
                g_message("bad connection accepted");
                continue;
            }
            g_message("%d client online",context->curr_user_num+1);   
            

            /*set keepalive option*/
            setsockopt(context->user_socket,
                           SOL_SOCKET,
                                   SO_KEEPALIVE,
                         &option_value,
                         sizeof(option_value)
                          );

            /*update mx if needed*/
            if(client_socket[context->curr_user_num]>context->mx)
                context->mx=client_socket[context->curr_user_num]+1;

            /*add new client fd to read set watch range*/
            g_mutex_lock(context->wr_mutex);
            FD_SET(client_socket[index],&context->read_set);
            g_mutex_unlock(context->wr_mutex);
        
            g_thread_pool_push(auth_datas->user_data_accept_pool,
                    &client_socket[index],
                    NULL);
            context->curr_user_num++;   
            continue;   
        }

        /*check if a socket can recv*/
        for(i=0;i<context->mx;i++)
        {
            /*skip user_socket*/
            if(i==context->user_socket)
                continue;
            if(FD_ISSET(i,&wk_set))
                g_thread_pool_push(auth_datas->user_data_accept_pool,
                        &client_socket[i],
                        NULL);
        }
   
    }
   
    close(context->user_socket);
    g_message("uesr server thread exit ok\n");
   
    return NULL;
}




[ 本帖最后由 gordenisgk 于 2009-5-9 20:25 編輯 ]
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP