Linux下多人聊天空间代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Linux下多人聊天空间代码在相同目录下新建文件wwb.h、wwb.c、room.c,把下面相应的代码拷进这两个文件里;再建一个recv.txt,该文件用于保存和显示聊天记录。在Linux中,使用下面命令编译:gcc-oroomroom.cwwb.c使用命令运行:./room使用命令查看聊天记录:tail-frecv.txt//wwb.h头文件intWriteLog(constchar*format,...);//写日志文件char*GetSysTime(char*time);//得到系统时间//wwb.c代码#includetime.h#includeunistd.h#includestdio.h#includesys/mman.h#includefcntl.h#includestring.h#includestdlib.h#includestdarg.hintWriteLog(constchar*format,...){FILE*fp;fp=fopen(recv.txt,a+);if(fp==NULL)return-1;va_listarg_ptr;va_start(arg_ptr,format);vfprintf(fp,format,arg_ptr);va_end(arg_ptr);fclose(fp);return0;}char*GetSysTime(char*out){time_ttimep;structtm*p;time(&timep);p=localtime(&timep);intyear=(1900+p-tm_year);intmonth=(1+p-tm_mon);intday=p-tm_mday;inthour=p-tm_hour;intminute=p-tm_min;intsecond=p-tm_sec;sprintf(out,%d-,year);if(month10)sprintf(out+5,0%d-,month);elsesprintf(out+5,%d-,month);if(day10)sprintf(out+8,0%d,day);elsesprintf(out+8,%d,day);if(hour10)sprintf(out+11,0%d:,hour);elsesprintf(out+11,%d:,hour);if(minute10)sprintf(out+14,0%d:,minute);elsesprintf(out+14,%d:,minute);if(second10)sprintf(out+17,0%d,second);elsesprintf(out+17,%d,second);returnout;}/*intmain(){chartime[50];inti=10;char*s=hello;WriteLog(%s,%d,%s\n,GetSysTime(time),i,s);}*///room.c主程序代码#includestdio.h#includestdlib.h#includestring.h#includeunistd.h#includesys/types.h#includesys/socket.h#includenetinet/in.h#includearpa/inet.h#includewwb.h#defineBUFSIZE255intmain(intargc,char*argv[]){if(argc!=2){printf(Parameter(s):username\n);exit(1);}char*username=argv[1];/*charusername[16]={0};do{printf(请输入你的用户名:);fgets(username,16,stdin);}while(!(strcmp(username,\n)));*/in_port_tservPort=5000;char*bcIP=192.168.12.255;in_port_tbcPort=5000;pid_tpid;pid=fork();if(pid0)//ParentProcess{intsock1=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);if(sock10){printf(socket()failed\n);exit(1);}inton=1;setsockopt(sock1,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on));structsockaddr_inbcAddr;memset(&bcAddr,0,sizeof(bcAddr));bcAddr.sin_family=AF_INET;intrtnVal=inet_pton(AF_INET,bcIP,&bcAddr.sin_addr.s_addr);if(rtnVal==0){printf(inet_pton()failed,invalidaddressstring\n);exit(1);}elseif(rtnVal0){printf(inet_pton()failed\n);exit(1);}bcAddr.sin_port=htons(bcPort);ssize_tnumBytes1=sendto(sock1,username,strlen(username),0,(structsockaddr*)&bcAddr,sizeof(bcAddr));if(numBytes10){printf(sendto()failed\n);exit(1);}ssize_tnumBytes2=sendto(sock1,Hello!,strlen(Hello!),0,(structsockaddr*)&bcAddr,sizeof(bcAddr));if(numBytes20){printf(sendto()failed\n);exit(1);}while(1){charstrMsg[256]={0};printf(yousay:);//scanf(%s,strMsg);fgets(strMsg,256,stdin);if(strcmp(strMsg,quit\n)==0){memset(strMsg,strlen(strMsg),0);ssize_tnumBytes3=sendto(sock1,username,strlen(username),0,(structsockaddr*)&bcAddr,sizeof(bcAddr));if(numBytes30){printf(sendto()failed\n);exit(1);}ssize_tnumBytes4=sendto(sock1,Bye!\n,strlen(Bye!),0,(structsockaddr*)&bcAddr,sizeof(bcAddr));if(numBytes40){printf(sendto()failed\n);exit(1);}exit(0);}if(strcmp(strMsg,\n)==0);else{ssize_tnumBytes5=sendto(sock1,username,strlen(username),0,(structsockaddr*)&bcAddr,sizeof(bcAddr));if(numBytes50){printf(sendto()failed\n);exit(1);}ssize_tnumBytes6=sendto(sock1,strMsg,strlen(strMsg)-1,0,(structsockaddr*)&bcAddr,sizeof(bcAddr));if(numBytes60){printf(sendto()failed\n);exit(1);}}}}elseif(pid==0)//childprocess{intsock2=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);if(sock20){printf(socket()failed\n);exit(1);}structsockaddr_inservAddr;memset(&servAddr,0,sizeof(servAddr));servAddr.sin_family=AF_INET;servAddr.sin_addr.s_addr=htonl(INADDR_ANY);servAddr.sin_port=htons(servPort);if(bind(sock2,(structsockaddr*)&servAddr,sizeof(servAddr))0){printf(bind()failed\n);exit(1);}while(1){structsockaddr_infromAddr;socklen_tfromAddrLen=sizeof(fromAddr);charbuffer1[BUFSIZE]={0};ssize_tnumBytes7=recvfrom(sock2,buffer1,BUFSIZE,0,(structsockaddr*)&fromAddr,&fromAddrLen);if(numBytes70){printf(recvfrom()failed\n);exit(1);}char*fromuser=buffer1;charbuffer2[BUFSIZE]={0};ssize_tnumBytes8=recvfrom(sock2,buffer2,BUFSIZE,0,(structsockaddr*)&fromAddr,&fromAddrLen);if(numBytes80){printf(recvfrom()failed\n);exit(1);}if(strcmp(buffer2,Hello!)==0){charctime[50]={0};WriteLog(%sReceived:%s已进入聊天室!\n,GetSysTime(ctime),fromuser);}elseif(strcmp(buffer2,Bye!)==0){charctime[50]={0};WriteLog(%sReceived:%s已离开聊天室!\n,GetSysTime(ctime),fromuser);}else{charctime[50]={0};WriteLog(%s%ssay:%s\n,GetSysTime(ctime),fromuser,buffer2);}}}}

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功