linux下多人聊天室

来源:互联网 发布:淘宝店铺头像可以换吗 编辑:程序博客网 时间:2024/06/10 21:15
客户端

一个在Linux下可以使用的聊天软件,要求至少实现如下功能:
1. 采用Client/Server架构
2. Client A 登陆聊天服务器前,需要注册自己的ID和密码
3. 注册成功后,Client A 就可以通过自己的ID和密码登陆聊天服务器
4. 多个Client X 可以同时登陆聊天服务器之后,与其他用户进行通讯聊天
5. Client A成功登陆后可以查看当前聊天室内其他在线用户Client x
6. Client A可以选择发消息给某个特定的Client X,即”邮件”功能
7. Client A 可以选择发消息全部的在线用户,即”群发消息”功能


可以选择实现的附加功能:
送欢迎你来到咱们的聊天室”

附加功能:

文件传输

部分主要代码:

#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <pthread.h>#include <unistd.h>#include <stdlib.h>#include <fcntl.h>#include <string.h>#include <netinet/in.h>#include <arpa/inet.h>#include <ctype.h>#include <memory.h>#define SIZE sizeof(X)#include <netinet/ip.h>#include <netinet/tcp.h>#include<time.h>typedef struct xinxi{char id[20];char passwd[20];char name[50];char hy[100][20];int hys;int online;//0不在线,1在线int fd;//struct xinxi *next;}X;typedef struct message{char id[20];//收的人char id1[20];//发的人int type;//好友请求1,私聊2,好友请求回复信息3char mess[1024];struct message * next;}M;int count=0;X* head=NULL;int count1=0;M* head1=NULL;int zhzx(char id[20]);void baocun(){FILE *fp;fp=fopen("123","w");if(fp==NULL){printf("open error\n");return;}printf("count=%d",count);fwrite(&count,sizeof(int),1,fp);X* p;p=head;if(p->next==NULL){fclose(fp);return ;}p=p->next;while(p){fwrite(p,sizeof(X),1,fp);p=p->next;}printf("保存成功\n");fclose(fp);}void baocun1(){FILE *fp;fp=fopen("1234","w");if(fp==NULL){printf("open error\n");return;}printf("count=%d",count1);fwrite(&count1,sizeof(int),1,fp);M* p;p=head1;if(p->next==NULL){fclose(fp);return ;}p=p->next;while(p){fwrite(p,sizeof(X),1,fp);p=p->next;}printf("xx保存成功\n");fclose(fp);}X * create(){X * x;x=(X*)malloc(SIZE);if(x==NULL){printf("create error\n");return NULL;}x->next=NULL;FILE* fp;fp=fopen("123","a+");if(fp==NULL){printf("open error\n");return NULL;}if(fgetc(fp)==EOF){fclose(fp);return x;}rewind(fp);int n;fread(&n,sizeof(int),1,fp);printf("n=%d\n",n);count=n;X t;X *p,*p1;int i;for(i=0;i<n;i++){fread(&t,sizeof(X),1,fp);p1=x;while(p1->next){p1=p1->next;}p=(X*)malloc(sizeof(X));p->next=NULL;strcpy(p->id,t.id);strcpy(p->name,t.name);strcpy(p->passwd,t.passwd);p->hys=t.hys;int j;for(j=0;j<p->hys;j++)strcpy(p->hy[j],t.hy[j]);p->fd=-5;p->online=0;p1->next=p;}fclose(fp);return x;}M * create1(){M * x;x=(M*)malloc(sizeof(M));if(x==NULL){printf("create error\n");return NULL;}x->next=NULL;FILE* fp;fp=fopen("1234","a+");if(fp==NULL){printf("open error\n");return NULL;}if(fgetc(fp)==EOF){fclose(fp);return x;}rewind(fp);int n;fread(&n,sizeof(int),1,fp);printf("n=%d\n",n);count1=n;M t;M *p,*p1;int i;for(i=0;i<n;i++){fread(&t,sizeof(M),1,fp);p1=x;while(p1->next){p1=p1->next;}p=(M*)malloc(sizeof(M));p->next=NULL;strcpy(p->id,t.id);strcpy(p->id1,t.id1);p->type=t.type;strcpy(p->mess,t.mess);p1->next=p;}fclose(fp);return x;}int isnum1(char s[20]){int i=0;while(s[i]){if(!isdigit(s[i]))return 0;i++;}return 1;}void add(int fd)//注册{X *p1,*p,*p2;int leap=0;p=(X*)malloc(SIZE);if(p==NULL)return;char str[256];char str1[256];memset(str,0,256);memset(str1,0,256);strcpy(str,"请输入你想注册的账号");send(fd,str,strlen(str),0);memset(str,0,256);recv(fd,str,256,0);strcpy(str1,str);//strcpy(p->id,str);if(!isnum1(str)){memset(str,0,256);strcpy(str,"请输入纯数字账号!\n");send(fd,str,strlen(str),0);return;}p1=head;while(p1->next){if(strcmp(p1->next->id,str)==0){leap=1;break;}p1=p1->next;}if(leap==1){memset(str,0,256);strcpy(str,"账号重复\n");send(fd,str,strlen(str),0);return;}strcpy(p->id,str1);memset(str,0,256);strcpy(str,"请输入密码");send(fd,str,strlen(str),0);memset(str,0,256);recv(fd,str,256,0);strcpy(p->passwd,str);memset(str,0,256);strcpy(str,"请输入昵称");send(fd,str,strlen(str),0);memset(str,0,256);recv(fd,str,256,0);strcpy(p->name,str);p1=head;while(p1->next){p1=p1->next;}p1->next=p;p->hys=0;p->online=0;p->fd=-5;//p->hy=NULL;p->next=NULL;memset(str,0,256);strcpy(str,"注册成功\n");send(fd,str,strlen(str),0);count++;baocun();}int yzzh(char id[20])//验证id是否存在{X *p;int leap=0;if(head->next==NULL)return 0;p=head->next;while(p){if(strcmp(id,p->id)==0)return 1;p=p->next;}return 0;}int yzmm(char id[20],char passwd[20])//验证passwd是否存在{X *p;int leap=0;if(head->next==NULL)return 0;p=head->next;while(p){if(strcmp(id,p->id)==0&&strcmp(passwd,p->passwd)==0)return 1;p=p->next;}return 0;}void look(int fd,char id[20])//查找好友{X* p,*p1;p=head;char fa[1024];char shou[1024];char shouid[1024];memset(fa,0,1024);memset(shou,0,1024);memset(shouid,0,1024);strcpy(fa,"输入您要查找的号码");send(fd,fa,strlen(fa),0);recv(fd,shouid,1024,0);if(yzzh(shouid)==0){memset(fa,0,1024);strcpy(fa,"此号码不存在");send(fd,fa,strlen(fa),0);return;}p=p->next;while(p){if(strcmp(p->id,id)==0)break;p=p->next;}if(p->hys==0){memset(fa,0,1024);strcpy(fa,"您没有此好友");send(fd,fa,strlen(fa),0);return;}int i;for(i=0;i<(p->hys);i++){if(strcmp((p->hy[i]),shouid)==0){p1=head->next;char str[20];memset(str,0,20);while(p1){if(strcmp(p1->id,shouid)==0){strcpy(str,p1->name);break;}p1=p1->next;}memset(fa,0,1024);sprintf(fa,"此好友账号%s,昵称%s\n",shouid,str);send(fd,fa,strlen(fa),0);return;}}memset(fa,0,1024);sprintf(fa,"您没有此好友");send(fd,fa,strlen(fa),0);}void display(int fd,char id[20])//列出好友信息{X * p,*p1;int i;char fa[1024];char shou[1024];memset(fa,0,1024);memset(shou,0,1024);p=head->next;while(p){if(strcmp(p->id,id)==0)break;p=p->next;}if(p->hys==0){strcpy(fa,"您的好友列表为空");send(fd,fa,strlen(fa),0);return;}for(i=0;i<p->hys;i++){memset(fa,0,1024);p1=head->next;char str[20];int leap=0;memset(str,0,20);while(p1){printf("%s,,,,\n",p->hy[0]);if(strcmp(p1->id,p->hy[i])==0){strcpy(str,p1->name);printf("%s\n",p1->id);leap=1;break;}p1=p1->next;}if(leap==1){memset(fa,0,1024);sprintf(fa,"账号%s\t昵称%s\n",p1->id,p1->name);send(fd,fa,strlen(fa),0);return ;}}}void del(int fd,char id[20])//删除好友{X *p,*p1;int i,leap=0;char fa[1024];char shou[1024];memset(fa,0,1024);memset(shou,0,1024);p=head->next;while(p){if(strcmp(p->id,id)==0)break;p=p->next;}if(p->hys==0){strcpy(fa,"您的好友列表为空");send(fd,fa,strlen(fa),0);return;}memset(fa,0,1024);strcpy(fa,"输入你想删除的好友账号");send(fd,fa,strlen(fa),0);recv(fd,shou,1024,0);for(i=0;i<(p->hys);i++){if(strcmp((p->hy[i]),shou)==0){while(i<(p->hys-1)){strcpy(p->hy[i],p->hy[i+1]);i++;}memset(p->hy[i],0,20);//p->hy[i]="\0";p->hys--;memset(fa,0,1024);sprintf(fa,"删除成功");send(fd,fa,strlen(fa),0);leap=1;baocun();break;}}if(1==leap){p1=head->next;while(p1){if(strcmp(p1->id,shou)==0)break;p1=p1->next;}for(i=0;i<(p1->hys);i++){if(strcmp((p1->hy[i]),p->id)==0){while(i<(p1->hys-1)){strcpy(p1->hy[i],p1->hy[i+1]);i++;}memset(p1->hy[i],0,20);//p->hy[i]="\0";p1->hys--;memset(fa,0,1024);sprintf(fa,"对方那也删除成功");send(fd,fa,strlen(fa),0);baocun();break;}}}else{memset(fa,0,1024);sprintf(fa,"删除失败,您没有此好友");send(fd,fa,strlen(fa),0);}}void addhy(int fd,char id[20]){X *p,*p1;int i;char fa[1024];char shou[1024];memset(fa,0,1024);memset(shou,0,1024);p=head->next;while(p){if(strcmp(p->id,id)==0)break;p=p->next;}strcpy(fa,"输入你想添加的好友账号");send(fd,fa,strlen(fa),0);recv(fd,shou,1024,0);memset(fa,0,1024);strcpy(fa,"消息已发送");send(fd,fa,strlen(fa),0);if(yzzh(shou)==0){memset(fa,0,1024);strcpy(fa,"没有此账号");send(fd,fa,strlen(fa),0);return;}if(strcmp(shou,p->id)==0){memset(fa,0,1024);strcpy(fa,"不能添加自己为好友");send(fd,fa,strlen(fa),0);return;}for(i=0;i<p->hys;i++){if(strcmp(shou,p->hy[i])==0){memset(fa,0,1024);strcpy(fa,"此好友已添加,无需再添加");send(fd,fa,strlen(fa),0);return;}}p1=head->next;time_t timep;while(p1){if(strcmp(p1->id,shou)==0){M* m,*p3;m=(M*)malloc(sizeof(M));m->type=1;strcpy(m->id,shou);strcpy(m->id1,p->id);memset(fa,0,1024);time(&timep);sprintf(fa,"%s\n%s想添加您为好友,y同意,n不同意",ctime(&timep),p->id);strcpy(m->mess,fa);m->next=NULL;p3=head1;while(p3->next)p3=p3->next;p3->next=m;count1++;//如果对方在线,给对方发送有新消息if(zhzx(p1->id)==1){memset(fa,0,1024);strcpy(fa,"您有新消息,请输入open查看");send(p1->fd,fa,strlen(fa),0);}baocun1();}p1=p1->next;}}xgnc(int fd,char id[20]){X *p,*p1;int i;char fa[1024];char shou[1024];memset(fa,0,1024);memset(shou,0,1024);p=head->next;while(p){if(strcmp(p->id,id)==0)break;p=p->next;}sprintf(fa,"我的昵称:%s\n我的账号:%s我的密码:%s",p->name,p->id,p->passwd);send(fd,fa,strlen(fa),0);memset(fa,0,1024);strcpy(fa,"\t1.修改昵称\n\t2.修改密码");send(fd,fa,strlen(fa),0);recv(fd,shou,1024,0);if(strcmp(shou,"1")==0){memset(fa,0,1024);strcpy(fa,"请输入新的昵称");send(fd,fa,strlen(fa),0);memset(shou,0,1024);recv(fd,shou,1024,0);strcpy(p->name,shou);baocun();return ;}else if(strcmp(shou,"2")==0){memset(fa,0,1024);strcpy(fa,"请输入新的密码");send(fd,fa,strlen(fa),0);memset(shou,0,1024);recv(fd,shou,1024,0);strcpy(p->passwd,shou);baocun();//保存一次return ;}else{memset(fa,0,1024);strcpy(fa,"输入非法");send(fd,fa,strlen(fa),0);return;}}void * siliao(int fd,char id[20]){X *p,*p1;int i,leap=0;char fa[1024];char shou[1024];memset(fa,0,1024);memset(shou,0,1024);p=head->next;while(p){if(strcmp(p->id,id)==0)break;p=p->next;}strcpy(fa,"请输入你想私聊的好友账号");send(fd,fa,strlen(fa),0);recv(fd,shou,1024,0);if(strcmp(shou,p->id)==0){memset(fa,0,1024);strcpy(fa,"不能与自己聊天");send(fd,fa,strlen(fa),0);return;}if(p->hys==0){memset(fa,0,1024);strcpy(fa,"你没有此好友");send(fd,fa,strlen(fa),0);return;}printf("phys:%d\n",p->hys);for(i=0;i<p->hys;i++){if(strcmp(p->hy[i],shou)==0){leap=1;p1=head->next;while(p1){if(strcmp(p1->id,shou)==0)break;p1=p1->next;}printf("p fd%d,p1 fd%d\n",p->fd,p1->fd);memset(fa,0,1024);strcpy(fa,"请输入你要发送给他的内容,输入over结束");send(fd,fa,strlen(fa),0);if(p1->fd==-5){memset(fa,0,1024);strcpy(fa,"提示:好友不在线,可能无法及时回复");send(fd,fa,strlen(fa),0);}M* m,*p8;int kkk=0;time_t timep;while(1){memset(shou,0,1024);if(recv(fd,shou,1024,0)==-1)return;if(strcmp(shou,"over")==0)break;kkk=1;p8=head1;while(p8->next){p8=p8->next;}m=(M*)malloc(sizeof(M));m->type=2;strcpy(m->id,p1->id);strcpy(m->id1,p->id);memset(fa,0,1024);p8->next=m;m->next=NULL;time(&timep);sprintf(fa,"%s%s say:%s\n",ctime(&timep),p->id,shou);strcpy(m->mess,fa);printf("%s对%s说%s\n",m->id1,m->id,m->mess);count1++;printf("count1=%d\n",count1);}if(zhzx(p1->id)==1&&kkk==1){memset(fa,0,1024);strcpy(fa,"您有新消息,请输入open查看");send(p1->fd,fa,strlen(fa),0);}baocun1();return;}}memset(fa,0,1024);strcpy(fa,"你没有此好友");send(fd,fa,strlen(fa),0);}int zhzx(char id[20])//验证账号是否在线{X* p;p=head;while(p){if((strcmp(p->id,id)==0)&&p->online==1)return 1;//在线返回1p=p->next;}return 0;}char a[100][20];//放聊天室人的idint len=0;//聊天室人数void duorenliao(int fd,char id[20]){system("play -q 11.wav  repeat 2"); char fa[1024];char shou[1024];memset(fa,0,1024);memset(shou,0,1024);strcpy(fa,"您已进入聊天室,输入stop退出,输入look查看当前人");send(fd,fa,strlen(fa),0);strcpy(a[len],id);len++;int i;X*p;time_t timep;time(&timep);while(1){memset(shou,0,1024);recv(fd,shou,1024,0);if(strcmp(shou,"stop")==0){for(i=0;i<len;i++){if(strcmp(a[i],id)==0){while(i<len-1){strcpy(a[i],a[i+1]);i++;}}}len--;for(i=0;i<len;i++){p=head->next;while(p){if(strcmp(p->id,a[i])==0){memset(fa,0,1024);sprintf(fa,"%s退出聊天室",id);send(p->fd,fa,strlen(fa),0);break;}p=p->next;}}return;}if(strcmp(shou,"look")==0){memset(fa,0,1024);sprintf(fa,"当前聊天室有%d人,他们是:",len);send(fd,fa,strlen(fa),0);for(i=0;i<len;i++){p=head->next;while(p){if(strcmp(p->id,a[i])==0){memset(fa,0,1024);sprintf(fa,"昵称是%s  账号是%s\n",p->name,p->id);send(fd,fa,strlen(fa),0);break;}p=p->next;}}continue;}for(i=0;i<len;i++){p=head->next;while(p){if(strcmp(p->id,a[i])==0&&strcmp(p->id,id)!=0){memset(fa,0,1024);time(&timep);sprintf(fa,"%s%s say: %s",ctime(&timep),id,shou);send(p->fd,fa,strlen(fa),0);break;}p=p->next;}}}}void * handleclient(void * newfd){int fd=(int)newfd;char recvbuf[1024];char recvbuf1[1024];char sendbuf[1024];memset(recvbuf,0,sizeof(recvbuf));memset(recvbuf1,0,sizeof(recvbuf1));memset(sendbuf,0,1024);if(recv(fd,recvbuf,1024,0)==-1){perror("recv");return;}else{printf("cli say:%s\n",recvbuf);}while(1){memset(sendbuf,0,1024);strcpy(sendbuf,"欢迎使用本服务,可使用功能如下\n\t1.登录帐号\n\t2.注册帐号\n\tend.退出\n");send(fd,sendbuf,strlen(sendbuf),0);memset(recvbuf,0,sizeof(recvbuf));recv(fd,recvbuf,1024,0);if(strcmp(recvbuf,"1")==0){memset(sendbuf,0,1024);strcpy(sendbuf,"请输入登录账号");if(send(fd,sendbuf,strlen(sendbuf),0)==-1){perror("send");return;}memset(recvbuf,0,sizeof(recvbuf));if(recv(fd,recvbuf,1024,0)==-1){perror("recv");return;}memset(sendbuf,0,sizeof(sendbuf));strcpy(sendbuf,"请输入登录密码");if(send(fd,sendbuf,strlen(sendbuf),0)==-1){perror("send");return;}memset(recvbuf1,0,sizeof(recvbuf1));if(recv(fd,recvbuf1,1024,0)==-1){perror("recv");return;}//匹配账号密码是否存在if(yzzh(recvbuf)==0||yzmm(recvbuf,recvbuf1)==0){memset(sendbuf,0,1024);strcpy(sendbuf,"输入账号或密码不正确");send(fd,sendbuf,strlen(sendbuf),0);}else if(zhzx(recvbuf)==1){memset(sendbuf,0,1024);strcpy(sendbuf,"此帐号在线");send(fd,sendbuf,strlen(sendbuf),0);}else{strcpy(recvbuf1,recvbuf);memset(sendbuf,0,1024);strcpy(sendbuf,"登陆成功\n");X*p;p=head;while(p->next){if(strcmp(p->next->id,recvbuf1)==0){p->next->online=1;p->next->fd=fd;break;}p=p->next;}send(fd,sendbuf,strlen(sendbuf),0);//登陆时判断是否有新消息M* p9;p9=head1;int le=0;while(p9){if(strcmp(p9->id,recvbuf1)==0){le=1;memset(sendbuf,0,1024);strcpy(sendbuf,"您有新消息,请输入open查看\n");send(fd,sendbuf,strlen(sendbuf),0);break;}p9=p9->next;}//登录后可以使用的功能while(1){memset(sendbuf,0,1024);strcpy(sendbuf,"你可以使用的功能如下\n\t1.查找好友\n\t2.显示好友\n\t3.删除好友\n\t4.添加好友\n\t5.查看或修改个人信息\n\t6.选择好友私聊\n\t7.群聊\n\t8.退出\n\topen.查看消息");send(fd,sendbuf,strlen(sendbuf),0);memset(recvbuf,0,sizeof(recvbuf));if(recv(fd,recvbuf,1024,0)==-1){perror("recv");return;}if(strcmp(recvbuf,"1")==0)//查找好友{look(fd,recvbuf1);}else if(strcmp(recvbuf,"2")==0)//显示好友列表{display(fd,recvbuf1);}else if(strcmp(recvbuf,"3")==0)//删除好友{del(fd,recvbuf1);}else if(strcmp(recvbuf,"4")==0)//添加好友{addhy(fd,recvbuf1);}else if(strcmp(recvbuf,"5")==0)//查看或修改个人信息{xgnc(fd,recvbuf1);}else if(strcmp(recvbuf,"6")==0)//与好友私聊{siliao(fd,recvbuf1);}else if(strcmp(recvbuf,"7")==0)//进入多人聊天室{duorenliao(fd,recvbuf1);}else if(strcmp(recvbuf,"8")==0){p=head;while(p->next){if(strcmp(p->next->id,recvbuf1)==0){p->next->online=0;p->next->fd=-5;break;}p=p->next;}break;}else if(strcmp(recvbuf,"open")==0)//打开消息{char fa[1024];char shou[1024];M* m;int lea=0;m=head1->next;printf("count111=%d\n",count1);while(m){if(strcmp(m->id,recvbuf1)==0){lea=1;if(m->type==1)//好友请求消息{memset(fa,0,1024);strcpy(fa,m->mess);send(fd,fa,strlen(fa),0);while(1){memset(shou,0,1024);recv(fd,shou,1024,0);if(strcmp(shou,"y")==0){m->type=3;strcpy(m->id,m->id1);strcpy(m->id1,recvbuf1);memset(fa,0,1024);sprintf(fa,"%s已同意添加,添加成功",recvbuf1);strcpy(m->mess,fa);X* p,*p1;p=head->next;p1=head->next;while(p){if(strcmp(p->id,m->id)==0)break;p=p->next;}while(p1){if(strcmp(p1->id,m->id1)==0)break;p1=p1->next;}strcpy(p->hy[p->hys],p1->id);p->hys++;strcpy(p1->hy[p1->hys],p->id);p1->hys++;if(zhzx(m->id)==1){memset(fa,0,1024);strcpy(fa,"您有新消息,请输入open查看");send(p->fd,fa,strlen(fa),0);}baocun();baocun1();break;}else if(strcmp(shou,"n")==0){X* p;p=head->next;while(p){if(strcmp(p->id,m->id1)==0)break;p=p->next;}m->type=3;strcpy(m->id,m->id1);strcpy(m->id1,recvbuf1);memset(fa,0,1024);sprintf(fa,"%s已拒绝添加请求,添加失败",recvbuf1);strcpy(m->mess,fa);if(zhzx(m->id)==1){memset(fa,0,1024);strcpy(fa,"您有新消息,请输入open查看");send(p->fd,fa,strlen(fa),0);}baocun1();break;}}}else if(m->type==3)//处理普通消息,看完消息,链表中删除{lea=1;memset(fa,0,1024);strcpy(fa,m->mess);send(fd,fa,strlen(fa),0);M*m1,*m2;m1=head1;while(m1->next){if(strcmp(m1->next->id,recvbuf1)==0){m2=m1->next;m1->next=m2->next;free(m2);break;}m1=m1->next;}baocun1();count1--;if(m==NULL);break;}else if(m->type==2){   lea=1;M* p7;p7=head1->next;while(p7){int ppp=0;if((strcmp(p7->id,recvbuf1)==0)&&(p7->type==2)){memset(fa,0,1024);strcpy(fa,p7->mess);send(fd,fa,strlen(fa),0);M *m1,*m2;m1=head1;while(m1->next){if((strcmp(m1->next->id,recvbuf1)==0)&&(m1->next->type==2)){m2=m1->next;m1->next=m2->next;free(m2);ppp=1;break;}m1=m1->next;}}if(ppp==1){count1--;baocun1();p7=head1->next;continue;}p7=p7->next;}}}if(m==NULL)break;m=m->next;}if(lea==0){memset(fa,0,1024);strcpy(fa,"您没有任何未处理消息");send(fd,fa,strlen(fa),0);}}}}}else if(strcmp(recvbuf,"2")==0){add(fd);}else if(strcmp(recvbuf,"end")==0){memset(sendbuf,0,1024);strcpy(sendbuf,"欢迎使用,谢谢");send(fd,sendbuf,strlen(sendbuf),0);break;}else {continue;}}close(fd);}//判断客户端是否依旧在线void * panduan(void){X* p;char fa[1024];p=head;while(1){p=head->next;while(p){if(p->online==1){struct tcp_info info;int len=sizeof(info);getsockopt(p->fd,IPPROTO_TCP,TCP_INFO,&info,(socklen_t*)&len);if((info.tcpi_state!=TCP_ESTABLISHED)){p->online=0;p->fd=-5;}/*memset(fa,0,1024);if(recv(p->fd,fa,1024,0)==0){p->online=0;p->fd=-5;}*/}p=p->next;}}}int main(){int sockfd=0,newfd=0;int chlid=0;int ret=0;int len=sizeof(struct sockaddr);struct sockaddr_in myaddr;struct sockaddr_in otheraddr;memset(&myaddr,0,len);memset(&otheraddr,0,len);//tcp套接字连接/*if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){perror("sockfd");return -1;}else {printf("socket success...%d\n",sockfd);}*/int reuse = 1;    sockfd = socket(AF_INET, SOCK_STREAM,0);    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)    {        perror("setsockopet error\n");        return -1;    }//初始化结构体myaddr.sin_family = AF_INET;myaddr.sin_port = htons(8889);myaddr.sin_addr.s_addr=inet_addr("192.168.194.129");//myaddr.sin_addr.s_addr=INADDR_ANY;//绑定套接字if(-1==(bind(sockfd,(struct sockaddr*)&myaddr,len))){perror("bind");return -1;}else{printf("bind success...\n");}//开始侦听if((listen(sockfd,10))==-1){perror("listen");return -1;}else{printf("listen success...\n");}printf("server waiting...\n");pthread_t id1,id2;head=create();head1=create1();while(1){if((newfd=accept(sockfd,(struct sockaddr*)&otheraddr,&len))==-1){perror("accept");return -1;}else{printf("accept success...client's fd=%d,sever's fd=%d\n",newfd,sockfd);printf("--client ip=%s,port=%d\n",inet_ntoa(otheraddr.sin_addr),ntohs(otheraddr.sin_port));}//线程客户端pthread_create(&id1,NULL,handleclient,(void*)newfd);//判断客户端是否依旧在线pthread_create(&id2,NULL,(void*)panduan,NULL);}close(sockfd);return 0;}

原创粉丝点击