版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、数字通信(实训)之,客户机与服务器 的实用程序,程序功能,Client acquires user information from Remote server Client is a program that can be called on by user with Remote hostname and username info wanted,客户机,/client.cpp #include # include # include /A application client prog supporting whos service main(int argc, char*argv);
2、int s;/socket descriptor int len;/received data length struct sockaddr_in sa;/internet socket address struct hostent *hp;/host name lookup result; struct servent *sp;/service lookup,客户机,char bufBUFSIZ+1; char *myname,*host,*user;/prog name,remote user and host name myname=argv0; if (argc!=3) exit(1)
3、;/progname host,user host=argv1; user=argv2; /lookup hostname if(hp=gethostname(host)=NULL) /Get a 32bites address of host devided by points and decimal numbers exit(1);/no result /hosts address and type is put into socket struct bcopy(char *)hp-h_addr, (char *) /look up socket number for service,客户
4、机,if (sp=getservbyname(“whois”,”tcp”)=NULL) /get port number exit(1);/no whois service on the host /put the whois socket No. Into socket structure Sa.sin_port=sp-s_port; /allocate socket If(s=socket(hp-h_addesstype,SOCK_STREAM,0)0) exit(1); /connect to the remote server If(connect(s, /sending reques
5、t,客户机,If(write(s,user,strlen(user)!=strlen(user) exit(3); /reading reply While(len=read(s,buf,BUFSIZ)0) write(s,buf,len); Close(s);exit(0); ,服务器,在端口上监听并返回信息 /server.cpp #include # include # include #include # include # include /use:whois hostname,username,port43 #define BACKLOC 5 /queuing No. #defin
6、e MAXHOSTNAME 32 /maximum name len main(argc,argv),服务器, int argc; char*argv; int s,t;/socket descriptors int i; struct sockaddr_in sa,isa;/internet socket address struct hostent *hp;/host name lookup result; struct servent *sp;/service lookup char *myname;/prog name char localhostMAXHOSTNAME+1; myna
7、me=argv0; /look up service entry if(sp=getservbyname(“whois”,”TCP”)=NULL) exit(1);/no this service,服务器,/get host info if(hp=gethostname(localhost, MAXHOSTNAME)=NULL) exit(1); /put socket no and addr info into socket structure sa.sin_port=sp-s_port; bcopy(char *)hp-h_addr, (char *) /setting maximum connections ,s:service port listen(s,BACKLOG);/ BACKLOG is the maxmum queue len received while(1) /waiting for connection,服务器
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 47565-2026农用挂车和农用牵引车许用机械连接组件
- 护理职业健康保护
- 2025年监管科技的NLP合规条款解析
- 2025年家庭服务器网络带宽管理 合理分配流量的技巧
- 护理礼仪中的团队合作精神
- 选剥混茧工班组建设测试考核试卷含答案
- 洗衣粉制造工岗前全能考核试卷含答案
- 普通研磨工安全素养竞赛考核试卷含答案
- 采油平台水手创新应用水平考核试卷含答案
- 打字员变革管理知识考核试卷含答案
- 2025年神农架林区公安局招聘辅警真题
- 胸痹患者中医护理评估与干预
- 2026年4月福建厦门市思明区部分单位联合招聘非在编人员4人笔试模拟试题及答案解析
- 江苏苏豪控股集团秋招面笔试题及答案
- 24J113-1 内隔墙-轻质条板(一)
- 律师事务所内部惩戒制度
- 高中英语课堂形成性评价与听力理解能力提升教学研究课题报告
- 校园校园环境智能监测系统方案
- (2025年)资阳市安岳县辅警考试公安基础知识考试真题库及参考答案
- 职业健康卫生知识培训内容-职业健康培训内容
- 2022年济宁医学院附属医院医护人员招聘考试笔试题库及答案解析
评论
0/150
提交评论