线程池服务端设计

来源:互联网 发布:黑米软件怎么样 编辑:程序博客网 时间:2024/05/23 02:07

客户端代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 5555
#define SERVER_IP "127.0.0.1"

typedef struct mathopt{
    int type;
    float num1;
    float num2;
}mopt;

void create_mopt(struct mathopt *send_mopt);

int main()
{
    int rv;
    int client_sock;
    struct sockaddr_in ser_addr;
    char command[10];
    char recvbuf[100];

    client_sock = socket(AF_INET, SOCK_STREAM, 0);
    if(client_sock < 0)
    {
        printf("socket error:%s\n", strerror(errno));
        exit(0);
    }

    ser_addr.sin_family = AF_INET;
    ser_addr.sin_port = htons(PORT);
    ser_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    rv = connect(client_sock, (struct sockaddr*)&ser_addr, sizeof(ser_addr));
    if(rv < 0)
    {
        printf("connect error:%s\n", strerror(errno));
        exit(0);
    }
    printf("connect success\n");
    
    while(1)
    {
        memset(command, 0, sizeof(command));
        struct mathopt send_mopt;
        printf("input your command>");
        scanf("%s", command);
        if(strcmp(command, "add") == 0)
        {
            send_mopt.type = 1;
            create_mopt(&send_mopt);    
        }
        else if(strcmp(command, "quit") == 0)
        {
            send_mopt.type = 4;
        }

        send(client_sock, (char*)&send_mopt, sizeof(send_mopt), 0);

        if(4 == send_mopt.type)
        {
            break;
        }    
    
        memset(recvbuf, 0, sizeof(recvbuf));
        recv(client_sock, recvbuf, sizeof(recvbuf), 0);
        printf("the result is:%s\n", recvbuf);    
        
        

    }
    
    close(client_sock);
    
    exit(0);
}


void create_mopt(struct mathopt *send_mopt)
{
    printf("input num1>");
    scanf("%f", &(send_mopt->num1));

    printf("input num2>");
    scanf("%f", &(send_mopt->num2));

}

服务端代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define PORT 5555

struct thread_flag{
    int client_sock;
    int flag;
};

typedef struct mathopt{
    int type;
    float num1;
    float num2;
}mopt;

void* process_thread(void *param);
void showthreadstatus();

pthread_t thread_id[5];
struct thread_flag th_flag[5];

int main()
{
    int i;
    int rv;
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    int len = sizeof(client_addr);
    int server_sock;
    
    server_sock = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == server_sock)
    {
        exit(0);
    }
    
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PORT);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    rv = bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
    if(rv < 0)
    {
        printf("bind error:%s\n", strerror(errno));
        exit(0);
    }

    rv = listen(server_sock, 5);
    if(rv < 0)
    {
        printf("listen error:%s\n", strerror(errno));
        exit(0);
    }

    
    for(i=0;i<5;i++)
    {
        thread_id[i] = 0;
        th_flag[i].client_sock = 0;
        th_flag[i].flag = 0;
    }

    for(i=0;i<5;i++)
    {
        pthread_t tmp;
        rv = pthread_create(&tmp, NULL, process_thread, (void*)&i);
        if(rv == 0)
        {
            thread_id[i] = tmp;
        }
        sleep(2);
    }
    
    showthreadstatus();    
    
    while(1)
    {
        int client = accept(server_sock, (struct sockaddr*)&client_addr, (socklen_t*)&len);
        if(client < 0)
        {
            continue;
        }

        printf("new connect\n");
        int i = 0;
        for(;i<5;i++)
        {
            if(0 == th_flag[i].flag)
            {
                th_flag[i].flag = 1;
                th_flag[i].client_sock = client;
                printf("now change the thread's data:%d\n", th_flag[i].client_sock);
                break;
            }        
        }
        
        if(i ==5)
        {
            printf("server is busy,try later\n");
        }

    }
    
    printf("now thread pool status:\n");
    showthreadstatus();

    close(server_sock);
    exit(0);
}


void* process_thread(void *param)
{
    int i = *((int*)param);
    printf("%d start\n", i);
    while(1)
    {
        if(th_flag[i].flag == 1)
        {
            printf("thread %d is recv message\n", i);
            char buf[1024];
            memset(buf, 0, sizeof(buf));
            int len = recv(th_flag[i].client_sock, buf, sizeof(buf), 0);
            if(len < sizeof(struct mathopt))
            {
                close(th_flag[i].client_sock);
                th_flag[i].client_sock = 0;
                th_flag[i].flag = 0;
                continue;
            }
            
            struct mathopt *recvopt = (struct mathopt*)buf;
            char sendbuf[10];
            memset(sendbuf, 0, sizeof(sendbuf));
            if(recvopt->type == 1)
            {
                float result = recvopt->num1 + recvopt->num2;
                sprintf(sendbuf, "%f", result);    
            }
            else if(recvopt->type == 4)
            {
                close(th_flag[i].client_sock);
                th_flag[i].client_sock = 0;
                th_flag[i].flag = 0;
                continue;
            }

            len = send(th_flag[i].client_sock, sendbuf, sizeof(sendbuf), 0);
            if(len < 0)
            {
                close(th_flag[i].client_sock);
                th_flag[i].client_sock = 0;
                th_flag[i].flag = 0;
                continue;
            }


        }
        else
        {
            sleep(2);
        }
        
    }    

    return "thread exit";
}

void showthreadstatus()
{
    int i = 0;
    for(;i<5;i++)
    {
        printf("%d,%d,%d\n", i, th_flag[i].client_sock, th_flag[i].flag);
    }
}




0 0
原创粉丝点击