linux上的一个简单的多人聊天室.docx

上传人:b****6 文档编号:5996038 上传时间:2023-01-02 格式:DOCX 页数:33 大小:22.29KB
下载 相关 举报
linux上的一个简单的多人聊天室.docx_第1页
第1页 / 共33页
linux上的一个简单的多人聊天室.docx_第2页
第2页 / 共33页
linux上的一个简单的多人聊天室.docx_第3页
第3页 / 共33页
linux上的一个简单的多人聊天室.docx_第4页
第4页 / 共33页
linux上的一个简单的多人聊天室.docx_第5页
第5页 / 共33页
点击查看更多>>
下载资源
资源描述

linux上的一个简单的多人聊天室.docx

《linux上的一个简单的多人聊天室.docx》由会员分享,可在线阅读,更多相关《linux上的一个简单的多人聊天室.docx(33页珍藏版)》请在冰豆网上搜索。

linux上的一个简单的多人聊天室.docx

linux上的一个简单的多人聊天室

linux上的一个简单的多人聊天室002i.hisausedforcreatingalibrary003forserverclient

004Mar182010

005

006

007#ifndef_I_H

008

009#define_I_H

010

011#includemath.h

012#includestdio.h

013#includesyssocket.h

014#includearpainet.h

015#includenetinetin.h

016#includeunistd.h

017#includesystypes.h

018#includesysstat.h

019#includefcntl.h

020#includestdlib.h

021#includeerrno.h

022#includestring.h

023#includeinttypes.h

024#includetime.h

025#includesysioctl.h

026#includenetif.h

027#includesignal.h

028#includencurses.h

029#includemath.h

030

031#defineSEVR_IP127.0.0.1

032#defineSEVR_PORT8081

033#defineCNTNT_LEN150

034#defineMSG_LENsizeof(structmsg035#defineADDR_LENsizeof(structsockaddr036#defineUSR_LENsizeof(structuser

037#definePRT_LEN8

038#defineHSTR_LENsizeof(structchat_history039

040declareGlobalvariables

041intmainfd;usedaschathistroyfilehandle

042intsockfd;usedassocketlocalhandle

043intcount;

044structsockaddr_inserver;

045

046msgisusedforcommunicatingmessage

047structmsg

048{

049intflag;flagmeaning1,ordinary;2,logmsg;3,regmsg,other,file050intid_from;

051intid_to;

052charcontent[CNTNT_LEN];

053charappend[10];

054};

055

056userisusedinformationlist

057structuser

058{

059intid;

060charname[10];

061charpassword[10];

062charp_chatlog;

063structsockaddruser_addr;

064};

065chat_historyusedforreadingchathistory

066structchat_history

067{

068charcontent[CNTNT_LEN];

069chartime[25];

070intto;

071intfrom;

072intcount;

073};

074

