• Linux select 实现聊天室


    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char* argv[])
    {
    	if (argc <= 2)
    	{
    		printf("usage: %s ip_address port_number\n", basename(argv[0]));
    		return 1;
    	}
    	const char* ip = argv[1];
    	int port = atoi(argv[2]);
    	printf("ip is %s and port is %d\n", ip, port);
    
    	int ret = 0;
    	struct sockaddr_in address;
    	bzero(&address, sizeof(address));
    	address.sin_family = AF_INET;
    	inet_pton(AF_INET, ip, &address.sin_addr);
    	address.sin_port = htons(port);
    
    	int listenfd = socket(PF_INET, SOCK_STREAM, 0);
    	assert(listenfd >= 0);
    
    	ret = bind(listenfd, (struct sockaddr*)&address, sizeof(address));
    	assert(ret != -1);
    
    	ret = listen(listenfd, 5);
    	assert(ret != -1);
    
    	char buf[1024];
    	fd_set read_fds;
    	fd_set allset;
    
    	FD_ZERO(&read_fds);
    	FD_ZERO(&allset);
    
    	FD_SET(listenfd, &allset);
    
    	int maxfd = listenfd;
    
    	
    	while (1)
    	{
    		read_fds = allset;	//  更新事件
    
    		memset(buf, '\0', sizeof(buf));
    		ret = select(maxfd + 1, &read_fds, NULL, NULL, NULL);
    
    		printf("select one\n");
    		if (ret < 0)
    		{
    			printf("selection failure\n");
    			break;
    		}
    
    		for (int myfd = listenfd; myfd <= maxfd; myfd++)
    		{
    			if (FD_ISSET(myfd, &read_fds))
    			{
    
    				if (myfd == listenfd) {
    					struct sockaddr_in client_address;
    					socklen_t client_addrlength = sizeof(client_address);
    
    					int connfd = accept(listenfd, (struct sockaddr*)&client_address, &client_addrlength);
    					if (connfd < 0)
    					{
    						printf("errno is: %d\n", errno);
    						close(listenfd);
    					}
    					int nReuseAddr = 1;
    					setsockopt(connfd, SOL_SOCKET, SO_OOBINLINE, &nReuseAddr, sizeof(nReuseAddr));
    
    					FD_SET(connfd, &allset);
    					if (connfd > maxfd)
    						maxfd = connfd;
    					char remote_addr[INET_ADDRSTRLEN];
    					printf("connected with ip: %s and port: %d\n", inet_ntop(AF_INET, &client_address.sin_addr, remote_addr, INET_ADDRSTRLEN), ntohs(client_address.sin_port));
    
    				}
    				else {
    					ret = recv(myfd, buf, sizeof(buf) - 1, 0);
    					if (ret <= 0)
    					{
    						close(myfd);
    						FD_CLR(myfd, &allset);
    						break;
    					}
    					printf("get %d bytes of normal data: %s\n", ret, buf);
    				}
    			}
    
    		}
    
    	}
    
    	close(listenfd);
    	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
  • 相关阅读:
    分别用c++,python,java写一个行列转换的算法
    如何在 Tomcat 中为 Web 应用程序启用和配置缓存?
    java-php-python-仁爱公益网站计算机毕业设计
    使用cpolar发布群晖NAS博客网站 1(7.X版)
    docker-compose手册
    【毕业设计项目】基于单片机的指纹识别系统实现 - 物联网 stm32 c51
    使用dd命令创建swap交换分区
    Hive 导出数据到 CSV 文件
    在cesuim上展示二维模型
    龙霸区块链:主链开发新突破
  • 原文地址:https://blog.csdn.net/weixin_52068490/article/details/126157489