使用多进程并发服务器时要考虑以下几点:
- 父进程最大文件描述个数(父进程中需要close关闭accept返回的新文件描述符)
- 系统内创建进程个数(与内存大小相关)
- 进程创建过多是否降低整体服务性能(进程调度)
/* wrap.h */
#ifndef __WRAP_H_
#define __WRAP_H_
void perr_exit(const char *s);
int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr);
int Bind(int fd, const struct sockaddr *sa, socklen_t salen);
int Connect(int fd, const struct sockaddr *sa, socklen_t salen);
int Listen(int fd, int backlog);
int Socket(int family, int type, int protocol);
ssize_t Read(int fd, void *ptr, size_t nbytes);
ssize_t Write(int fd, const void *ptr, size_t nbytes);
int Close(int fd);
ssize_t Readn(int fd, void *vptr, size_t n);
ssize_t Writen(int fd, const void *vptr, size_t n);
ssize_t my_read(int fd, char *ptr);
ssize_t Readline(int fd, void *vptr, size_t maxlen);
#endif
/* wrap.c */
#include
#include
#include
#include
#include
void perr_exit(const char *s)
{
perror(s);
exit(-1);
}
int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
int n;
again:
if ((n = accept(fd, sa, salenptr)) < 0) {
if ((errno == ECONNABORTED) || (errno == EINTR))
goto again;
else
perr_exit("accept error");
}
return n;
}
int Bind(int fd, const struct sockaddr *sa, socklen_t salen)
{
int n;
if ((n = bind(fd, sa, salen)) < 0)
perr_exit("bind error");
return n;
}
int Connect(int fd, const struct sockaddr *sa, socklen_t salen)
{
int n;
if ((n = connect(fd, sa, salen)) < 0)
perr_exit("connect error");
return n;
}
int Listen(int fd, int backlog)
{
int n;
if ((n = listen(fd, backlog)) < 0)
perr_exit("listen error");
return n;
}
int Socket(int family, int type, int protocol)
{
int n;
if ((n = socket(family, type, protocol)) < 0)
perr_exit("socket error");
return n;
}
ssize_t Read(int fd, void *ptr, size_t nbytes)
{
ssize_t n;
again:
if ( (n = read(fd, ptr, nbytes)) == -1) {
if (errno == EINTR)
goto again;
else
return -1;
}
return n;
}
ssize_t Write(int fd, const void *ptr, size_t nbytes)
{
ssize_t n;
again:
if ( (n = write(fd, ptr, nbytes)) == -1) {
if (errno == EINTR)
goto again;
else
return -1;
}
return n;
}
int Close(int fd)
{
int n;
if ((n = close(fd)) == -1)
perr_exit("close error");
return n;
}
/*参三: 应该读取的字节数*/
ssize_t Readn(int fd, void *vptr, size_t n)
{
size_t nleft; //usigned int 剩余未读取的字节数
ssize_t nread; //int 实际读到的字节数
char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ((nread = read(fd, ptr, nleft)) < 0) {
if (errno == EINTR)
nread = 0;
else
return -1;
} else if (nread == 0)
break;
nleft -= nread;
ptr += nread;
}
return n - nleft;
}
ssize_t Writen(int fd, const void *vptr, size_t n)
{
size_t nleft;
ssize_t nwritten;
const char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
if (nwritten < 0 && errno == EINTR)
nwritten = 0;
else
return -1;
}
nleft -= nwritten;
ptr += nwritten;
}
return n;
}
static ssize_t my_read(int fd, char *ptr)
{
static int read_cnt;
static char *read_ptr;
static char read_buf[100];
if (read_cnt <= 0) {
again:
if ( (read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
if (errno == EINTR)
goto again;
return -1;
} else if (read_cnt == 0)
return 0;
read_ptr = read_buf;
}
read_cnt--;
*ptr = *read_ptr++;
return 1;
}
ssize_t Readline(int fd, void *vptr, size_t maxlen)
{
ssize_t n, rc;
char c, *ptr;
ptr = vptr;
for (n = 1; n < maxlen; n++) {
if ( (rc = my_read(fd, &c)) == 1) {
*ptr++ = c;
if (c == '\n')
break;
} else if (rc == 0) {
*ptr = 0;
return n - 1;
} else
return -1;
}
*ptr = 0;
return n;
}
/* server.c */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "wrap.h"
#define SRV_PORT 9999
void catch_child(int signum)
{
while ((waitpid(0, NULL, WNOHANG)) > 0);
return ;
}
int main(int argc, char *argv[])
{
pid_t pid;
int ret;
int i; //循环因子 i
char buf[BUFSIZ]; // 子进程buf
int lfd, cfd;
struct sockaddr_in srv_addr; //定义服务器地址结构
struct sockaddr_in clt_addr; // 4 定义客户端地址结构
socklen_t clt_addr_len; // 4
//memset(&srv_addr, 0, sizeof(srv_addr)); //将地址结构清零
bzero(&srv_addr, sizeof(srv_addr));
srv_addr.sin_family = AF_INET; //初始化 IPv4
srv_addr.sin_port = htons(SRV_PORT); //端口号 转为网络字节序
srv_addr.sin_addr.s_addr = htonl(INADDR_ANY); //获取本机任意有效IP
lfd = Socket(AF_INET, SOCK_STREAM, 0); // 1 创建套接字,返回一个文件描述符
Bind(lfd, (struct sockaddr *)&srv_addr, sizeof(srv_addr));// 2 将文件描述符与本地IP端口进行绑定
Listen(lfd, 128); // 3 监听
clt_addr_len = sizeof(clt_addr); // 4
while(1){ //可自定义条件
cfd = Accept(lfd, (struct sockaddr *)&clt_addr, &clt_addr_len); // 4 阻塞等待客户端连接请求
pid = fork();
if(pid < 0){
perr_exit("fork error"); //封装在wrap.c中
}else if(pid == 0){
break;
}else{
struct sigaction act;
act.sa_handler = catch_child;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
ret = sigaction(SIGCHLD, &act, NULL);
if(ret != 0){
perr_exit("sigaction error");
}
close(cfd);
continue;
}
}
if(pid == 0){ //子进程
for(;;){
ret = Read(cfd, buf, sizeof(buf));
if(ret == 0 ){ //检测到客户端关闭
close(cfd);
exit(1); //异常终止退出
}
for(i = 0; i < ret; i++){
buf[i] = toupper(buf[i]); //小写 ——> 大写
}
write(cfd, buf, ret); //写到buf
write(STDOUT_FILENO, buf, ret); //写到屏幕以便查看
}
}
return 0;
}