代码如下:
/*********************************************************************************
* Copyright: (C) 2018 weirenqiu
* All rights reserved.
*
* Filename: mul_pro_server.c
* Description: This file
*
* Version: 1.0.0(05/17/2018)
* Author: weirenqiu <>
* ChangeLog: 1, Release initial version on "05/17/2018 03:55:18 PM"
*
********************************************************************************/
/********************************************************************************
* Description:as a concurrent server,Each client opens a process, and the number of clients is equal to the number of child processes on the server.
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUF_SIZE 1024
void read_childproc(int sig); //childprocess destruction
int main (int argc, char **argv)
{
int serv_fd, clit_fd;
struct sockaddr_in serv_addr, clit_addr;
pid_t pid;
struct sigaction act;
socklen_t addr_sz;
int str_len, state;
char buf[BUF_SIZE];
if (argc != 2)
{
printf("Usage: %s \n", argv[0]);
exit(1);
}
act.sa_handler = read_childproc;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
state = sigaction(SIGCHLD, &act, 0);//The registered child process terminates the event.
//Initialize the socket
if( (serv_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
//Initialize
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);//The IP address is set to INADDR_ANY,The system automatically gets the IP address
serv_addr.sin_port = htons(atoi(argv[1]));
//bind the local address to the created socket.
if( bind(serv_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1){
printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
//Start listening for client connections or not .
if( listen(serv_fd, 10) == -1){
printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
printf("======waiting for client's request======\n");
while (1)
{ //Until there is a client connection.
addr_sz = sizeof(clit_addr);
if( (clit_fd=accept(serv_fd, (struct sockaddr*)&clit_addr,&addr_sz)) == -1){
printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
continue;
}
else
puts("new client connected...");
pid = fork(); // creat childprocess
if(pid == -1)
{
close(clit_fd);
continue;
}
if (pid == 0)
{
close(serv_fd);
while((str_len = read(clit_fd, buf, BUF_SIZE)) != 0)
write(clit_fd, buf, str_len);
close(clit_fd);
puts("client disconnected...");
return 0;
}
else
close(clit_fd);
}
close(serv_fd);
return 0;
}
/* ----- End of main() ----- */
void read_childproc(int sig)
{
pid_t pid;
int status;
pid = waitpid(-1, &status, WNOHANG); //销毁子进程
printf("removed proc id: %d \n", pid);
}
回声客户端:
/*********************************************************************************
* Copyright: (C) 2018 weirenqiu
* All rights reserved.
*
* Filename: mul_pro_client.c
* Description: This file
*
* Version: 1.0.0(05/16/2018)
* Author: weirenqiu <>
* ChangeLog: 1, Release initial version on "05/16/2018 02:09:29 PM"
*
********************************************************************************/
/********************************************************************************
* Description:The parent process receives the data, and the subprocess sends the data.
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#define BUF_SIZE 1024
void read_data(int sockfd,char *buf);
void write_data(int sockfd,char *buf);
int main (int argc, char **argv)
{
int sockfd = -1;
pid_t pid;
char buf[BUF_SIZE];
struct sockaddr_in serv_addr;
if(argc !=3)
{
printf("Usage:%s \n",argv[0]);
exit(1);
}
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
exit(1);
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(atoi(argv[2]));
if( inet_pton(AF_INET, argv[1], &serv_addr.sin_addr) <= 0){
printf("inet_pton error for %s\n",argv[1]);
exit(1);
}
if( connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){
printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
exit(1);
}
/*if( (fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{ printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(atoi(argv[2]));
if( inet_pton(AF_INET, argv[1], &serva_ddr.sin_addr) <= 0)
{
printf("inet_pton error for %s\n",argv[1]);
exit(0);
}
if (connect(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == -1)
{
printf("connect socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}*/
/*Customer service I/O segmentation, the parent process is responsible for receiving data, and the child process is responsible for sending data.
* This separation can improve the performance of the programs that frequently exchange data, without having to do the same as before,
* You can only send the next one after you have received the last piece of data.*/
pid =fork();
if(pid == 0){
printf("one\n");
write_data(sockfd,buf);
}
else
read_data(sockfd,buf);
close(sockfd);
return 0;
} /* ----- End of main() ----- */
void read_data(int sockfd,char *buf)
{
while(1)
{
int str_len = read(sockfd,buf,BUF_SIZE);
if(str_len == 0)
return;
buf[str_len] = 0;
printf("message from server:%s",buf);
}
}
void write_data(int sockfd ,char *buf)
{
while(1)
{
fgets(buf,BUF_SIZE,stdin);
if (!strcmp(buf, "q\n") || !strcmp(buf, "Q\n"))
{
shutdown(sockfd,SHUT_WR);
}
write(sockfd,buf,strlen(buf));
}
}