TCP并发服务器多进程编程

环境:linux C

功能:并发服务器实现cs通信

/*server.c*/

#include
#include
#include
#include
#include
#include //文件io read write close fork waitpid
#include //socket() struct sockaddr_in  htonl htons 
#include //socket()
#include //inetaddr() inet_pton()
#include //signal
#define SERV_PORT 5001
#define SERV_IP_ADDR "192.168.152.128"
//子进程退出的消息处理函数
void sig_child_handler(int signo)
{
if(SIGCHLD == signo)
{
waitpid(-1,NULL,WNOHANG);
}
}
int main()
{
int fd = -1;
//创建socket
fd = socket(AF_INET,SOCK_STREAM,0);//流式套接字-TCP
if(fd == -1)
{
perror("socket");
exit(-1);
}
//2绑定socket
//2.1填充struct sockaddr_in
struct sockaddr_in sin;
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(SERV_PORT);//网络字节序的端口号
#if 1 
sin.sin_addr.s_addr = htonl(INADDR_ANY);//代替某个IP inet_addr(SERV_IP_ADDR) 让服务器能绑定在任意IP
#else
if(inet_pton(AF_INET,SERV_IP_ADDR,(void *)&sin.sin_addr.s_addr) != 1)
{
perror("inet_pton");
exit(-1);
}
#endif
//2.2开始绑定socket就是给fd带上属性
if(bind(fd,(const struct sockaddr *)&sin,sizeof(sin)) < 0)
{
perror("bind");
exit(-1);
}
//3 调用listen()将主动套接字变成被动套接字
if(listen(fd,5) == -1)
{
perror("listen");
exit(-1);
}
//4 多进程并发处理客户端连接请求 
while(1)
{
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);


int newfd = accept(fd,(struct sockaddr *)&cin,&addrlen);
if(newfd < 0 )
{
perror("accept");
break;
}
pid_t pid;
if((pid = fork()) < 0)
{
perror("fork");
break;
}
if(pid == 0)
{
close(fd);
char  ipv4_addr[16];
if(!inet_ntop(AF_INET,(void *)&cin.sin_addr.s_addr,ipv4_addr,sizeof(cin)))
{
perror("inet_ntop");
exit(-1);
}
printf("client(%s:%d) is connected\n",ipv4_addr,ntohs(cin.sin_port));
//5通过newfd进行cs通信
char buf[BUFSIZ];
bzero(buf,BUFSIZ);
int ret;
while(1)
{
while((ret = read(newfd,buf,BUFSIZ)) > 0)
{
if(strncasecmp(buf,"quit\n",4) == 0)
{
printf("client is exiting\n");
goto _exit;
}
printf("%s",buf);
}


if(ret == 0 )
{
break;
}
else
{
perror("read");
break;
}


}
exit(0);
}
close(newfd);
signal(SIGCHLD,sig_child_handler);
}
_exit:
//6关闭套接子
close(fd);
return 0;

}


/*client.c*/

#include
#include
#include
#include
#include
#include //文件io read write close
#include //socket() struct sockaddr_in  htonl htons 
#include //socket()
#include //inetaddr() inet_pton()
#define SERV_PORT 5001
#define SERV_IP_ADDR "192.168.152.128"
int main()
{
int fd = -1;
//创建socket
fd = socket(AF_INET,SOCK_STREAM,0);//流式套接字-TCP
if(fd == -1)
{
perror("socket");
exit(-1);
}
//2 connect()申请连接
 //填充sockaddr_in()结构体
struct sockaddr_in sin;
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(SERV_PORT);//网络字节序的端口号
#if 0 
sin.sin_addr.s_addr = inet_addr(SERV_IP_ADDR);
#else
if(inet_pton(AF_INET,SERV_IP_ADDR,(void *)&sin.sin_addr.s_addr) != 1)
{
perror("inet_pton");
exit(-1);
}
#endif
//申请连接
if((connect(fd,(const struct sockaddr *)&sin,sizeof(sin))) < 0)
{
perror("connect");
exit(-1);
}
//
char buf[BUFSIZ];
while(1)
{
if(fgets(buf,BUFSIZ,stdin) == NULL)
{
continue;
}
write(fd,buf,BUFSIZ);
if(!strncasecmp(buf,"quit\n",4))
{
printf("client is exiting\n");
break;
}
}
//关闭套接字
close(fd);

return 0;
}

你可能感兴趣的:(多进程)