利用C实现聊天室搭建 附带客户端与服务器的代码
来源:互联网 发布:宝宝照片涂鸦软件 编辑:程序博客网 时间:2024/06/07 10:38
利用C语言实现聊天室,首先要搭建一个C/S模型
1:服务端需要拥有一个文件来存放用户名和密码;
每个用户在注册时即创建一个自己的文件,其他用户向该用户发送消息时存放在这个文件里;
2:客户端需要拥有一个系统文件和不同好友之间的聊天文件;
A通过读取服务器中的A文件,将里面的消息分别存放到A的系统消息文件和聊天文件里;
3:C/S之间传递消息利用SOCKET;
参考:http://blog.csdn.net/wjb123sw99/article/details/76898877
搭建聊天室难点有3:
1:实现客户端与服务器的通信
2:实现服务器对多个客户端的服务
3:客户端实现动态聊天;输入的文字不受屏幕上不断输出的文字的影响
首先我们先建立一个简单的1对1 C\S模型
// vi ser.c
#include<stdio.h> #include<string.h>#include<sys/types.h>#include<error.h>#include<sys/socket.h>#include<netinet/in.h>int main() //服务器端{ int socket_fd,connect_fd; char buff[1000]={0}; struct sockaddr_in servaddr; socket_fd=socket(AF_INET,SOCK_STREAM,0); //创建一个套接字 servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(8000); if ( bind(socket_fd,(struct sockaddr*)&servaddr,sizeof(servaddr) )==-1) //给套接字绑定端口和协议 { printf("bind error!\n"); //如果出错,试试等会再运行 return 0; } listen(socket_fd,10); //监听套接字 connect_fd=accept(socket_fd,NULL,NULL); //同意链接 while(1) { if(recv(connect_fd,buff,999,0)==0) //接受数据 break; //如果连接断开则停止循环 printf("get:%s",buff); //输出接受数据的内容 } close(connect_fd); close(socket_fd); return 0;}
//vi lic.c
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>int main(int argc, char** argv) //客户端{ if(argc!=2) return 0; int sockfd; char buff[1000]={0}; struct sockaddr_in servaddr; sockfd=socket(AF_INET,SOCK_STREAM,0); servaddr.sin_family = AF_INET; //iv4协议 servaddr.sin_port = htons(8000); //8000端口 inet_pton(AF_INET, argv[1], &servaddr.sin_addr); //指定IP地址 if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) //链接服务器 { printf("connect error: %s(errno: %d)\n",strerror(errno),errno); return 0; } while(1) { printf("Enter:"); fgets(buff,998,stdin); send(sockfd,buff,strlen(buff),0); }首先编译源代码
gcc ser.c -o ser
gcc lic.c -o lic
如何查看服务器IP
ifconfig
最后搭建c/s
./ser
./lic.c xxx.xxx.xxx.xxx
结果展示
在1对1基础上搭建1对多 C/S模型
注意避免僵尸进程的产生
这里我们只需修改服务器程序
多进程服务器搭建
#include<stdio.h>#include<string.h>#include<sys/types.h>#include<error.h>#include<sys/socket.h>#include<netinet/in.h>#include<signal.h>#include<sys/wait.h>void handle(){ while(waitpid(-1,NULL,WNOHANG)); //处理完当前所有的僵尸进程}int main() //服务端{ int socket_fd,connect_fd; char buff[1000]={0}; struct sockaddr_in servaddr; socket_fd=socket(AF_INET,SOCK_STREAM,0); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(8000); if ( bind(socket_fd,(struct sockaddr*)&servaddr,sizeof(servaddr) )==-1) { printf("bind error!\n"); return 0; } listen(socket_fd,10); while(1) { connect_fd=accept(socket_fd,NULL,NULL); if(!fork()) //每接受一个客户端开一个子进程 { while(1) { if(recv(connect_fd,buff,999,0)==0) return 0; printf("get:%s",buff); } } signal(SIGCHLD,handle); //当子进程结束时,运行handle函数 } return 0;}
多线程服务器搭建
#include<stdio.h> #include<string.h> #include<sys/types.h> #include<error.h> #include<sys/socket.h> #include<netinet/in.h> #include<signal.h> #include<sys/wait.h>#include <pthread.h>void *thread(void *arg){ int connect_fd=*(int *)arg; char buff[1000]={0}; while(1) { if(recv(connect_fd,buff,999,0)==0) return ((void *)0); printf("get:%s",buff); } } int main() //服务端 { int socket_fd,connect_fd; struct sockaddr_in servaddr; socket_fd=socket(AF_INET,SOCK_STREAM,0); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(8000); if ( bind(socket_fd,(struct sockaddr*)&servaddr,sizeof(servaddr) )==-1) { printf("bind error!\n"); return 0; } listen(socket_fd,10); while(1) { connect_fd=accept(socket_fd,NULL,NULL); pthread_t id; int ret; ret=pthread_create(&id,NULL,thread,&connect_fd); // 成功返回0,错误返回错误编号 } return 0; }
在1对多的C/S模型上搭建聊天室
基础功能如下:
1:实现注册新用户和登录的功能
2:可以添加好友
3:可以和好友聊天
完善的功能:
1:查看当前好友是否在线
2:修改昵称
3:实现多人聊天(第三个功能,博主懒,没去码,感兴趣的可以去实现)
分享代码:
客户端和服务端必须在不同目录下运行!!!!!
服务端代码如下
ser.c
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include <sys/wait.h>#include <sys/stat.h>#include <fcntl.h>typedef struct userinfo{ char id[20]; char codes[20]; char name[20]; struct userinfo *next; int friendnum; int state; char friend[100][2][20];}user;user *head,*tail,nowuser,usenull;#include"ser2.h"int main(int argc, char** argv) { int socket_fd, connect_fd; struct sockaddr_in servaddr; char buff[4096]; if( (socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) { printf("create socket error: %s(errno: %d)\n",strerror(errno),errno); exit(0); } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(8000); if( bind(socket_fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) { printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno); exit(0); } if( listen(socket_fd, 10) == -1) { printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno); exit(0); } printf("======waiting for client's request======\n"); //main while(1) { printf("wait......\n"); if( (connect_fd = accept(socket_fd, (struct sockaddr*)NULL, NULL)) == -1) { printf("accept socket error: %s(errno: %d)",strerror(errno),errno); continue; } printf("get a new cli\n"); while(!fork()) //a new ser to a cli { int n=0; n = recv(connect_fd, buff, 4095, 0); if(0==n) return 0; buff[n] = '\0'; printf("recv msg from client: %s\n", buff); char login[]="login\n"; //login id char sign[]="sign\n"; char quit[]="quit\n"; char change[]="change name\n"; char addfr[]="add a friend\n"; char check[]="check file\n"; char chat[]="i went to chat\n"; char renew[]="renew data\n"; char agree[]="agree friend\n"; char seefriend[]="see friend\n"; printf("%s",buff); if(strcmp(buff,seefriend)==0) { char seefi[20]={0}; recv(connect_fd,seefi,20,0); seefr(connect_fd,seefi); } if(strcmp(buff,agree)==0) { char agreeid[4096]={0}; recv(connect_fd,agreeid,20,0); agreefriend(agreeid); send(connect_fd,&nowuser,sizeof(user),0); } if(strcmp(buff,renew)==0) { printf("i am renew my data!!!!!!!!!!!!!!!!!!\n"); FILE *fp=fopen("./userset","a+"); int number=0; fread(&number,4,1,fp); printf("user have %d\n",number); tail=&usenull; for(;number>0;number--) { int oldsize=0; fread(&oldsize,4,1,fp); user *olduser=(user *)malloc(oldsize); fread(olduser,oldsize,1,fp); olduser->next=NULL; tail->next=olduser; tail=tail->next; } fclose(fp); tail=&usenull; int idture=0; while(tail->next!=NULL) { tail=tail->next; if( strcmp(nowuser.id,tail->id)==0 ) { idture++; break; } } printf("my friend number is %d\n",tail->friendnum); nowuser=*tail; freeuser(); printf("my firend number have %d\n",nowuser.friendnum); // send(connect_fd,&nowuser,sizeof(user),0); printf("i am renew data\n"); printf("this id is %s",nowuser.id); int ren=strlen(nowuser.id); printf("id len is %di\n",ren); char renewuser[20]={0}; strcpy(renewuser,nowuser.id); printf("cmp id id %s",renewuser); renewuser[ren-1]='\0'; FILE *fl=fopen(renewuser,"a+"); printf("i open the file %s!\n",renewuser); int renewstate; while(1) { renewstate=0; printf("i will read state\n"); fread(&renewstate,4,1,fl); printf("i am read state is %d\n",renewstate); if(renewstate==0) { send(connect_fd,"read over\n",20,0); printf("read over\n"); break; } if(renewstate==1) { char success[20]={0}; send(connect_fd,"request news\n",20,0); recv(connect_fd,success,20,0); printf("%s",success); printf("request news\n"); int renewsize; fread(&renewstate,4,1,fl); printf("len %d\n",renewstate); char renewid[20]={0}; fread(&renewid,20,1,fl); printf("read id is %s!\n",renewid); send(connect_fd,renewid,20,0); } if(renewstate==3) { char renewid[20]={0}; fread(renewid,20,1,fl); printf("i get new form %s !",renewid); int renewsize; fread(&renewsize,4,1,fl); printf("renewsize=%d\n",renewsize); char renewbuf[4096]={0}; fread(renewbuf,renewsize,1,fl); printf("new is %s",renewbuf); send(connect_fd,"chat news\n",20,0); sleep(1); send(connect_fd,renewid,20,0); sleep(1); send(connect_fd,&renewsize,4,0); sleep(1); send(connect_fd,renewbuf,renewsize,0); } } fclose(fl); fl=fopen(renewuser,"w+"); fclose(fl); } if(strcmp(buff,chat)==0) { printf("user went to chat!\n"); char chatname[20]={0}; recv(connect_fd,chatname,20,0); printf("chatid is %s",chatname); int chatlen=strlen(chatname); chatname[chatlen-1]='\0'; printf("file is %s!\n",chatname); FILE *fk=fopen(chatname,"a+"); int chatstate=3; fwrite(&chatstate,4,1,fk); fwrite(nowuser.id,20,1,fk); char chatbuff[4096]={0}; printf("s1\n"); recv(connect_fd,chatbuff,4095,0); printf("s2\n"); printf("new is %s\n",chatbuff); int chatsize=0; chatsize=strlen(chatbuff)+1; fwrite(&chatsize,4,1,fk); fwrite(chatbuff,chatsize,1,fk); fclose(fk); printf("write over!\n"); } if(strcmp(buff,check)==0) { int newtype=0; char usfile[20]; strcpy(usfile,nowuser.id); int usfilesize=strlen(usfile); usfile[usfilesize-1]='\0'; printf("my file name is %s heihei\n",usfile); FILE *fj=fopen(usfile,"a+"); while(1) { sleep(1); newtype=0; fread(&newtype,4,1,fj); printf("i am read file state data %d\n",newtype); if(newtype==0) { send(connect_fd,"system news read over\n",50,0); fclose(fj); break; } if(newtype==1) { char systemnew[4096]={0}; int systemsize=0; fread(&systemsize,4,1,fj); //requestor id size fread(systemnew,systemsize,1,fj); //requertor id int i4; //check requertor have been now? int i5=0; for(i4=0;i4<nowuser.friendnum;i4++) { if(strcmp(nowuser.friend[i4][0],systemnew)==0) { i5++; break; } } if(i5>0) continue; send(connect_fd,"someone went to be your friend\n",50,0); send(connect_fd,systemnew,4096,0); char n4[20]={0}; recv(connect_fd,n4,19,0); if(strcmp(n4,"agree\n")==0) { agreefriend(systemnew); printf("im new friend %s",nowuser.friend[nowuser.friendnum-1][0]); printf("i have %d friend\n",nowuser.friendnum); send(connect_fd,&nowuser,sizeof(user),0); printf("send success\n"); } continue; } } fj=fopen(usfile,"w+"); close(fj); } if(strcmp(buff,addfr)==0) { int n1=0; char addid[20]={0}; recv(connect_fd,addid,19,0); printf("what is id?id is %s su\n",addid); n1=addfriend(addid); if(n1==0) send(connect_fd,"id not exist\n",50,0); if(n1==1) send(connect_fd,"add friend request already sent\n",50,0); } if(strcmp(buff,change)==0) { int n1=0; char newname[20]={0}; recv(connect_fd,newname,19,0); n1=changeuser(newname); if(n1==2) send(connect_fd,"name change success\n",50,0); else send(connect_fd,"name change error\n",50,0); sleep(1); send(connect_fd,&nowuser,sizeof(user),0); } if(strcmp(buff,login)==0) { int n1; char newid[2][20]={0}; recv(connect_fd, newid,40,0); n1=adduser(newid); if(n1==0) send(connect_fd,"id has been registered\n",50,0); if(n1==1) send(connect_fd,"id registration successful\n",50,0); continue; } if(strcmp(buff,sign)==0) { int n2; char newid[2][20]={0}; recv(connect_fd,newid,40,0); n2=signset(newid); if(n2==0) send(connect_fd,"id does not exist\n",50,0); if(n2==1) { send(connect_fd,"lognin successful\n",50,0); sleep(1); send(connect_fd,&nowuser,sizeof(user),0); } if(n2==2) send(connect_fd,"node is error\n",50,0); continue; } if(strcmp(buff,quit)==0) { if(send(connect_fd, "quit success\n", 26,0) == -1) printf("error\n"); quituser(); close(connect_fd); return 0; } } signal(SIGCHLD,killchild); close(connect_fd); } close(socket_fd); }
ser2.h
void killchild(){ while(waitpid(-1,NULL,WNOHANG));}void seach(){ tail=&usenull; while(tail->next!=NULL) { tail=tail->next; printf("id=%s\n",tail->id); }}void freeuser(){ head=&usenull; tail=head->next; while(head->next!=NULL) { while(tail->next=NULL) { tail=tail->next; head=head->next; } free(tail); head->next=NULL; head=&usenull; tail=head->next; }}int seefr(int connect_fd,char seefi[20]){ FILE *fp=fopen("./userset","a+"); int number=0; fread(&number,4,1,fp); printf("user have %d\n",number); tail=&usenull; for(;number>0;number--) { int oldsize=0; fread(&oldsize,4,1,fp); user *olduser=(user *)malloc(oldsize); fread(olduser,oldsize,1,fp); printf("i am read a user,name is %s\n",olduser->id); olduser->next=NULL; tail->next=olduser; tail=tail->next; } fclose(fp); tail=&usenull; int idture=0; while(tail->next!=NULL) { tail=tail->next; if( strcmp(seefi,tail->id)==0 ) { idture++; break; } } send(connect_fd,&(tail->state),4,0); freeuser(); return 0;}int quituser(){ printf("i will quit\n"); FILE *fp=fopen("./userset","a+"); int number=0; fread(&number,4,1,fp); printf("user have %d\n",number); tail=&usenull; for(;number>0;number--) { int oldsize=0; fread(&oldsize,4,1,fp); user *olduser=(user *)malloc(oldsize); fread(olduser,oldsize,1,fp); printf("i am read a user,name is %s\n",olduser->id); olduser->next=NULL; tail->next=olduser; tail=tail->next; } fclose(fp); tail=&usenull; int idture=0; while(tail->next!=NULL) { tail=tail->next; if( strcmp(nowuser.id,tail->id)==0 ) { idture++; break; } } tail->state=0; FILE *fd=fopen("./userset","w+"); //write new data int newnumber=0; tail=&usenull; while(tail->next!=NULL) { newnumber++; tail=tail->next; } printf("new user have %d\n",newnumber); fwrite(&newnumber,4,1,fd); tail=&usenull; tail=tail->next; while(tail!=NULL) { int newsize=sizeof(user); fwrite(&newsize,4,1,fd); printf("id %s have write file\n",tail->id); printf("state is %d\n",tail->state); fwrite(tail,newsize,1,fd); tail=tail->next; } fclose(fd); freeuser();}int renewuser(){ printf("i am renew my data!!!!!!!!!!!!!!!!!!\n"); FILE *fp=fopen("./userset","a+"); int number=0; fread(&number,4,1,fp); printf("user have %d\n",number); tail=&usenull; for(;number>0;number--) { int oldsize=0; fread(&oldsize,4,1,fp); user *olduser=(user *)malloc(oldsize); fread(olduser,oldsize,1,fp); olduser->next=NULL; tail->next=olduser; tail=tail->next; } fclose(fp); tail=&usenull; int idture=0; while(tail->next!=NULL) { tail=tail->next; if( strcmp(nowuser.id,tail->id)==0 ) { idture++; break; } } printf("my friend number is %d\n",tail->friendnum); nowuser=*tail; freeuser(); return 0;}int adduser(char a[2][20]){ FILE *fp=fopen("./userset","a+"); int number=0; fread(&number,4,1,fp); printf("user have %d\n",number); tail=&usenull; for(;number>0;number--) { int oldsize=0; fread(&oldsize,4,1,fp); user *olduser=(user *)malloc(oldsize); fread(olduser,oldsize,1,fp); printf("i am read a user,name is %s\n",olduser->id); olduser->next=NULL; tail->next=olduser; tail=tail->next; } fclose(fp); tail=&usenull; while(tail->next!=NULL) { tail=tail->next; if( strcmp(a[0],tail->id)==0 ) { printf("this id has been\n"); return 0; } } printf("this id is not find\n"); user *newuser=(user *)malloc(sizeof(user)); //init user strcpy(newuser->id,a[0]); strcpy(newuser->codes,a[1]); strcpy(newuser->name,a[0]); newuser->next=NULL; newuser->friendnum=0; newuser->state=0; tail->next=newuser; printf("filename is %s",a[0]); int cao=strlen(a[0]); a[0][cao-1]='\0'; int asd=creat(a[0],666); //creat myself file close(asd); FILE *fg=fopen(a[0],"w+"); fclose(fg); printf("creat file successs\n"); FILE *fd=fopen("./userset","w+"); //write new data int newnumber=0; tail=&usenull; while(tail->next!=NULL) { newnumber++; tail=tail->next; } printf("new user have %d\n",newnumber); fwrite(&newnumber,4,1,fd); tail=&usenull; tail=tail->next; while(tail!=NULL) { int newsize=sizeof(user); fwrite(&newsize,4,1,fd); printf("id %s have write file\n",tail->id); fwrite(tail,newsize,1,fd); tail=tail->next; } fclose(fd); freeuser(); return 1;}int signset(char a[2][20]){ FILE *fp=fopen("./userset","a+"); int number=0; fread(&number,4,1,fp); printf("user have %d\n",number); tail=&usenull; for(;number>0;number--) { int oldsize=0; fread(&oldsize,4,1,fp); user *olduser=(user *)malloc(oldsize); fread(olduser,oldsize,1,fp); printf("i am read a user,name is %s\n",olduser->id); olduser->next=NULL; tail->next=olduser; tail=tail->next; } fclose(fp); tail=&usenull; int idture=0; while(tail->next!=NULL) { tail=tail->next; if( strcmp(a[0],tail->id)==0 ) { idture++; break; } } if(idture==0) { freeuser(); return 0; } if(strcmp(a[1],tail->codes)==0) //sign success { tail->state=1; nowuser=*tail; FILE *fd=fopen("./userset","w+"); //write new data int newnumber=0; tail=&usenull; while(tail->next!=NULL) { newnumber++; tail=tail->next; } printf("new user have %d\n",newnumber); fwrite(&newnumber,4,1,fd); tail=&usenull; tail=tail->next; while(tail!=NULL) { int newsize=sizeof(user); fwrite(&newsize,4,1,fd); printf("id %s have write file\n",tail->id); fwrite(tail,newsize,1,fd); tail=tail->next; } fclose(fd); freeuser(); return 1; } freeuser(); return 2;}int changeuser( char newname[20]){ FILE *fp=fopen("./userset","a+"); int number=0; fread(&number,4,1,fp); printf("user have %d\n",number); tail=&usenull; for(;number>0;number--) { int oldsize=0; fread(&oldsize,4,1,fp); user *olduser=(user *)malloc(oldsize); fread(olduser,oldsize,1,fp); printf("i am read a user,name is %s\n",olduser->id); olduser->next=NULL; tail->next=olduser; tail=tail->next; } fclose(fp); tail=&usenull; while(tail->next!=NULL) { tail=tail->next; if( strcmp(nowuser.id,tail->id)==0 ) { printf("this id has been\n"); break; } } strcpy(tail->name,newname); nowuser=*tail; FILE *fd=fopen("./userset","w+"); int newnumber=0; tail=&usenull; while(tail->next!=NULL) { newnumber++; tail=tail->next; } printf("new user have %d\n",newnumber); fwrite(&newnumber,4,1,fd); tail=&usenull; tail=tail->next; while(tail!=NULL) { int newsize=sizeof(user); fwrite(&newsize,4,1,fd); printf("id %s have write file\n",tail->id); fwrite(tail,newsize,1,fd); tail=tail->next; } fclose(fd); freeuser(); return 2;}int addfriend(char a[20]){ FILE *fp=fopen("./userset","a+"); //is there a judgment? int number=0; fread(&number,4,1,fp); printf("user have %d\n",number); tail=&usenull; for(;number>0;number--) { int oldsize=0; fread(&oldsize,4,1,fp); user *olduser=(user *)malloc(oldsize); fread(olduser,oldsize,1,fp); printf("i am read a user,name is %s\n",olduser->id); olduser->next=NULL; tail->next=olduser; tail=tail->next; } fclose(fp); tail=&usenull; int addtree=0; while(tail->next!=NULL) { tail=tail->next; if( strcmp(a,tail->id)==0 ) { printf("this id has been\n"); addtree++; //judgment break; } } if(addtree==0) return 0; //id id not judgment char adf[20]; strcpy(adf,tail->id); int adfsize=strlen(adf); adf[adfsize-1]='\0'; freeuser(); printf("open file name is %s\n",adf); FILE *fh=fopen(adf,"a+"); int newsstate=1; fwrite(&newsstate,4,1,fh); int newssize=20; fwrite(&newssize,4,1,fh); fwrite(nowuser.id,newssize,1,fh); fclose(fh); return 1;}int agreefriend(char systemnew[4096]){ FILE *fp=fopen("./userset","a+"); //see you are there? int number=0; fread(&number,4,1,fp); printf("user have %d\n",number); tail=&usenull; for(;number>0;number--) { int oldsize=0; fread(&oldsize,4,1,fp); user *olduser=(user *)malloc(oldsize); fread(olduser,oldsize,1,fp); printf("i am read a user,name is %s\n",olduser->id); olduser->next=NULL; tail->next=olduser; tail=tail->next; } fclose(fp); tail=&usenull; while(tail->next!=NULL) { tail=tail->next; if( strcmp(systemnew,tail->id)==0 ) { printf("on i find he,i agree withe you be friend\n"); break; } } nowuser.friendnum+=1; //friend+1; strcpy(nowuser.friend[nowuser.friendnum-1][0],tail->id); strcpy(nowuser.friend[nowuser.friendnum-1][1],tail->name); tail->friendnum+=1; strcpy(tail->friend[tail->friendnum-1][0],nowuser.id); strcpy(tail->friend[tail->friendnum-1][1],nowuser.name); tail=&usenull; while(tail->next!=NULL) { tail=tail->next; if( strcmp(nowuser.id,tail->id)==0 ) { printf("i find myself in file\n"); break; } } nowuser.next=tail->next; *tail=nowuser; /* i will updata to file*/ FILE *fd=fopen("./userset","w+"); //write new data int newnumber=0; tail=&usenull; while(tail->next!=NULL) { newnumber++; tail=tail->next; } printf("new user have %d\n",newnumber); fwrite(&newnumber,4,1,fd); tail=&usenull; tail=tail->next; while(tail!=NULL) { int newsize=sizeof(user); fwrite(&newsize,4,1,fd); printf("id %s have write file\n",tail->id); fwrite(tail,newsize,1,fd); tail=tail->next; } fclose(fd); freeuser(); return 0;}
客户端代码如下
lic.c
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h>#include <time.h> #include<signal.h>#include<unistd.h>typedef struct userinfo{ char id[20]; char codes[20]; char name[20]; struct userinfo *next; int friendnum; int state; char friend[100][2][20];}user;user myuser;int sockfd_wjb;int chat_wjb;char chatid_wjb[20]={0};int alarmstate;#include"lic.h" int main(int argc, char** argv) { int sockfd, n,rec_len; char recvline[4096], sendline[4096]; char buf[4095]; struct sockaddr_in servaddr; if( argc != 2) { printf("usage: ./client <ipaddress>\n"); exit(0); } if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("create socket error: %s(errno: %d)\n", strerror(errno),errno); exit(0); } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(8000); if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) { printf("inet_pton error for %s\n",argv[1]); exit(0); } if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) { printf("connect error: %s(errno: %d)\n",strerror(errno),errno); exit(0); } char a[2][20]; while(1) { int b=face1(a); if(b==1) { char login[]="login\n"; char num[4096]={0}; send(sockfd,login,strlen(login),0); send(sockfd,a,sizeof(a),0); recv(sockfd,num,4095,0); printf("get:%s",num); getchar(); if(strcmp(num,"id has been registered\n")==0) continue; if(strcmp(num,"id registration successful\n")==0) continue; } if(b==0) { char sign[]="sign\n"; char num[4096]={0}; send(sockfd,sign,strlen(sign),0); send(sockfd,a,sizeof(a),0); recv(sockfd,num,4095,0); printf("get:%s",num); getchar(); if( strcmp(num,"id does not exist\n")==0 ) continue; if( strcmp(num,"node is error\n") ==0 ) continue; if( strcmp(num,"lognin successful\n")==0 ) break; } } printf("1\n"); recv(sockfd,&myuser,4095,0); printf("2\n"); creatfile(); printf("3\n"); renew(sockfd); printf("4\n"); while(1) { int sele=face2(); if(sele==6) { while(1) { printf("please enter you went chat friend id\n"); printf("Enter:"); char chatid[20]={0}; fgets(chatid,20,stdin); int i5=0; int i5a=0; for(i5=0;i5<myuser.friendnum;i5++) { if(strcmp(chatid,myuser.friend[i5][0])==0) { i5a++; break; } } if(i5a==0) { char n6[5]={0}; while(1) { printf("this id not your friend!\n"); printf("1:return main inteface\n"); printf("2:continue\n"); printf("enter:"); fgets(n6,3,stdin); if(strcmp(n6,"1\n")==0) break; if(strcmp(n6,"2\n")==0) break; } if(strcmp(n6,"1\n")==0) break; if(strcmp(n6,"2\n")==0) continue; } if(i5a!=0) { printf("shi is you friend\n"); sockfd_wjb=sockfd; strcpy(chatid_wjb,chatid); chatroom(sockfd,chatid); printf("chat over!"); getchar(); break; } } } if(sele==2) { strcpy(sendline,"quit\n"); if( send(sockfd, sendline, strlen(sendline), 0) < 0) { printf("send msg error: %s(errno: %d)\n", strerror(errno), errno); exit(0); } sleep(1); if((rec_len = recv(sockfd, buf, 4095,0)) == -1) { perror("recv error"); exit(1); } buf[rec_len] = '\0'; printf("get: %s ",buf); char quit[]="quit success\n"; if(strcmp(buf,quit)==0) break; } if(sele==1) { strcpy(sendline,"change name\n"); if( send(sockfd, sendline, strlen(sendline), 0) < 0) { printf("send msg error: %s(errno: %d)\n", strerror(errno), errno); exit(0); } printf("enter you went name:"); char newname[20]; fgets(newname,20,stdin); if( send(sockfd, newname, strlen(newname), 0) < 0) { printf("send msg error: %s(errno: %d)\n", strerror(errno), errno); exit(0); } if((rec_len = recv(sockfd, buf, 4095,0)) == -1) { perror("recv error"); exit(1); } buf[rec_len] = '\0'; printf("get:%s",buf); recv(sockfd,&myuser,4095,0); getchar(); } if(sele==3) seefriend(sockfd); if(sele==4) { strcpy(sendline,"add a friend\n"); char addid[20]={0}; printf("please enter you went add id:"); fgets(addid,19,stdin); int adda; int addab=0; for(adda;adda<myuser.friendnum;adda++) { if( strcmp(myuser.friend[adda][0],addid)==0 ) { printf("he has been you friend,not add\n"); addab=1; break; } } if(addab==1) continue; if( send(sockfd, sendline, strlen(sendline), 0) < 0) { printf("send msg error: %s(errno: %d)\n", strerror(errno), errno); exit(0); } if( send(sockfd, addid, strlen(addid), 0) < 0) { printf("send msg error: %s(errno: %d)\n", strerror(errno), errno); exit(0); } if((rec_len = recv(sockfd, buf, 4095,0)) == -1) { perror("recv error"); exit(1); } buf[rec_len] = '\0'; printf("get:%s",buf); getchar(); } if(sele==5) { renew(sockfd); char systemid[20]={0}; int systemlen=strlen(myuser.id); strcpy(systemid,myuser.id); systemid[systemlen-1]='\0'; FILE *fe=fopen(systemid,"a+"); while(1) { int systemstate; systemstate=0; fread(&systemstate,4,1,fe); if(systemstate==0) break; if(systemstate==1) { int systemsize=0; char systemid[20]={0}; fread(&systemsize,4,1,fe); fread(systemid,20,1,fe); while(1) { char agre[5]={0}; printf("shi went quester be your frienid:%s",systemid); printf("agree to press 1\n"); printf("refuse to press 2\n"); fgets(agre,3,stdin); if(strcmp(agre,"1\n")==0) { send(sockfd,"agree friend\n",20,0); sleep(1); send(sockfd,systemid,20,0); recv(sockfd,&myuser,4096,0); break; } if(strcmp(agre,"2\n")==0) break; } } } fclose(fe); fe=fopen(systemid,"w+"); fclose(fe); } }}
lic.h
int face1(char a[2][20] ){ while(1) { system("clear"); printf("----welcome to use SU chat room----\n"); printf("If you already have an account,press 1\n"); printf("If you do not have an accout,press 2 to register\n"); printf("Enter(1 or 2):"); char num[5]; fgets((char *)&num,5,stdin); if( strcmp(num,"1\n")==0) { printf("Please enter id:"); fgets(a[0],20,stdin); printf("Please enter codes:"); fgets(a[1],20,stdin); return 0; } if( strcmp(num,"2\n")==0 ) { printf("Please enter id:"); fgets(a[0],20,stdin); printf("Please enter codes:"); fgets(a[1],20,stdin); return 1; } }}int face2(){ while(1) { char n[5]={0}; system("clear"); printf("------Hello %s\n",myuser.name); printf("1:Change name\n"); printf("2:qiut\n"); printf("3:see friend\n"); printf("4:add frined\n"); printf("5:seach system news\n"); printf("6:chat friend\n"); printf("pleace enter number:"); fgets(n,3,stdin); if(strcmp(n,"1\n")==0) return 1; if(strcmp(n,"2\n")==0) return 2; if(strcmp(n,"3\n")==0) return 3; if(strcmp(n,"4\n")==0) return 4; if(strcmp(n,"5\n")==0) return 5; if(strcmp(n,"6\n")==0) return 6; }} void creatfile(){ char myname[20]={0}; strcpy(myname,myuser.id); int n=strlen(myname); myname[n-1]='\0'; FILE *fd=fopen(myname,"a+"); fclose(fd); int n1; for(n1=0;n1<myuser.friendnum;n1++) { char friendname[40]={0}; strcpy(friendname,myuser.friend[n1][0]); n=strlen(friendname); friendname[n-1]='\0'; printf("%s,,,,,%s\n",friendname,myname); strcat(friendname,myname); fd=fopen(friendname,"a+"); fclose(fd); } printf("over\n");}int renew(int sockfd){ send(sockfd,"renew data\n",20,0); recv(sockfd,&myuser,sizeof(user),0); while(1) { char renewstate[40]={0}; recv(sockfd,renewstate,20,0); send(sockfd,"send success\n",20,0); if(strcmp(renewstate,"read over\n")==0) return 0; if(strcmp(renewstate,"request news\n")==0) { int n7=1; char renewid[20]={0}; recv(sockfd,renewid,20,0); int renewsize=0; renewsize=strlen(myuser.id); char renewsys[20]={0}; strcpy(renewsys,myuser.id); renewsys[renewsize-1]='\0'; FILE *fq=fopen(renewsys,"a+"); fwrite(&n7,4,1,fq); n7=20; fwrite(&n7,4,1,fq); fwrite(renewid,20,1,fq); fclose(fq); } if(strcmp(renewstate,"chat news\n")==0) { int renewsize; char renewid[20]={0}; int renewbuff[4096]={0}; recv(sockfd,renewid,20,0); recv(sockfd,&renewsize,4,0); recv(sockfd,renewbuff,renewsize,0); char renewfile[40]={0}; strcpy(renewfile,renewid); int n9=strlen(renewfile); renewfile[n9-1]='\0'; strcat(renewfile,myuser.id); n9=strlen(renewfile); renewfile[n9-1]='\0'; FILE *fw=fopen(renewfile,"a+"); fwrite(renewid,20,1,fw); fwrite(&renewsize,4,1,fw); fwrite(renewbuff,renewsize,1,fw); fclose(fw); } } getchar(); }void clearroom(){ int i=0; printf("\033[0m\033[1;1H"); for(i=0;i<25;i++) { printf("\033[0m\033[K"); printf("\n"); } printf("kkk12adsasdfq3r12adkfaefawrawreakwr\n");}void chatnew(){ if(alarmstate!=1) { printf("\n"); } else if(chat_wjb==1) { alarm(3); } else { printf("\033[0m\033[s"); printf("\033[0m\033[1;1H"); char chatid[20]={0}; int sockfd=sockfd_wjb; strcpy(chatid,chatid_wjb); renew(sockfd); int i=0; printf("\033[0m\033[1;1H"); for(i=0;i<25;i++) { printf("\033[0m\033[K"); printf("\n"); } printf("\033[0m\033[1;1H"); printf("------chat room--------\n"); char chatnewid[20]={0}; strcpy(chatnewid,chatid); chatnewid[strlen(chatnewid)-1]='\0'; strcat(chatnewid,myuser.id); chatnewid[strlen(chatnewid)-1]='\0'; int newnum=0; FILE *fr=fopen(chatnewid,"a+"); while(1) { int n8; char systemid[20]={0}; n8=0; n8=fread(systemid,20,1,fr); if(n8==0) break; char chatbuff[4096]={0}; fread(&n8,4,1,fr); fread(chatbuff,n8,1,fr); newnum++; } fclose(fr); fr=fopen(chatnewid,"a+"); while(1) { int n8; char systemid[20]={0}; n8=0; n8=fread(systemid,20,1,fr); if(n8==0) break; char chatbuff[4096]={0}; fread(&n8,4,1,fr); fread(chatbuff,n8,1,fr); if(newnum>7) { newnum--; continue; } if(strcmp(systemid,chatid)==0) printf("friend:%s\n",chatbuff); else printf("me:%s\n",chatbuff); } fclose(fr); printf("\033[0m\033[u"); fflush(stdout); if(alarmstate==1) alarm(2); }}int chatroom(int sockfd,char chatid[20]){ alarmstate=1; chat_wjb=0; char buff[4096]={0}; system("clear"); alarm(2); signal(SIGALRM,chatnew); while(1) { printf("\033[0m\033[30;1H"); printf("\n-------chat %s",chatid); char chat[20]="i went to chat\n"; sleep(1); printf("Enter news:"); fgets(buff,4095,stdin); //buff chat_wjb=1; send(sockfd,chat,20,0); sleep(1); send(sockfd,chatid,20,0); time_t timep; time(&timep); char sendname[4095]={0}; strcpy(sendname,myuser.id); send(sockfd,chat,strlen(chat),0); sendname[strlen(sendname)-1]=':'; buff[strlen(buff)-1]='\0'; strcat(sendname,buff); strcpy(buff,sendname); char timesend[]={" send time:"}; strcat(buff,timesend); strcat(buff,ctime(&timep)); send(sockfd,buff,strlen(buff),0); chat_wjb=0; char chatnewid[20]={0}; strcpy(chatnewid,chatid); chatnewid[strlen(chatnewid)-1]='\0'; strcat(chatnewid,myuser.id); chatnewid[strlen(chatnewid)-1]='\0'; FILE *fr=fopen(chatnewid,"a+"); fwrite(myuser.id,20,1,fr); int sizebuff=strlen(buff);; fwrite(&sizebuff,4,1,fr); fwrite(buff,sizebuff,1,fr); fclose(fr); while(1) { printf("1:quit\n"); printf("2:continue chat\n"); printf("Enter:"); char chatstate[5]={0}; fgets(chatstate,3,stdin); if(strcmp(chatstate,"1\n")==0) { alarmstate=0; return 0; } if(strcmp(chatstate,"2\n")==0) break; } system("clear"); } return 0;} void seefriend(int sockfd){ int i; renew(sockfd); printf("-------your friend link--------\n"); for(i=0;i<myuser.friendnum;i++) { printf("id %s\n",myuser.friend[i][0]); printf("name %s\n",myuser.friend[i][1]); int state; send(sockfd,"see friend\n",20,0); sleep(1); send(sockfd,myuser.friend[i][0],20,0); recv(sockfd,&state,4,0); if(state==0) printf("off line\n"); if(state==1) printf("on line\n"); printf("------------------------\n"); } getchar();}
登录窗口
菜单窗口
聊天窗口
阅读全文
7 0
- 利用C实现聊天室搭建 附带客户端与服务器的代码
- Socket心跳包异常检测的C语言实现,服务器与客户端代码案例
- Socket心跳包异常检测的C语言实现,服务器与客户端代码案例
- Java 网络编程(TCP)实现服务器与客户端1对多的随意顺 序的聊天室
- Android聊天室客户端的实现
- WinSock2简单的服务器与客户端(C语言实现)
- 利用多线程实现linux下C语言的聊天室程序:
- C语言实现基本服务器与客户端
- 流媒体服务器与客户端的搭建
- SVN服务器与客户端的搭建
- QT服务器与客户端的搭建
- 利用C#Socket实现简单聊天室
- 利用socket搭建一个多客户端/服务器的框架
- C#完整聊天室服务器和客户端代码实例
- Windows 下利用FTP实现客户端与服务器文件传输
- Java 利用套接字Socket实现简单的服务器与客户端通信
- 利用桥接实现OpenVPN客户端访问与服务器(windows)同一网段的其它电脑
- Android开发之简单的聊天室(客户端与服务器进行通信)
- C语言注册回调实例
- 东方百货集团荆门东方百货连锁超市
- POJ 2533Longest Ordered Subsequence(最长上升子序列)
- MySQL5.6解压版出错Access denied for user 'root'@'localhost'
- Cause: java.sql.SQLException: Data truncated for column 'LOAN_SUM' at row 1
- 利用C实现聊天室搭建 附带客户端与服务器的代码
- log4j 配置全面解析
- EasyStream系列2之rtmp转rtsp
- C#的MVC架构
- HDU 6129 Just do it
- cocos2d-x 源码分析 : control 源码分析 ( 控制类组件 controlButton)
- 跨站请求伪造防御
- Spring实践之2:AOP的应用
- ros_indigo语音控制