随便一点代码

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h>
#include <fcntl.h>

typedef struct p_worker 
{
    pid_t pid;
    int index;	
} p_worker_t;


int stop = 0;

int max_worker_process_num = 32;


int _socket_setnonblock(int fd) 
{
    int flags = fcntl(fd, F_GETFL);
    if (flags == -1) 
    {
        printf("fcntl error %d\n", errno);
				return -1;
    }

    flags |= O_NONBLOCK;
    if (fcntl(fd, F_SETFL, flags) == -1)
    {
        printf("fcntl error %d\n", errno);
				return -1;
    }
    return 0;
}

int socket_setnonblock(int fd) 
{
    int flags = fcntl(fd, F_GETFL);
    if (flags == -1) 
    {
        printf("fcntl error %d\n", errno);
				return -1;
    }

    flags |= O_NONBLOCK;
    if (fcntl(fd, F_SETFL, flags) == -1)
    {
        printf("fcntl error %d\n", errno);
				return -1;
    }
    return 0;
}

int _socket(int domain, int type, int protocol)
{
    return socket(domain, type, protocol);
}

int socket_bind(int sock, unsigned short int port) 
{
    printf("bind to port %d\n", port);
    struct sockaddr_in sock_addr;
    bzero(&sock_addr, sizeof(struct sockaddr_in));
    sock_addr.sin_family = AF_INET;
    sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    sock_addr.sin_port = htons(port);

    if(bind(sock, (struct sockaddr *) &sock_addr, sizeof(struct sockaddr)) == -1) 
    {
        printf("bind error %d\n", errno);
        return -1;
    }
    return 0;
}

int socket_listen(int sock)
{
    if (listen(sock, 50) == -1) 
    {
        printf("listen error %d\n", errno);
        return -1;
    }
    return 0;
}

void dump_fork(pid_t child_pid) 
{
	  pid_t parent = getppid();
    pid_t pid = getpid();
    printf("parent: %d, child: %d, pid: %d\n", parent, child_pid, pid);
}

int child(int argc, char** argv) 
{
	  printf("fork child process...\n");
    pid_t child_pid = fork();
    
    /*
    pid_t pid = getppid();
    pid_t process = getpid();
    printf("pid: %d, child: %d, process: \n", pid, child, process);
    */
    //*
    if (child_pid == 0) 
    {
    	  dump_fork(child_pid);
    } 
    else if (child_pid == -1)
    {
        dump_fork(child_pid);
    }
    else 
    {
        dump_fork(child_pid);
    }
    //*/
    return child_pid;
}

int start_acceptor_process(int argc, char** argv)
{
    return child(argc, argv);
}

void start_worker_process(int argc, char** argv, int *index, p_worker_t* w, int size)
{  
    pid_t child_pid = child(argc, argv);
    
    w[*index].pid = child_pid;
    w[*index].index = *index;
    
    //*
    if (child_pid == 0) 
    {
        dump_fork(child_pid);
    	  printf("worker process index: %d\n", *index);
    	  while (1) 
    	  {
    	      // loop to handle request...	
    	  }
    	  return;
    } 
    else if (child_pid == -1)
    {
        dump_fork(child_pid);
        printf(">worker process index: %d\n", *index);
        return;
    }
    else 
    {
        dump_fork(child_pid);
        printf(">>worker process index: %d\n", *index);
        if (*index < size) 
        {
        	  *index = *index + 1;
        	  start_worker_process(argc, argv, index, w, size);
        }
    }
    //*/
}

int main(int argc, char** argv) 
{
	  int child_pid = 0;
    int fd = -1;
    
    pid_t pid = getpid();
    pid_t parent = getppid();
    printf("pid: %d, parent: %d\n", pid, parent);
    
#ifdef USE_SOCK_NONBLOCK
    fd = _socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
#else
		fd = _socket(AF_INET, SOCK_STREAM, 0);
		if (fd == -1) 
		{
		    printf("socket error %d\n", errno);
    	  return 1;
		}
		else 
		{
		    printf("socket ok, socket: %d\n", fd);
		}

    socket_setnonblock(fd);
#endif

    if (socket_bind(fd, 2121) == -1)
    {
        return -1;
    }
    
    if (socket_listen(fd) == -1) 
    {
				return -1;
    }
    
    child_pid = start_acceptor_process(argc, argv);
    
    //*
    if (child_pid == 0) 
    {
        int i = 0;
    	  p_worker_t *w = (p_worker_t *) malloc(max_worker_process_num * sizeof(p_worker_t));
    	  
    	  
        dump_fork(child_pid);
        printf("acceptor process started...\n");
        
        start_worker_process(argc, argv, &i, w, max_worker_process_num);
    } 
    else if (child_pid == -1)
    {
        dump_fork(child_pid);
    }
    else 
    {
    		//*
    	  int new_fd = -1;
        struct sockaddr sock_addr;
    	  int addrlen = sizeof(struct sockaddr);
    	  //*/
    	  
    	  
    	  
        dump_fork(child_pid);
        
        
        //*
        printf("accepting connection...\n");
        while (1) 
        {  
        	  //printf("server socket: %d\n", fd);
            new_fd = accept(fd, &sock_addr, &addrlen);
            if (new_fd == -1) 
            {
            	  if (errno == EWOULDBLOCK) 
            	  {
            	      
            	  }
            	  else 
            	  {
                    printf("accept error %d\n", errno);
              	}
                continue;
            }
            else 
            {
                printf("accept new connection from %d\n", new_fd);
            }
      	}
      	//*/
    }
    //*/
    
    printf("started...\n");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值