• 多线程多进程并发服务器


    #include
    #define PORT "8888"
    #define IP "192.168.125.61"
    int deal_cli_msg(int newfd, struct sockaddr_in cin)
    {
    	char buf[128];
    	ssize_t res = 0;
    	while (1)
    	{
    		bzero(buf, sizeof(buf));
    		//接收数据
    		res = recv(newfd, buf, sizeof(buf), 0);
    		if (res < 0)
    		{
    			ERR_MSG("recv");
    			return -1;
    		}
    		else if (0 == res)
    		{
    			newfd = %d \n",newfd);
    			break;
    		}
    		printf("newfd = %d :%s\n",newfd, buf);
    		if (strcmp(buf, "quit") == 0)
    		{
    			break;
    		}
    
    		strcat(buf, "*_*");
    		if (send(newfd, buf, sizeof(buf), 0) < 0)
    		{
    			ERR_MSG("send");
    			return -1;
    		}
    		printf("send success\n");
    	}
    	close(newfd);
    	return 0;
    }
    
    void handle(int sig)
    {
    	while(waitpid(-1, NULL, WNOHANG) > 0);
    }
    
    int main(int argc, const char* argv)
    {
    	if(signal(17, handle) == SIG_ERR)
    	{
    		ERR_MSG("signal");
    		return -1;
    	}
    	
    	int sfd = socket(AF_INET, SOCK_STREAM, 0);
    	if (sfd < 0)
    	{
    		ERR_MSG("socket");
    		return -1;
    	}
    
    	int reuse = 1
    	if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    	{
    		ERR_MSG("setsockopt");
    		return -1;
    	}
    
    	struct sockaddr_in sin;
    	sin.sin_family = AF_INET;
    	sin.sin_port = htons(PORT);
    	sin.sin_addr.sin_addr = inet_addr(IP);
    	
    	if(bind(sfd, (struct*)&sin, sizeof(sin)) < 0)
    	{
    		ERR_MSG("bind");
    		return -1;
    	}
    	
    	if(listen(sfd, 128) < 0)
    	{
    		ERR_MSG("listen");
    		return -1;
    	}
    
    	struct sockaddr_in cin;
    	socklen_t addrlen = sizeof(cin);
    	int newfd = -1;
    	pid_t cpid = -1;
    
    	while(1)
    	{
    		newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
    		if(newfd < 0)
    		{
    			ERR_MSG("fork");
    			return -1;
    		}
    		close(newfd);
    	}
    	close(sfd);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    #include
    #define PORT 8888
    #define IP "192.168.125.61"
    
    struct Climsg
    {
        int newfd;
        struct sockaddr_in cin;
    };
    void* deal_cli_msg(void* arg)
    {
    	int newfd = ((struct Climsg*)arg)->newfd;
        struct sockaddr_in cin = ((struct Climsg*)arg)->cin;
    
        char buf[128] = ""; 
        ssize_t res = 0;
        while(1)
        {   
            bzero(buf, sizeof(buf));   
            res = recv(newfd, buf, sizeof(buf), 0); 
            if(res < 0)
            {
                ERR_MSG("recv");
                break;
            }
            else if(0 == res)
            {
                printf("客户端下线 newfd=%d\n",newfd);
                break;
            }
            printf("newfd=%d : %s\n", newfd, buf);
            strcat(buf, "*_*");
            if(send(newfd, buf, sizeof(buf), 0) < 0)
            {
                ERR_MSG("send");
                break;
            }
            printf("send success\n");
        }   
    
        close(newfd);
        pthread_exit(NULL);
    
    }
    int main(int argc, const char *argv[])
    {
        int sfd = socket(AF_INET, SOCK_STREAM, 0); 
        if(sfd < 0)
        {   
            ERR_MSG("socket");
            return -1; 
        }   
        printf("socket create success sfd=%d\n", sfd);
        int reuse = 1;
        if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
        {   
            ERR_MSG("setsockopt");
            return -1; 
        }   
        printf("允许端口快速的被复用成功\n");
    
        struct sockaddr_in sin;
        sin.sin_family      = AF_INET;     
        sin.sin_port        = htons(PORT);
        sin.sin_addr.s_addr = inet_addr(IP);
    
        if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
        {   
            ERR_MSG("bind");
            return -1; 
        }   
        printf("bind success\n");
    
        if(listen(sfd, 128) < 0)
        {   
            ERR_MSG("listen");
            return -1; 
        }   
        printf("listen success\n");
    
        int newfd = -1; 
        struct sockaddr_in cin;            
        socklen_t addrlen = sizeof(cin);   
        pthread_t tid;
        struct Climsg info;
    
        while(1)
        {   
            newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
            if(newfd < 0)
            {
                ERR_MSG("newfd");
                return -1; 
            }
            printf("newfd=%d\n",newfd);
            info.newfd = newfd;
            info.cin = cin;
            if(pthread_create(&tid, NULL, deal_cli_msg, (void*)&info) != 0)
            {
                fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
                return -1; 
            }
            pthread_detach(tid);
        } 
        if(close(sfd) < 0)
        {   
            ERR_MSG("close");                                                                                     
            return -1; 
        }   
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
  • 相关阅读:
    Ubuntu Server搭建个人服务器
    无限上下文,多级内存管理!突破ChatGPT等大语言模型上下文限制
    Vue3中defineEmits、defineProps 是怎么做到不用引入就能直接用的
    OriginTrail平行链通过XCM与Moonbeam集成
    ssm学术会议管理系统设计与实现毕业设计源码061504
    住宅IP与普通IP的区别
    【小程序源码】花体字转换器支持多种花样字体不同风格
    FFmpeg -r 放在 -i 前后的区别
    微信扫码登陆流程
    Java后台开发中常用规范文档说明
  • 原文地址:https://blog.csdn.net/wbbbzzzzz/article/details/133147319