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

📄 client.c

📁 linux平台下的网络通讯程序
💻 C
字号:
#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<errno.h>#include<stdio.h>#include<unistd.h>#include<signal.h>#define SERVER_PORT 8888#define MAX_BUF_SIZE 1024#define MIN_BUF_SIZE 256#define MSGLEN (sizeof(MSG))#define SOCKLEN (sizeof(struct sockaddr_in))void exit();void bzero();size_t strlen(const char *);char *strcpy(char *,const char *);typedef struct{    int ctl;    char uinfo1[MIN_BUF_SIZE];    char uinfo2[MIN_BUF_SIZE];    char msg[MAX_BUF_SIZE];}MSG;void stopChild(pid_t child){	if(kill(child,SIGSTOP)==-1)	{		printf("STOP Child Error:%s\n",strerror(errno));		exit(1);	}}void contChild(pid_t child){	if(kill(child,SIGCONT)==-1)	{		printf("CONTUINE Child Error:%s\n",strerror(errno));		exit(1);	}}void trimStr(char *p){	int len;	len=(strlen(p));	p[len-1]=0;}void send_msg(int sockfd,const MSG *buf,const struct sockaddr_in *addr){    sendto(sockfd,(char *)buf,MSGLEN,0,(struct sockaddr *)addr,sizeof(struct sockaddr_in));}int recv_msg(int sockfd,MSG *p){    int n;    n=recvfrom(sockfd,p,MSGLEN,0,NULL,NULL);    return n;}int main(int argc,char **argv){    int sockfd;    struct sockaddr_in addr,chataddr;    if(argc!=2)    {    	fprintf(stderr,"Usage:%s server_ip\n",argv[0]);    	exit(1);    }	sockfd=socket(AF_INET,SOCK_DGRAM,0);    if(sockfd<0)    {    	fprintf(stderr,"Socket Error:%s\n",strerror(errno));    	exit(1);    }    bzero(&addr,sizeof(struct sockaddr_in));    addr.sin_family=AF_INET;    addr.sin_port=htons(SERVER_PORT);    if(inet_aton(argv[1],&addr.sin_addr)<0)    {			fprintf(stderr,"Ip error:%s\n",strerror(errno));			exit(1);    }    pid_t child;    if((child=fork())==-1)    {    	printf("Fork Error :%s\n",strerror(errno));    	exit(1);    }    else if(child==0)    {			MSG current;    	//子进程无限接收信息    	while(1)    	{				printf("\n");	    	recv_msg(sockfd,&current);				printf("[%s]send to [%s] :%s",current.uinfo1,current.uinfo2,current.msg);				bzero(&current,sizeof(MSG));    	}    }    char ctl_str[MAX_BUF_SIZE];    char my_uid[MAX_BUF_SIZE]="guest";    int logstatus=0;    MSG current;    strcpy(current.uinfo1,"guest");    printf("Your status is no login!\nType /help to see list.\n");	    do    {    	//输入控制命令    	fgets(ctl_str,MAX_BUF_SIZE,stdin);			trimStr(ctl_str);			if(strcmp(ctl_str,"/reg")==0){current.ctl=1;}			else if(strcmp(ctl_str,"/login")==0){current.ctl=2;}			else if(strcmp(ctl_str,"/logout")==0){current.ctl=3;}			else if(strcmp(ctl_str,"/msg")==0){current.ctl=4;}			else if(strcmp(ctl_str,"/listuser")==0){current.ctl=5;}			else if(strcmp(ctl_str,"/help")==0){current.ctl=6;}			else if(strcmp(ctl_str,"/exit")==0){current.ctl=7;}			else current.ctl=0;			switch(current.ctl)			{	    	case 1:{						char temp[MAX_BUF_SIZE];						printf("please input your uid :");						fgets(current.uinfo1,MIN_BUF_SIZE,stdin);						trimStr(current.uinfo1);						printf("please input your pwd :");						fgets(current.uinfo2,MIN_BUF_SIZE,stdin);						trimStr(current.uinfo2);						send_msg(sockfd,&current,&addr);						//bzero(&current,MSGLEN);						stopChild(child);						recv_msg(sockfd,&current);						contChild(child);						int n;						if((n=strcmp(current.msg,"registered"))==0)						{							printf("registere succeddful!\n");						}else						{ 							printf("uid = %s,msg = %s \n",current.uinfo1,current.msg);							printf("[#]unavail user! please try again by using other name.\n");						}					}break;				case 2:{					   if(logstatus == 1)					   {							printf("You have logined,please logout first.\n");					   }						else						{							char temp[MAX_BUF_SIZE];							printf("please input your uid :");							fgets(current.uinfo1,MIN_BUF_SIZE,stdin);							trimStr(current.uinfo1);							printf("please input your pwd :");							fgets(current.uinfo2,MIN_BUF_SIZE,stdin);							trimStr(current.uinfo2);							send_msg(sockfd,&current,&addr);							stopChild(child);							recv_msg(sockfd,&current);							contChild(child);							//printf("msg = %s \n",current.msg);							if(strcmp(current.msg,"login")==0)							{								strcpy(my_uid,current.uinfo1);								logstatus = 1;								printf("login succeddful!\n");							}else							{								if(strcmp(current.msg,"user have login")==0)								{									printf("[#]the ID has login!please try the other ID plz.\n");								}else								printf("[#]unavail user! please confirm your user infomation and try again.\n");							}						}				   }break;				case 3:{					   if(logstatus == 0)					   {						   printf("You haven't logined,please login first.\n");					   }					   else					   {						   int n;						   strcpy(current.uinfo1,my_uid);						   send_msg(sockfd,&current,&addr);						   bzero(current.msg,MAX_BUF_SIZE);						   stopChild(child);						   recv_msg(sockfd,&current);						   contChild(child);						   if((n=strcmp(current.msg,"logout"))==0)						   {							   strcpy(my_uid,"guest");							   logstatus = 0;							   printf("logout succeddful!\n");						   }else						   {							   printf("Something unexpect wrong happend,please try again.\n");						   };		       					   }				   }break;				case 4:{					   if(logstatus==0)					   {						   printf("You haven't logined,please login first.\n");					   }else					   {						   strcpy(current.uinfo1,my_uid);						   printf("sendto :");						   fgets(current.uinfo2,MIN_BUF_SIZE,stdin);						   trimStr(current.uinfo2);						   printf("msg :");						   fgets(current.msg,MAX_BUF_SIZE,stdin);						   trimStr(current.msg);						   send_msg(sockfd,&current,&addr);						   }				   }break;				case 5:{					   if(logstatus==0)					   {						   printf("You haven't logined,please login first.\n");					   }else					   {						   strcpy(current.uinfo1,my_uid);						   trimStr(current.uinfo2);						   send_msg(sockfd,&current,&addr);						   }				   }break;				case 6:{					   printf("/reg\n/login\n/logout\n/msg (send to \"all\" to chat to all)\n/listuser\n/exit\n");				   }break;				case 7:{					   if(logstatus == 1)					   {						   int n;						   current.ctl=3;						   strcpy(current.uinfo1,my_uid);						   send_msg(sockfd,&current,&addr);						   bzero(current.msg,MAX_BUF_SIZE);						   stopChild(child);						   recv_msg(sockfd,&current);						   contChild(child);						   if((n=strcmp(current.msg,"logout"))==0)						   {							   strcpy(my_uid,"guest");							   logstatus = 0;							   printf("logout succeddful!\n");						   }else						   {							   printf("Something unexpect wrong happend,please try again.\n");							   if(kill(child,SIGTERM)==-1)							   {								   printf("Kill Child Error:%s\n",strerror(errno));								   exit(1);							   }						   };		       					   }					   if(kill(child,SIGTERM)==-1)					   {						   printf("Kill Child Error:%s\n",strerror(errno));						   exit(1);					   }					   exit(0);				   }break;			default:break;		}		bzero(&current,MSGLEN);	}while(1);	close(sockfd);}

⌨️ 快捷键说明

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