075i_functionsbelowisfuntionsneededbybothclientandsever076externinti_saveto_chat(structmsgpmsg;

077

078inti_clean_stdin(

079{

080while('n'==getchar(

081{

082continue;

083}

084

085return(0;

086}

087

088inti_print(charpmsg,intsize

089{

090inti=1;

091

092for(i;i=size;i++

093{

094if(pmsg!

='n'095{

096printf(%c,pmsg;097pmsg++;

098}

099else

100{

101return(0;

102}

103}

104

105return(0;

106}

107inti_input(charp_input

108{

109charc='0';

110inti;

111

112for(i=0;iCNTNT_LEN;i++

113{

114p_input[i]=getchar(;

115if(p_input[i]=='n'

116{

117return(0;

118}

119}

120

121printf(youhaveinputlongenough!

n;

122return(0;

123}

124inti_socket(intdomain,inttype,intprotocol125{

126intfd;

127

128if((fd=socket(domain,type,protocol==-1129{

130perror(creatsocketerror;

131exit(1;

132}

133

134return(fd;

135}

136

137inti_bind(intfd,conststructsockaddraddr,intnamelen138{

139if(-1==bind(fd,addr,namelen

140{

141perror(i_binderror;

142exit(1;

143}

144

145return(0;

146}

147

148inti_recvfrom(intfd,voidbuf,size_tlen,intflags,149structsockaddraddr,intsize

150{

151if(-1==recvfrom(fd,buf,len,flags,addr,size152{

153perror(i_recvfromerror;

154exit(1;

155}

156

157return(0;

158}

159

160inti_sendto(intfd,voidbuf,size_tlen,intflags,161structsockaddraddr,intsize

162{

163if(-1==sendto(fd,buf,len,flags,addr,size164{

165perror(i_sendtoerror;

166exit(1;

167}

168

169return(0;

170}

171

172inti_open(constcharpathname,intflags

173{

174intfd;

175if((fd=open(pathname,flags==-1176{

177perror(open_failed;178exit(1;

179}

180

181return(fd;

182}

183inti_read(intfd,voidmsg,intlen184{

185if(-1==read(fd,msg,len186{

187perror(i_readerror;188exit(1;

189}

190return(0;

191}

192inti_write(intfd,voidmsg,intlen193{

194if(-1==write(fd,msg,len195{

196perror(i_writeerror;197exit(0;

198}

199return(0;

200}

201

202initasocket,fileandserveraddr

203inti_init(

204{

205mainfd=i_open(.chat_log,O_RDWRO_CREAT;206sockfd=i_socket(AF_INET,SOCK_DGRAM,0;207

208initializeserveraddress

209bzero(&server,sizeof(server;

210server.sin_family=AF_INET;

211inet_pton(AF_INET,127.0.0.1,&server.sin_addr;212server.sin_port=htons(SEVR_PORT;

213

214perror(init;

215

216return(0;

217}

218

219chari_get_time(

220{

221time_ttime_now;

222time(&time_now;

223

224return(ctime(&time_now;225}

226inti_lseek(intfd,off_tsize,intposition227{

228if(-1==lseek(fd,size,position229{

230perror(seekerror;

231exit(1;

232}

233return(0;

234}

235inti_saveto_chat(structmsgpmsg236{

237structchat_historyhstr;

238

239

240bzero(&hstr,HSTR_LEN;241count=count+1;

242hstr.count=count;

243hstr.from=pmsg-id_from;

244hstr.to=pmsg-id_to;

245strncpy(hstr.content,pmsg-content,CNTNT_LEN;246strncpy(hstr.time,i_get_time(,25;

247

248i_lseek(mainfd,0,SEEK_END;

249

250i_write(mainfd,&hstr,HSTR_LEN;

251

252return(0;

253}

254

255inti_print_history(intlen,inti

256{

257structchat_historychat_reader;

258intj;

259intposition;

260

261bzero(&chat_reader,HSTR_LEN;

262if(i!

=0

263{

264position=leniHSTR_LEN;

265i_lseek(mainfd,position,SEEK_END;266}

267else

268{

269position=leniHSTR_LEN;

270

271i_lseek(mainfd,HSTR_LEN,SEEK_SET;272}

273

274for(j=1;j=len;j++

275{

276

277i_read(mainfd,&chat_reader,HSTR_LEN;278printf(n#item%did%dtoid%dn,j,

279chat_reader.from,chat_reader.to;280i_print(chat_reader.content,CNTNT_LEN;281printf(nTime%sn,chat_reader.time;282}

283

284return(0;

285}

286

287#endif

server.c~6KB下载(364

viewsourceprint001

002titleserver.c

003contentserverpart

004starttimeMar.25.2011

005endtimeApr.82011

006

007

008#includei.h

009

010intuser_list_fd;

011

012startinitialization

013intinit(

014{

015i_init(;

016

017user_list_fd=i_open(.user_list,O_RDWRO_CREAT;018

019structuserusr;

020inittheuserlistfile'sfistuserto0

021memset((structuser&usr,'0',sizeof(structuser;022i_lseek(user_list_fd,0,SEEK_SET;

023i_write(user_list_fd,(char&usr,USR_LEN;

024

025bindthestructsockaddr_inservertothesockfd026i_bind(sockfd,(structsockaddr&server,ADDR_LEN;027

028structchat_historyapple;

029

030bzero(&apple,HSTR_LEN;

031i_lseek(mainfd,0,SEEK_SET;

032i_write(mainfd,&apple,HSTR_LEN;

033i_lseek(mainfd,-HSTR_LEN,SEEK_END;

034i_read(mainfd,&apple,HSTR_LEN;

035count=apple.count;

036

037return(0;

038}

039endinitialization

040

041startmessagecontrol

042intsend_msg(structmsgmsg_recv,structsockaddraddr

043{

044inti;

045structuserusr;

046

047acommonmessagecome

048printf(aordinarmessagecome!

n;

049

050i=msg_recv-id_to;

051i_lseek(user_list_fd,iUSR_LEN,SEEK_SET;

052i_read(user_list_fd,&usr,USR_LEN;

053strncpy(msg_recv-append,usr.name,10;

054

055i_sendto(sockfd,msg_recv,MSG_LEN,0,

056&(usr.user_addr,ADDR_LEN;

057

058printf(id%dsendamessagetoid%dsucess!

n,msg_recv-id_from,msg_recv-id_to;059

060return(0;

061}

062intcheck_login(structmsgmsg_recv,structsockaddraddr

063{

064inti=msg_recv-id_from;;

065structuserusr;

066

067aloginrequet

068printf(aloginrequestcome!

n;

069

070gettheid'sinformation

071i_lseek(user_list_fd,iUSR_LEN,SEEK_SET;

072i_read(user_list_fd,&usr,USR_LEN;

073

074intn;

075n=strcmp(usr.password,msg_recv-content;

076如果验证成功,则发送成功信息

077if(n==0

078{

079saveusernewaddress

080i_lseek(user_list_fd,-USR_LEN,SEEK_CUR;

081usr.user_addr=addr;

082i_write(user_list_fd,&usr,USR_LEN;

083telluserpass

084i_sendto(sockfd,(structmsgmsg_recv,sizeof(structmsg,0,085&(usr.user_addr,ADDR_LEN;

086

088else

089{

090出错的话的respond

091if(0!

=n

092{

093printf(id%dloginerror.n,i;

094bzero(msg_recv-content,CNTNT_LEN;

095msg_recv-flag=-1;

096i_sendto(sockfd,(structmsgmsg_recv,sizeof(structmsg,0,097&(usr.user_addr,ADDR_LEN;

098

099}

100return(1;

101}

102printf(Id%dloginsucess!

n,i;

103

104return(0;

105}

106intreg_user(structmsgmsg_recv,structsockaddraddr

107{

108structuserusr;

110printf(aregitrequetcomen;

111

112findthelastuserandhavathepleasetoaddanewuser113intn;

114i_lseek(user_list_fd,-USR_LEN,SEEK_END;

115i_read(user_list_fd,&usr,USR_LEN;

116把新用户的信息赋值到usr然后填入到userlistfile中117constcharname;

118constcharpassword;

119

120name=&(msg_recv-content[0];

121password=&(msg_recv-content[10];

122strcpy((usr.name,name;

123strcpy(usr.password,password;

124memcpy(&(usr.user_addr,addr,ADDR_LEN;

125

126usr.id=(usr.id+1;

127i_lseek(user_list_fd,0,SEEK_END;

128i_write(user_list_fd,&usr,USR_LEN;

129

130msg_recv-id_from=usr.id;

131registtotheuserlistthentelltheuserregsuccess

132i_sendto(sockfd,(structmsgmsg_recv,sizeof(structmsg,0,133addr,ADDR_LEN;

134

135printf(Id%dregistsucess!

n,usr.id;

136

137return(0;

138

139}

140intmsg_cntl(

141{

142structmsgmsg_recv;

143structsockaddraddr_recv;

144

145printf(beginlisteninput...n;

146intsize=ADDR_LEN;

147

148for(;;

149{

150bzero(&msg_recv,MSG_LEN;

151i_recvfrom(sockfd,&msg_recv,sizeof(structmsg,0,152&addr_recv,&size;

153printf(messagereceived...n;

154

155i_saveto_chat(&msg_recv;

156

157switch(msg_recv.flag

158{

159case1

160send_msg(&msg_recv,(structsockaddr&addr_recv;sendordinarychat161break;

162case2

163check_login(&msg_recv,(structsockaddr&addr_recv;

164break;

165case3

166reg_user(&msg_recv,(structsockaddr&addr_recv;

167break;

168default

169break;

170}

171}

172return(0;

173}

174endmessagecontrol

175startexit_sys(

176intexit_sys(

177{

178close(sockfd;

179close(mainfd;

180close(user_list_fd;

181printf(exitsystem;

182kill(0,SIGABRT;

183

184exit(0;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1