⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 talks.c

📁 基于Linux操作系统的网络编程
💻 C
字号:
#include<signal.h>#include<sys/wait.h>#include "talk.h"struct word_list {	char word[MAX_WORD_LEN + 1];	struct word_list *next;} *list = NULL;int check_word(char *line){	char buf[MAX_LINE];	struct word_list *word = NULL;	char *p = NULL, *p1 = NULL, *end = NULL;	if (line == NULL)		return 0;	memset(buf, 0, sizeof(buf));	strcpy(buf, line);	if (buf[strlen(buf) - 1] == '\n')		buf[strlen(buf) - 1] = '\0';	p = buf;	end = p + strlen(buf);	while (((p1 = strchr(p, ' ')) != NULL) || 		((p1 = strchr(p, '!')) != NULL) || 		((p1 = strchr(p, ';')) != NULL) || 		((p1 = strchr(p, ',')) != NULL) || 		((p1 = strchr(p, '.')) != NULL) || 		((p1 = strchr(p, '-')) != NULL) || 		((p1 = strchr(p, '_')) != NULL) || 		((p1 = strchr(p, ':')) != NULL) || 		((p1 = strchr(p, '?')) != NULL) || 		((p1 = strchr(p, '+')) != NULL) || 		((p1 = strchr(p, '=')) != NULL) || 		((p1 = strchr(p, '/')) != NULL) || 		((p1 = strchr(p, '\'')) != NULL) || 		((p1 = strchr(p, '\"')) != NULL) || 		(p != end)) { 		if (p1 != NULL)			*p1 = '\0';		if (p != p1) {			printf("check word [%s]\n", p);			for (word = list; word; word = word->next) {				if (strcmp(p, word->word) == 0) {					printf("match word [%s]\n", p);					sprintf(line, "word [%s] is not permited in this room!\n", p);					return -1;				}			}		}		p = p1 + 1;		if (p1 == NULL)			break;	}	return 0;}void join_user(char *line,char *name){	strcpy(name,&line[1]);	sprintf(line,"%s join the room!\n", name);}void add_user(char *line,char *name){	char theline[MAX_LINE];		sprintf(theline, "\"%s\" say: %s", name, line);	strcpy(line,theline);}int free_user(int user_link[MAX_CLIENT]){	int i=0;		while((user_link[i]!=0)&&(i<MAX_CLIENT))		i++;		if(i==MAX_CLIENT)		return(-1);		return(i);}void  add_sockset(fd_set *sockset,int sockfd,int *user_link,int *userfd){	int i;		FD_ZERO(sockset);	FD_SET(sockfd,sockset);	for(i=0;i<MAX_CLIENT;i++){		if(user_link[i]==1){			FD_SET(userfd[i],sockset);		}	}}int init_ser(int port) //if success,return sockfd,else return0{	int  SERV_TCP_PORT;	int   sockfd;	struct sockaddr_in serv_addr;	SERV_TCP_PORT=port;	if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){		perror("socket:");		printf("server:can't open stream socker.\n");		fflush(stdout);		return(0);	}		bzero((char*)&serv_addr,sizeof(serv_addr));	serv_addr.sin_family   =AF_INET;	serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);	serv_addr.sin_port   =htons(SERV_TCP_PORT);		if(bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0){		perror("bind:");		printf("server:can't bind local address\n");		fflush(stdout);		return(0);	}	return(sockfd);//successful.}int maxvalue(int a,int b){	int  themax;	if(a>b)		themax=a;	else		themax=b;	return themax;}int main(void){	int sockfd;	int new_sockfd;	int user_link[MAX_CLIENT];	int userfd[MAX_CLIENT];	char username[MAX_CLIENT][MAX_NAME];	char line[MAX_LINE];	int userCount;	unsigned int cli_len;	struct sockaddr_in cli_addr;	FILE *file;	int port;	int length,i,j;	fd_set sockset;	int maxfd=0;	struct word_list *word = NULL;	char wordbuf[MAX_WORD_LEN + 1];	int len, num;	file = fopen("word", "r");	if (file == NULL) {		printf("open word file error!\n");		//exit(1);	}	memset(wordbuf, 0, sizeof(wordbuf));	while (fgets(wordbuf, sizeof(wordbuf) - 1, file)) {		word = (struct word_list *)malloc(sizeof(struct word_list));		if (word == NULL) {			printf("malloc error\n");			exit(1);		}		memset(word, 0, sizeof(struct word_list));		len = strlen(wordbuf);		if (wordbuf[len - 1] == '\n')			wordbuf[len - 1] = '\0';		strcpy(word->word, wordbuf);		word->next = list;		list = word;		memset(wordbuf, 0, sizeof(wordbuf));	}	fclose(file);	if (list)		printf("get word list:\n");	num = 0;	for (word = list; word; word = word->next) {		num++;		printf("%d %s\n", num, word->word);	}	file=fopen("config","r");	fgets(line,MAX_LINE,file);	fscanf(file,"%d",&port);	fclose(file);		sockfd=init_ser(port);	if(sockfd==0){		printf("init serve socket error\n");		fflush(stdout);		exit(1);	}//Socker init done	listen(sockfd,MAX_CLIENT);	cli_len=sizeof(cli_addr);	for(i=0;i<MAX_CLIENT;i++){		user_link[i]=0;		username[i][0] ='\0';	}	userCount=0;	FD_ZERO(&sockset);	FD_SET(sockfd, &sockset);	maxfd = maxvalue(maxfd, sockfd + 1);	for(;;){		select(maxfd,&sockset,NULL,NULL,NULL);		if (FD_ISSET(sockfd, &sockset)			       && ((userCount = free_user(user_link)) >= 0)) {			new_sockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&cli_len);			if(new_sockfd<0){				user_link[userCount]=0;				printf("accept error\n");			}else{				user_link[userCount]=1;				userfd[userCount]=new_sockfd;				FD_SET(new_sockfd,&sockset);				maxfd=maxvalue(maxfd,new_sockfd+1);			}		}//if userCount>0				for (i = 0; i < MAX_CLIENT; i++) {			if ((user_link[i] == 1) && (FD_ISSET(userfd[i], &sockset))) {				length = read(userfd[i], line, MAX_LINE);				if (length == 0) {//socket is closed.					user_link[i] = 0;					username[i][0] = '\0';					FD_CLR(userfd[i], &sockset);				} else if (length > 0) {					line[length] = '\0';					//printf("get line:%s\n", line);					if ((line[0] == '/') && (username[i][0] == '\0')) {						join_user(line, username[i]);					} else {						if (check_word(line) < 0) {							write(userfd[i], line, strlen(line));							break;						}						add_user(line, username[i]);					}					for (j = 0; j < MAX_CLIENT; j++) {						if ((j != i) && (user_link[j] == 1)) {							write(userfd[j], line, strlen(line));						}					}				}//length>0			}//user_link[i]==1		}//for		add_sockset(&sockset, sockfd, user_link, userfd);	}//for	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -