博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
模仿pgpool-II的方式,建立线程池
阅读量:6156 次
发布时间:2019-06-21

本文共 4367 字,大约阅读时间需要 14 分钟。

接上一篇,,自己实现线程池。可以正常运行。

server端代码:con-server03.c

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define HELLO_WORLD_SERVER_PORT 6666#define LENGTH_OF_LISTEN_QUEUE 20#define BUFFER_SIZE 1024int main(int argc, char **argv){ struct sockaddr_in server_addr; bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htons(INADDR_ANY); server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT); int server_socket = socket(AF_INET,SOCK_STREAM,0); if( server_socket < 0){ printf("Create Socket Failed!"); exit(1); }if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr))){ printf("Server Bind Port : %d Failed!", HELLO_WORLD_SERVER_PORT); exit(1);}if ( listen(server_socket, LENGTH_OF_LISTEN_QUEUE) ){ printf("Server Listen Failed!"); exit(1);}//prepare for forking childrenstruct sockaddr_in client_addr;socklen_t length = sizeof(client_addr);int procnt;int fds;fd_set readmask;int newsock;int fd = 0;for (procnt=0; procnt<10; procnt++){ int child_process_pid = fork(); if(child_process_pid == 0 ){ fprintf(stderr,"Child %d is born.\n",getpid()); while(1){ FD_ZERO(&readmask); FD_SET(server_socket, &readmask); struct timeval timeout; timeout.tv_sec=30; timeout.tv_usec=0; int ssocket=server_socket+1; fds = select( ssocket, &readmask, NULL, NULL,&timeout); /* timeout */ if (fds == 0){ sleep(5); continue; } if (FD_ISSET(server_socket, &readmask)){ fd =server_socket; } memset( &client_addr, 0, sizeof(client_addr)); newsock = accept(fd, (struct sockaddr *)&client_addr, &length); char buffer[BUFFER_SIZE]; bzero(buffer, BUFFER_SIZE); strcpy(buffer,"Hello,World! FromServer! "); int pd; pd=getpid(); char cpd[10]; sprintf(cpd,"%d",pd); strcat(buffer,cpd); strcat(buffer,"\n"); send(newsock,buffer,BUFFER_SIZE,0); bzero(buffer,BUFFER_SIZE); length = recv(newsock,buffer,BUFFER_SIZE,0); if (length < 0){ printf("Server Recieve Data Failed!\n"); exit(1); } fprintf(stderr,"got: %s in %d\n",buffer,getpid()); close(newsock); }///while circle ends exit(0);///child ends.never goes here }else{ //parent process, do nothing }} /// for circle of forking endsfor(;;){ sleep(10); fprintf(stderr,"I am main process waked from sleep.\n");}close(server_socket);return 0;}

client端代码:con-client.c

#include 
#include
#include
#include
#include
#include
#include
#define HELLO_WORLD_SERVER_PORT 6666#define BUFFER_SIZE 1024void talk_to_server(char ** argv){ struct sockaddr_in client_addr; bzero(&client_addr,sizeof(client_addr)); client_addr.sin_family = AF_INET; client_addr.sin_addr.s_addr = htons(INADDR_ANY); client_addr.sin_port = htons(0); int client_socket = socket(AF_INET,SOCK_STREAM,0); if( client_socket < 0){ printf("Create Socket Failed!\n"); exit(1); } if( bind(client_socket,(struct sockaddr*)&client_addr,sizeof(client_addr))){ printf("Client Bind Port Failed!\n"); exit(1); } struct sockaddr_in server_addr; bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family = AF_INET; if(inet_aton(argv[1],&server_addr.sin_addr) == 0){ printf("Server IP Address Error!\n"); exit(1); } server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT); socklen_t server_addr_length = sizeof(server_addr); if( connect( client_socket, (struct sockaddr*)&server_addr, server_addr_length) < 0){ printf("Can Not Connect To %s!\n",argv[1]); exit(1); } char buffer[BUFFER_SIZE]; bzero(buffer,BUFFER_SIZE); int length = recv( client_socket, buffer,BUFFER_SIZE,0); if(length < 0){ printf("Recieve Data From Server %s Failed!\n", argv[1]); exit(1); } printf("From Server %s :\t%s",argv[1],buffer); bzero(buffer,BUFFER_SIZE); char name[64]; gethostname(name,sizeof(name)); strcpy(buffer,name); send(client_socket,buffer,BUFFER_SIZE,0); close(client_socket);}int main(int argc, char **argv){ if (argc != 2){ printf("Usage: ./%s ServerIPAddress\n",argv[0]); exit(1); } int i=0; for(i=0; i<10000; i++){ talk_to_server(argv); sleep(10); } return 0;}

基本上来说,server端的实现方式,就是:

监听某个port, listen后,开始建立若干的子进程。在父进程里进入sleep/循环;

在子进程里面,select/accept ,accept后作动作,然后循环往复。

好像酒店大堂的门口,开了门纳客(监听);

然后派一批员工(子进程)等在门口(listen/accept),

若有客人来(网络请求),哪个员工(子进程)逮住了就给迎进酒店,领位(服务),然后循环往复。

转载地址:http://tfbfa.baihongyu.com/

你可能感兴趣的文章
在mac OS10.10下安装 cocoapods遇到的一些问题
查看>>
css技巧
查看>>
Tyvj 1728 普通平衡树
查看>>
javascript性能优化
查看>>
多路归并排序之败者树
查看>>
java连接MySql数据库
查看>>
转:Vue keep-alive实践总结
查看>>
深入python的set和dict
查看>>
C++ 11 lambda
查看>>
Android JSON数据解析
查看>>
DEV实现日期时间效果
查看>>
java注解【转】
查看>>
centos 下安装g++
查看>>
嵌入式,代码调试----GDB扫盲
查看>>
下一步工作分配
查看>>
Response. AppendHeader使用大全及文件下载.net函数使用注意点(转载)
查看>>
Wait Functions
查看>>
jQuery最佳实践
查看>>
centos64i386下apache 403没有权限访问。
查看>>
jquery用法大全
查看>>