一、fork创建进程,来实现多并发
这只是个demon,并不能用于实际项目,多进程,消耗太多资源。没有人这么玩

1、服务端代码:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 
int main(int argc,char **argv)
{
        int s_fd;
        int c_fd;
        int n_read;
        char readBuf[128];
 
        char msg[128] = {0};
        struct sockaddr_in s_addr;
        struct sockaddr_in c_addr;
 
        if(argc != 3)
        {
                printf("param is not good\n");
                exit(-1);
        }
 
        memset(&s_addr,0,sizeof(struct sockaddr_in));
        memset(&c_addr,0,sizeof(struct sockaddr_in));
		
        //1.socket
        s_fd = socket(AF_INET,SOCK_STREAM,0);
        if(s_fd == -1)
        {
                perror("socket");
                exit(-1);
        }
 
        s_addr.sin_family = AF_INET;
        s_addr.sin_port = htons(atoi(argv[2]));
        inet_aton(argv[1],&s_addr.sin_addr);
 
        //2.bind
        bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
 
        //3.listen 
        listen(s_fd,10);
 
        //4.accept
        int clen = sizeof(struct sockaddr_in);
        while(1)
        {
                c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&clen);//父进程负责accept
                if(c_fd == -1)
                {
                        perror("accept");
                }
                printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr));
 
                if(fork() == 0)//创建一个子进程,来实现读写功能,父进程负责accept
                {
                        if(fork() == 0)//创建一个子进程的子进程,来实现写功能
                        {
                                while(1)
                                {
                                        memset(msg,0,sizeof(msg));
                                        printf("input:");
                                        fgets(msg,sizeof(msg),stdin);
                                        write(c_fd,msg,strlen(msg));
                                }
                        }
 
                        //5.read
                        while(1)//子进程父进程,来实现读功能
                        {
                                memset(readBuf,0,sizeof(readBuf));
                                n_read = read(c_fd,readBuf,128);
                                if(n_read == -1)
                                {
                                        perror("read");
                                }
                                else
                                {
                                        printf("get message:%d,%s\n",n_read,readBuf);
                                }
                        }
                        break;
                }
        }
        return 0;
}

2、客户端代码:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 
int main(int argc,char **argv)
{
        int c_fd;
        int n_read;
        char readBuf[128];
 
//      char *msg = "msg form client";
        char msg[128] = {0};
        struct sockaddr_in c_addr;
 
        memset(&c_addr,0,sizeof(struct sockaddr_in));
 
        if(argc != 3)
        {
                printf("parm is not good\n");
                exit(-1);
        }
        //1.socket
        c_fd = socket(AF_INET,SOCK_STREAM,0);
        if(c_fd == -1)
        {
                perror("socket");
                exit(-1);
        }
 
        c_addr.sin_family = AF_INET;
        c_addr.sin_port = htons(atoi(argv[2]));
        inet_aton(argv[1],&c_addr.sin_addr);
 
        //2.connect
        if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr)) == -1)
        {
                perror("connect");
                exit(-1);
        }
      
        if(fork() == 0)//创建一个子进程,来实现写功能
        {
                while(1)
                {
                        memset(msg,0,sizeof(msg));
                        printf("input:");
                        fgets(msg,sizeof(msg),stdin);
                        write(c_fd,msg,strlen(msg));
                }
        }
        while(1)//父进程进程,来实现读功能
        {
                memset(readBuf,0,sizeof(readBuf));
                n_read = read(c_fd,readBuf,128);
                if(n_read == -1)
                {
                        perror("read");
                }
                else
                {
                        printf("get message form server:%d,%s\n",n_read,readBuf);
                }
        }

        
        return 0;
}       

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部